Skip to main content

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

resolvers {
Mutation {
resolve("addMovie") {
val title = arguments["title"]
val directorName = arguments["director"]

println("Adding $title from $directorName")
}
}

type<Director>("Director") {
resolve("movies") {
source.name
}
}
}
note

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:

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:

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"
}
}
KeyRequiredTypeDescription
packageNameOptionalStringThe package name in which the codegen will generate the types. By default, it'll use com.arianegraphql.codegen
scalarsRequired if you have scalarsMap<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:

sourceSets {
main {
kotlin {
srcDir("build/generated/ariane/main/kotlin")
}
}
}