README.md 5.4 KB
Newer Older
1 2
# GraphQL Codegen Gradle plugin #

3
![Build](https://github.com/kobylynskyi/graphql-java-codegen/workflows/Build/badge.svg)
B
Bogdan Kobylynskyi 已提交
4
[![Gradle Plugins](https://img.shields.io/maven-metadata/v/https/plugins.gradle.org/m2/io/github/kobylynskyi/graphql-java-codegen-gradle-plugin/maven-metadata.xml.svg?label=gradle)](https://plugins.gradle.org/plugin/io.github.kobylynskyi.graphql.codegen)
5 6
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

7 8 9 10 11 12 13 14 15
* [Plugin Setup](#plugin-setup)
* [Plugin Options](#plugin-options)
* [Sample Plugin Configuration](#sample-plugin-configuration)
* [Examples](#examples)
  * [GraphQL **server** code generation](#graphql-server-code-generation)
  * [GraphQL **client** code generation](#graphql-client-code-generation)
* [Different configurations for graphql schemas](#different-configurations-for-graphql-schemas)
* [Convert generated Java classes to Kotlin classes](#convert-generated-java-classes-to-kotlin-classes)

16 17 18 19 20

### Plugin Setup

```groovy
plugins {
21
  id "io.github.kobylynskyi.graphql.codegen" version "4.1.1"
22 23 24 25 26 27 28 29 30 31 32 33 34
}
```

Using [legacy plugin application](https://docs.gradle.org/current/userguide/plugins.html#sec:old_plugin_application):

```groovy
buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
35
    classpath "io.github.kobylynskyi.graphql.codegen:graphql-codegen-gradle-plugin:4.1.1"
36 37 38 39 40 41
  }
}

apply plugin: "io.github.kobylynskyi.graphql.codegen"
```

42 43
### Plugin Options

44
Please refer to [Codegen Options](../../docs/codegen-options.md)
45 46

### Sample Plugin Configuration
47 48 49 50 51

#### build.gradle:

```groovy
graphqlCodegen {
B
Bogdan Kobylynskyi 已提交
52 53 54
    // all config options: 
    // https://github.com/kobylynskyi/graphql-java-codegen/blob/master/docs/codegen-options.md
    graphqlSchemas.includePattern = "schema\\.graphqls"
55
    outputDir = new File("$buildDir/generated")
56 57 58 59 60 61
    packageName = "com.example.graphql.model"
    customTypesMapping = [
        DateTime: "org.joda.time.DateTime",
        Price.amount: "java.math.BigDecimal"
    ]
    customAnnotationsMapping = [
62
        DateTime: ["@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = com.example.json.EpochMillisScalarDeserializer.class)"]
63 64 65 66 67 68 69 70 71 72 73
    ]
    modelNameSuffix = "TO"
}

// Automatically generate GraphQL code on project build:
compileJava.dependsOn 'graphqlCodegen'

// Add generated sources to your project source sets:
sourceSets.main.java.srcDir "$buildDir/generated"
```

74 75
You can also refer to build.gradle files in example projects: [example-client/build.gradle](example-client/build.gradle), [example-server/build.gradle](example-server/build.gradle)

76 77
#### build.gradle.kts:

78 79
```kotlin
tasks.named<GraphQLCodegenGradleTask>("graphqlCodegen") {
B
Bogdan Kobylynskyi 已提交
80 81
    // all config options: 
    // https://github.com/kobylynskyi/graphql-java-codegen/blob/master/docs/codegen-options.md
82 83
    graphqlSchemaPaths = listOf("$projectDir/src/main/resources/graphql/schema.graphqls")
    outputDir = File("$buildDir/generated")
84 85
    packageName = "com.example.graphql.model"
    customTypesMapping = mutableMapOf(Pair("EpochMillis", "java.time.LocalDateTime"))
86
    customAnnotationsMapping = mutableMapOf(Pair("EpochMillis", listOf("@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = com.example.json.EpochMillisScalarDeserializer.class)")))
87 88 89 90
}

// Automatically generate GraphQL code on project build:
sourceSets {
91
    getByName("main").java.srcDirs("$buildDir/generated")
92 93 94
}

// Add generated sources to your project source sets:
95 96 97
tasks.named<JavaCompile>("compileJava") {
    dependsOn("graphqlCodegen")
}
98 99 100
```


101 102
### Examples

103 104 105
#### GraphQL **server** code generation

[example-server](example-server):
106 107
  * [Plugin configuration in build.gradle](example-server/build.gradle)
  * [GraphQL Resolver classes that implement generated interfaces](example-server/src/main/java/io/github/kobylynskyi/product/graphql/resolvers)
108 109 110 111

#### GraphQL **client** code generation 

[example-client](example-client):
112
  * [Plugin configuration in build.gradle](example-client/build.gradle)
B
Bogdan Kobylynskyi 已提交
113
  * [Building GraphQL request and parsing response using Spring RestTemplate](example-client/src/main/java/io/github/kobylynskyi/order/external/product/ProductServiceGraphQLClient.java)
114 115 116 117
  * [Building GraphQL request and parsing response using RestAssured](example-client/src/test/java/io/github/kobylynskyi/order/service/CreateProductIntegrationTest.java)


### Different configurations for graphql schemas
118 119 120 121 122

If you want to have different configuration for different `.graphqls` files (e.g.: different javaPackage, outputDir, etc.), then you will need to create separate gradle tasks for each set of schemas. E.g.:

```groovy
task graphqlCodegenService1(type: GraphqlCodegenGradleTask) {
B
Bogdan Kobylynskyi 已提交
123
    graphqlSchemas.includePattern = "schema1\\.graphqls"
124 125 126 127
    outputDir = new File("$buildDir/generated/example1")
}

task graphqlCodegenService2(type: GraphqlCodegenGradleTask) {
B
Bogdan Kobylynskyi 已提交
128
    graphqlSchemas.includePattern = "schema2\\.graphqls"
129 130 131 132 133 134
    outputDir = new File("$buildDir/generated/example2")
}
```

Later on you can call each task separately or together:

135 136 137
* `gradle clean graphqlCodegenService1 build`
* `gradle clean graphqlCodegenService2 build`
* `gradle clean graphqlCodegenService1 graphqlCodegenService2 build`
138 139 140 141


### Convert generated Java classes to Kotlin classes

142 143
1. Navigate in IntelliJ IDEA to the `./build/generated/graphql/` folder and press `Cmd+Alt+Shift+K`
2. Access generated classes as normal Kotlin classes.
144 145 146 147 148 149


### Inspired by

[swagger-codegen](https://github.com/swagger-api/swagger-codegen)