Java Resolvers

Java Resolvers Plugin. Generate Java interfaces for your resolvers. Works with graphql-java library.

Version
4.0.1
Weekly downloads
928
License
MIT
Updated
Apr 19, 2026

Installation

          npm i -D @graphql-codegen/java-resolvers
        

Config API Reference

package

type: string

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

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.

defaultMapper

type: string

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. Default value: “Object”

className

type: string

Allow you to customize the parent class name. Default value: “Resolvers”

listType

type: string

Allow you to customize the list type. Default value: “Iterable”

strictScalars

type: boolean

Makes scalars strict.

If scalars are found in the schema that are not defined in scalars an error will be thrown during codegen. Default value: “false”

defaultScalarType

type: string

Allows you to override the type that unknown scalars will have. Default value: “unknown”

scalars

type: object

Extends or overrides the built-in scalars and custom GraphQL scalars to a custom type.

namingConvention

type: object

Allow you to override the naming convention of the output. You can either override all namings, or specify an object with specific custom naming convention per output. The format of the converter must be a valid module#method. Allowed values for specific output are: typeNames, enumValues. You can also use “keep” to keep all GraphQL names as-is. Additionally, you can set transformUnderscore to true if you want to override the default behavior, which is to preserve underscores.

Available case functions in change-case-all are camelCase, capitalCase, constantCase, dotCase, headerCase, noCase, paramCase, pascalCase, pathCase, sentenceCase, snakeCase, lowerCase, localeLowerCase, lowerCaseFirst, spongeCase, titleCase, upperCase, localeUpperCase and upperCaseFirst See more Default value: “change-case-all#pascalCase”

typesPrefix

type: string

Prefixes all the generated types. Default value: ""

typesSuffix

type: string

Suffixes all the generated types. Default value: ""

enumPrefix

type: boolean

Allow you to disable prefixing for generated enums, works in combination with typesPrefix. Default value: “true”

enumSuffix

type: boolean

Allow you to disable suffixing for generated enums, works in combination with typesSuffix. Default value: “true”

useTypeImports

type: boolean

Will use import type {} rather than import {} when importing only types. This gives compatibility with TypeScript’s “importsNotUsedAsValues”: “error” option Default value: “false”

inlineFragmentTypes

type: string

Whether fragment types should be inlined into other operations. “inline” is the default behavior and will perform deep inlining fragment types within operation type definitions. “combine” is the previous behavior that uses fragment type references without inlining the types (and might cause issues with deeply nested fragment that uses list types). “mask” transforms the types for use with fragment masking. Useful when masked types are needed when not using the “client” preset e.g. such as combining it with Apollo Client’s data masking feature. Default value: “inline”

emitLegacyCommonJSImports

type: boolean

Emit legacy common js imports. Default it will be true this way it ensure that generated code works with non-compliant bundlers. Default value: “true”

importExtension

type: object

Append this extension to all imports. Useful for ESM environments that require file extensions in import statements.

printFieldsOnNewLines

type: boolean

If you prefer to have each field in generated types printed on a new line, set this to true. This can be useful for improving readability of the resulting types, without resorting to running tools like Prettier on the output. Default value: “false”

includeExternalFragments

type: boolean

Whether to include external fragments in the generated code. External fragments are not defined in the same location as the operation definition. Default value: “false”

The java-resolvers plugin creates Java interfaces for the resolvers’ signature.

It works with graphql-java library, and it uses it’s DataFetcher API.

You can use this plugin to generate interfaces and later implement them, this way you can always tell if one of the fields is missing a resolvers:

import com.my.app.generated.Resolvers;
import com.my.app.models.User;
import graphql.schema.DataFetcher;

export class QueryResolvers implements Resolvers.Query {
  public DataFetcher<String> id() {
    return environment -> environment.<User>getSource().getId();
  }
}

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:

{
  "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"
  }
}

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

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:

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.