- Plugin Setup
- Plugin Options
- Sample Plugin Configuration
- Examples
- Different configurations for graphql schemas
- Convert generated Java classes to Kotlin classes
plugins {
id "io.github.kobylynskyi.graphql.codegen" version "5.10.0"
}
Using legacy plugin application:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "io.github.kobylynskyi.graphql.codegen:graphql-codegen-gradle-plugin:5.10.0"
}
}
apply plugin: "io.github.kobylynskyi.graphql.codegen"
Please refer to Codegen Options
graphqlCodegen {
// all config options:
// https://github.com/kobylynskyi/graphql-java-codegen/blob/main/docs/codegen-options.md
graphqlSchemas.includePattern = "schema\\.graphqls"
outputDir = new File("$buildDir/generated")
packageName = "com.example.graphql.model"
customTypesMapping = [
DateTime: "org.joda.time.DateTime",
Price.amount: "java.math.BigDecimal"
]
customAnnotationsMapping = [
DateTime: ["@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = com.example.json.EpochMillisScalarDeserializer.class)"]
]
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"
You can also refer to build.gradle files in example projects: example-client/build.gradle , example-server/build.gradle
tasks.named<GraphQLCodegenGradleTask>("graphqlCodegen") {
// all config options:
// https://github.com/kobylynskyi/graphql-java-codegen/blob/main/docs/codegen-options.md
graphqlSchemaPaths = listOf("$projectDir/src/main/resources/graphql/schema.graphqls")
outputDir = File("$buildDir/generated")
packageName = "com.example.graphql.model"
customTypesMapping = mutableMapOf(Pair("EpochMillis", "java.time.LocalDateTime"))
customAnnotationsMapping = mutableMapOf(Pair("EpochMillis", listOf("@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = com.example.json.EpochMillisScalarDeserializer.class)")))
}
// Automatically generate GraphQL code on project build:
sourceSets {
getByName("main").java.srcDirs("$buildDir/generated")
}
// Add generated sources to your project source sets:
tasks.named<JavaCompile>("compileJava") {
dependsOn("graphqlCodegen")
}
- Plugin configuration in build.gradle
- Building GraphQL request and parsing response using Spring RestTemplate
- Building GraphQL request and parsing response using RestAssured
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.:
task graphqlCodegenService1(type: GraphqlCodegenGradleTask) {
graphqlSchemas.includePattern = "schema1\\.graphqls"
outputDir = new File("$buildDir/generated/example1")
}
task graphqlCodegenService2(type: GraphqlCodegenGradleTask) {
graphqlSchemas.includePattern = "schema2\\.graphqls"
outputDir = new File("$buildDir/generated/example2")
}
Later on you can call each task separately or together:
gradle clean graphqlCodegenService1 build
gradle clean graphqlCodegenService2 build
gradle clean graphqlCodegenService1 graphqlCodegenService2 build
- Navigate in IntelliJ IDEA to the
./build/generated/graphql/
folder and pressCmd+Alt+Shift+K
- Access generated classes as normal Kotlin classes.