PluginsJavajava

Java

Package nameWeekly DownloadsVersionLicenseUpdated
@graphql-codegen/javaDownloadsVersionLicenseApr 19th, 2026

Installation

npm i -D @graphql-codegen/java

Config API Reference

package

type: string

Customize the Java package name. The default package name will be generated according to the output file path.

Usage Examples

generates:
  src/main/java/my-org/my-app/Resolvers.java:
    plugins:
      - java-resolvers
    config:
      package: custom.package.name

mappers

type: object

Allow you to replace specific GraphQL types with your custom model classes. This is useful when you want to make sure your resolvers returns the correct class. The default value is the values set by defaultMapper configuration. You can use a direct path to the package, or use package#class syntax to have it imported.

Usage Examples

generates:
  src/main/java/my-org/my-app/Resolvers.java:
    plugins:
      - java-resolvers
    config:
      mappers:
        User: com.app.models#UserObject

defaultMapper

type: string default: Object

Sets the default mapper value in case it’s not specified by mappers. You can use a direct path to the package, or use package#class syntax to have it imported. The default mapper is Java’s Object.

Usage Examples

generates:
  src/main/java/my-org/my-app/Resolvers.java:
    plugins:
      - java-resolvers
    config:
      defaultMapper: my.app.models.BaseEntity

className

type: string default: Resolvers

Allow you to customize the parent class name.

Usage Examples

generates:
  src/main/java/my-org/my-app/Resolvers.java:
    plugins:
      - java-resolvers
    config:
      className: MyResolvers

listType

type: string default: Iterable

Allow you to customize the list type.

Usage Examples

generates:
  src/main/java/my-org/my-app/Resolvers.java:
    plugins:
      - java-resolvers
    config:
      listType: Map

Prepare your environment

To use the GraphQL Code Generator with Java, start by adding the com.moowork.node Gradle plugin to your build.gradle:

plugins {
  id "com.moowork.node" version "1.3.1"
}

Then, add the following in order to make sure you are running the code-generator on each build:

build.dependsOn yarn

Then, create a package.json file in your project root, with the following content:

package.json
{
  "name": "java-app",
  "scripts": {
    "postinstall": "graphql-codegen"
  },
  "dependencies": {
    "graphql": "14.5.8",
    "@graphql-codegen/cli": "1.7.0",
    "@graphql-codegen/RELEVANT_PLUGIN": "1.7.0"
  }
}
💡

Make sure to use the latest version of codegen and the plugins, and replace RELEVANT_PLUGIN with your plugin name.

Then, create codegen.yml file in your root directory, pointing to your schema, and add the plugins you need. For example:

codegen.yml
schema: src/main/resources/schema.graphqls
generates:
  src/main/java/com/my-name/my-app/generated/File.java:
    - RELEVANT_PLUGIN # Replace with your plugin name

Also, make sure you add the following to your .gitignore file:

.gitignore
yarn.lock
node_modules

Now, run gradle yarn to install the dependencies for the first time.

Next time, the codegen will run automatically each time you run your Gradle build script.

Prepare your environment

You can use it directly to transform your input in your resolvers:

type Query {
  user(id: ID!): User!
}
 
type User {
  id: ID
}

Then, in your resolver:

import com.my.app.generated.Types;
import com.my.app.models.User;
import graphql.schema.DataFetcher;
 
export class QueryResolvers {
  public DataFetcher<User> user() {
    return env -> {
      Types.QueryUserArgs args = new Types.QueryUserArgs(env.getArguments());
      String userId = args.getId();
 
      // rest of the code
    };
  }
}