Skip to content

GraphQL to Java Classes Transformer — Numio

Generates Java classes for the enums and input types in a GraphQL schema using @graphql-codegen/java. Everything is nested inside a public class Types; object types, interfaces and queries are not converted.

Creating Java input classes for mutation arguments in a graphql-java server.

GraphQL Schema

Java (Enum and Input)

When to use it

  • Creating Java input classes for mutation arguments in a graphql-java server.
  • Getting Java enums that mirror GraphQL enum values exactly.

How to use

  1. Paste a schema with enum and input definitions into the GraphQL Schema editor.
  2. Review the generated Types class: one public enum per GraphQL enum and one public static class per input type.
  3. Copy it into a Types.java file and change package com.example.graphql to your own package.

GraphQL schema

enum Role {
  ADMIN
  EDITOR
}

input CreateUserInput {
  name: String!
  email: String
  role: Role = EDITOR
  tags: [String!]
}

type Query {
  ping: String
}

Java enums and input classes

package com.example.graphql;

import java.util.HashMap;
import java.util.Map;

public class Types {
  public enum Role {
    ADMIN,
    EDITOR
    
  }
  
  public static class CreateUserInput {
    private String name;
    private String email;
    private Role role;
    private Iterable<String> tags;
  
    public CreateUserInput(Map<String, Object> args) {
      if (args != null) {
        this.name = (String) args.get("name");
        this.email = (String) args.get("email");
        if (args.get("role") instanceof Role) {
          this.role = (Role) args.get("role");
…

Details and limitations

The package line defaults to com.example.graphql, because the page has no package setting. Change it to your project's package.

Each input class has a constructor that reads a Map<String, Object> of arguments, plus getters and setters. Default values from the schema (role: Role = EDITOR) are not applied.

Type mapping: String to String, Int to Integer, Float to Double, Boolean to Boolean, ID to Object, lists to Iterable<T>. Input names not ending in Input get the suffix (A becomes AInput).

FAQ

Why are my object types missing from the Java output?

The java plugin only generates enums and input types. For data-fetcher interfaces per object type, use the JAVA Resolvers Signature option on the GraphQL to Resolvers Signature page.

How are nested input types handled?

A field whose type is another input becomes that class, and the constructor builds it from the nested map, for example new BInput((Map<String, Object>) args.get("nested")).

Does it run on a server?

No. The code generator runs in a Web Worker in your browser.

Specifications and references