Codegen Gradle plugin
The codegen
Gradle Plugin is a specialized tool designed to automate the code generation process based on a GraphQL schema.
This plugin simplifies the development workflow by automatically creating resolvers and types that corresponds to your GraphQL schema, reducing the manual effort required and ensuring consistency across your project.
Example
- Without plugin
- With plugin
resolvers {
Mutation {
resolve("addMovie") {
val title = arguments["title"]
val directorName = arguments["director"]
println("Adding $title from $directorName")
}
}
type<Director>("Director") {
resolve("movies") {
source.name
}
}
}
resolvers {
Mutation {
addMovie { (title, director) ->
println("Adding $title from $director")
}
}
Director {
movies {
source.name
}
}
}
The codgen plugin is not necessary to use ArianeGraphQL.
Getting started
1. Apply the plugin
Add the following to your build.gradle
file to apply the codegen plugin:
- Groovy
- Kotlin
plugins {
id 'com.arianegraphql.codegen' version '$arianeVersion'
}
plugins {
id("com.arianegraphql.codegen") version "$arianeVersion"
}
Note the version must be the same one used accross the arianegraphql
dependencies.
2. Configure the codegen plugin
Configure the code generation in your build.gradle
file, specifying the GraphQL schema file and other relevant settings:
- Groovy
- Kotlin
ariane {
schema file 'src/main/resources/schema.graphqls' //Path to your graphQL schema
configuration file 'src/main/resources/codegen.json' //Path to the codegen configuration file
}
ariane {
schema(file("src/main/resources/schema.graphqls")) //Path to your graphQL schema
configuration(file("src/main/resources/codegen.json")) //Path to the codegen configuration file
}
3. Configuration file
The codegen plugin uses a json configuration file. You can store it at the same level than the graphql schema or anywhere else in your project.
The configuration file must have this form:
{
"packageName": "com.example",
"scalars": {
"Date": "java.time.Instant"
}
}
Key | Required | Type | Description |
---|---|---|---|
packageName | Optional | String | The package name in which the codegen will generate the types. By default, it'll use com.arianegraphql.codegen |
scalars | Required if you have scalars | Map<String, String> | Key-value map representing the Scalars and their JVM type. The specified type must be a full qualified name type. |
4. Add the generated sourceSets to your project
Add the following lines in your build.gradle
file:
- Groovy
- Kotlin
sourceSets {
main {
kotlin {
srcDir 'build/generated/ariane/main/kotlin'
}
}
}
sourceSets {
main {
kotlin {
srcDir("build/generated/ariane/main/kotlin")
}
}
}