Gradle Integration
Integrate the GraphQL Code Generator into your Gradle build process to automatically generate code whenever your schema changes.
Basic Setup
1plugins {2id 'java'3}45// Define source sets for generated code6sourceSets {7main {8java {9srcDir 'src/main/java'10srcDir 'build/generated/graphql'11}12}13}1415// Task to run code generator16tasks.register('generateGraphQLCode', JavaExec) {17group = 'code generation'18description = 'Generates GraphQL schema code'1920classpath = sourceSets.main.runtimeClasspath21mainClass = 'com.example.codegen.BlogCodeGenerator'2223inputs.files(fileTree('src/main/resources/graphql'))24outputs.dir('build/generated/graphql')25}2627// Ensure code generation runs before compilation28tasks.named('compileJava') {29dependsOn('generateGraphQLCode')30}3132// Clean generated code on clean33tasks.named('clean') {34delete 'build/generated'35}3637dependencies {38// The GraphQL plugin provides both the code generator and the runtime framework39implementation 'com.brightspot.graphql:graphql'40}41
How It Works
sourceSets- Adds the generated sources directory to your Java source sets so the compiler can find themgenerateGraphQLCodetask - Runs your code generator wrapper classinputs.files/outputs.dir- Gradle up-to-date checking (only regenerates when schema changes)compileJava.dependsOn- Ensures code generation runs before compilation
Multiple GraphQL APIs
If you have multiple GraphQL schemas in the same project, create separate tasks:
1tasks.register('generateUserAPI', JavaExec) {2classpath = sourceSets.main.runtimeClasspath3mainClass = 'com.example.codegen.UserAPICodeGenerator'45inputs.file('src/main/resources/graphql/user-api.graphql')6outputs.dir('build/generated/graphql')7}89tasks.register('generateAdminAPI', JavaExec) {10classpath = sourceSets.main.runtimeClasspath11mainClass = 'com.example.codegen.AdminAPICodeGenerator'1213inputs.file('src/main/resources/graphql/admin-api.graphql')14outputs.dir('build/generated/graphql')15}1617tasks.register('generateAllAPIs') {18dependsOn('generateUserAPI', 'generateAdminAPI')19}2021tasks.named('compileJava') {22dependsOn('generateAllAPIs')23}
Multi-Module Projects
For projects with separate schema modules:
1sourceSets {2main {3java {4srcDir 'src/main/java'5srcDir 'build/generated/graphql/public'6srcDir 'build/generated/graphql/admin'7srcDir 'build/generated/graphql/internal'8}9}10}1112tasks.register('generatePublicAPICode', JavaExec) {13classpath = sourceSets.main.runtimeClasspath14mainClass = 'com.example.codegen.PublicAPICodeGenerator'15inputs.files(fileTree('graphql-schemas/public-api'))16outputs.dir('build/generated/graphql/public')17}1819tasks.register('generateAdminAPICode', JavaExec) {20classpath = sourceSets.main.runtimeClasspath21mainClass = 'com.example.codegen.AdminAPICodeGenerator'22inputs.files(fileTree('graphql-schemas/admin-api'))23outputs.dir('build/generated/graphql/admin')24}2526tasks.register('generateInternalAPICode', JavaExec) {27classpath = sourceSets.main.runtimeClasspath28mainClass = 'com.example.codegen.InternalAPICodeGenerator'29inputs.files(fileTree('graphql-schemas/internal-api'))30outputs.dir('build/generated/graphql/internal')31}3233tasks.register('generateAllGraphQLCode') {34dependsOn('generatePublicAPICode', 'generateAdminAPICode', 'generateInternalAPICode')35}3637tasks.named('compileJava') {38dependsOn('generateAllGraphQLCode')39}40
Gradle Plugin (Alternative)
If you prefer a Gradle plugin approach, you can create a custom plugin:
1// buildSrc/src/main/groovy/GraphQLCodeGenPlugin.groovy2import org.gradle.api.Plugin3import org.gradle.api.Project45class GraphQLCodeGenPlugin implements Plugin<Project> {6void apply(Project project) {7def extension = project.extensions.create('graphqlCodeGen', GraphQLCodeGenExtension)89project.tasks.register('generateGraphQL', JavaExec) {10group = 'code generation'11description = 'Generates GraphQL schema code'1213classpath = project.sourceSets.main.runtimeClasspath14mainClass = extension.generatorClass.get()1516inputs.files(project.fileTree(extension.schemaDir.get()))17outputs.dir(extension.outputDir.get())18}1920project.tasks.named('compileJava') {21dependsOn('generateGraphQL')22}23}24}2526class GraphQLCodeGenExtension {27final Property<String> generatorClass28final DirectoryProperty schemaDir29final DirectoryProperty outputDir3031GraphQLCodeGenExtension(ObjectFactory objects) {32generatorClass = objects.property(String)33schemaDir = objects.directoryProperty()34outputDir = objects.directoryProperty()35}36}
Then use it in your build.gradle:
1apply plugin: GraphQLCodeGenPlugin23graphqlCodeGen {4generatorClass = 'com.example.codegen.MyCodeGenerator'5schemaDir = file('src/main/resources/graphql')6outputDir = file('build/generated/graphql')7}
Build Optimization Tips
Incremental Builds
Gradle will skip code generation if:
- The schema file hasn't changed
- The output directory hasn't been deleted
- The generator class hasn't changed
Use inputs and outputs declarations to enable this:
1inputs.file('src/main/resources/schema.graphql')2inputs.file('src/main/java/com/example/codegen/MyCodeGenerator.java')3outputs.dir('build/generated/graphql')
Build Cache
Enable Gradle's build cache for faster builds across branches:
1// gradle.properties2org.gradle.caching=true
Parallel Execution
If you have multiple independent schemas, generate them in parallel:
1tasks.withType(JavaExec).configureEach {2if (name.startsWith('generate')) {3maxParallelForks = Runtime.runtime.availableProcessors()4}5}
Common Issues
Generated Code Not Found
Problem: Compiler can't find generated classes
Solution: Ensure generated directory is in sourceSets:
1sourceSets {2main {3java {4srcDir 'build/generated/graphql'5}6}7}
Circular Dependency
Problem: generateGraphQL depends on compileJava depends on generateGraphQL
Solution: Your generator class should be in a separate source set or module:
1// Option 1: Separate source set2sourceSets {3codegen {4java {5srcDir 'src/codegen/java'6}7}8}910tasks.register('generateGraphQL', JavaExec) {11classpath = sourceSets.codegen.runtimeClasspath12mainClass = 'com.example.codegen.MyCodeGenerator'13}
Code Generation Runs Too Often
Problem: Code generates every build even when schema hasn't changed
Solution: Add proper input/output declarations:
1inputs.files(fileTree('src/main/resources/graphql'))2outputs.dir('build/generated/graphql')
IDE Integration
IntelliJ IDEA
IntelliJ automatically recognizes sourceSets configuration. After running code generation:
- Right-click the project → "Reload Gradle Project"
- Or: Gradle tool window → Reload button
Eclipse
Add generated sources to .classpath:
1<classpathentry kind="src" path="build/generated/graphql"/>
VS Code
The Java extension will recognize sourceSets configuration automatically.
Best Practices
- Commit wrapper classes: Check in your
*CodeGenerator.javafiles so the build is reproducible - Don't commit generated code: Add
build/generated/to.gitignore - Clean task integration: The provided example automatically cleans generated code
- Schema validation: Consider adding a schema linting task before generation
- Documentation generation: Generate GraphQL SDL documentation alongside code
Next Steps
- Implementation Patterns - Learn how to implement the generated code
- Troubleshooting - Common issues and solutions