--- title: "Java GraphQL Client Generator (No Generics)" description: "Generated Java GraphQL client with no generics or casting: builder inputs, injectable HttpClient/OkHttp, optional Reactor, RxJava3 and Mutiny modes." --- # Java Client Type-safe. No generics. No casting. Works with any JSON library. ## Generated adapters GraphLink generates concrete adapter classes into your `client/` folder — no external GraphLink runtime, no boilerplate. Two config options control what is generated: === "JSON" ```json title="glink.json — adapter options" { "clientConfig": { "java": { "packageName": "com.example.generated", "wsAdapter": "java11", "jsonCodec": "jackson" } } } ``` === "YAML" ```yaml title="glink.yaml — adapter options" clientConfig: java: packageName: com.example.generated wsAdapter: java11 jsonCodec: jackson ``` **`wsAdapter`** — controls which WebSocket adapter is generated: - `"java11"` (default) — generates `DefaultGraphLinkWebSocketAdapter` using Java 11's built-in `java.net.http.WebSocket`. Zero external dependencies. Supports exponential-backoff auto-reconnect, an optional `Supplier>` for auth headers, and automatically forwards those headers as the `connection_init` payload so your server can authenticate the WebSocket handshake. - `"okhttp"` — generates the same interface implemented with OkHttp's WebSocket client instead. - `"none"` — no WebSocket adapter is generated. Use this if you do not need subscriptions. **`jsonCodec`** — controls which JSON codec is generated: - `"jackson"` (default) — generates `JacksonGraphLinkJsonCodec` implementing both `GraphLinkJsonEncoder` and `GraphLinkJsonDecoder`. - `"gson"` — generates `GsonGraphLinkJsonCodec` instead. - `"none"` — no codec class is generated; supply your own lambdas. `DefaultGraphLinkClientAdapter` is always generated (when `wsAdapter` is not `"none"`). It uses Java 11's `HttpClient` for HTTP requests and accepts the same optional headers provider as the WebSocket adapter. ### Injectable HTTP client Both `DefaultGraphLinkClientAdapter` and `DefaultGraphLinkWebSocketAdapter` accept a pre-configured `HttpClient` (Java 11) or `OkHttpClient` as an optional constructor argument: === "Java 11 HttpClient" ```java HttpClient httpClient = HttpClient.newBuilder() .connectTimeout(Duration.ofSeconds(10)) .sslContext(mySslContext) .proxy(ProxySelector.of(new InetSocketAddress("proxy.corp", 8080))) .build(); // Pass it to the adapter — all HTTP and WebSocket traffic uses this client DefaultGraphLinkClientAdapter adapter = new DefaultGraphLinkClientAdapter( "http://api.example.com/graphql", null, decoder, httpClient ); DefaultGraphLinkWebSocketAdapter wsAdapter = new DefaultGraphLinkWebSocketAdapter( "ws://api.example.com/graphql", httpClient ); ``` === "OkHttpClient" ```java OkHttpClient okHttpClient = new OkHttpClient.Builder() .connectTimeout(10, TimeUnit.SECONDS) .addInterceptor(loggingInterceptor) .build(); DefaultGraphLinkClientAdapter adapter = new DefaultGraphLinkClientAdapter( "http://api.example.com/graphql", null, decoder, okHttpClient ); DefaultGraphLinkWebSocketAdapter wsAdapter = new DefaultGraphLinkWebSocketAdapter( "ws://api.example.com/graphql", okHttpClient ); ``` Injecting a client is optional: `(url, decoder)` and `(url, headersProvider, decoder)` use a default one. ## Initializing the client The generated `GraphLinkClient` ships with several constructors that progressively add control: === "one-liner" ```java import com.example.generated.client.GraphLinkClient; // Simplest setup — Jackson + Java 11 HttpClient + auto-derives wsUrl. // wsUrl is derived by replacing "http" with "ws" in the provided URL. GraphLinkClient client = new GraphLinkClient("http://localhost:8080/graphql"); ``` === "with auth headers" ```java // Dynamic auth headers on every request — token is fetched fresh each time. Supplier> headers = () -> Map.of( "Authorization", "Bearer " + tokenService.getToken() ); // wsUrl auto-derived; both HTTP and WebSocket adapters receive the headers. GraphLinkClient client = new GraphLinkClient( "http://localhost:8080/graphql", headers, new JacksonGraphLinkJsonCodec(), new JacksonGraphLinkJsonCodec() ); ``` === "custom codec" ```java // Use a different JSON library — e.g. Gson. GraphLinkJsonEncoder encoder = obj -> gson.toJson(obj); GraphLinkJsonDecoder decoder = json -> gson.fromJson(json, Map.class); GraphLinkClient client = new GraphLinkClient( "http://localhost:8080/graphql", "ws://localhost:8080/graphql", encoder, decoder ); ``` === "full control" ```java // Full manual wiring — bring your own adapter, encoder, decoder, and cache store. GraphLinkClientAdapter adapter = payload -> { /* custom HTTP logic */ }; GraphLinkJsonEncoder encoder = obj -> mapper.writeValueAsString(obj); GraphLinkJsonDecoder decoder = json -> mapper.readValue(json, Map.class); GraphLinkWebSocketAdapter wsAdapter = new DefaultGraphLinkWebSocketAdapter( "ws://localhost:8080/graphql" ); GraphLinkClient client = new GraphLinkClient( adapter, encoder, decoder, myCacheStore, wsAdapter ); ``` !!! info "Any JSON library works" The three generated interfaces (`GraphLinkClientAdapter`, `GraphLinkJsonEncoder`, `GraphLinkJsonDecoder`) are all `@FunctionalInterface` — assign them from lambdas using Gson, Moshi, or any other library. Jackson is only the default for the convenience constructors. Pass a custom `GraphLinkCacheStore` in the full constructor if you need a persistent or shared cache (e.g. Redis-backed). See the [Caching](caching.md) page for details. ## Queries — no generics This is the core difference from every other Java GraphQL client. There are no `TypeReference` anonymous classes, no unchecked casts, no raw `Map` navigation: ```java title="Fetching a vehicle — GraphLink style" // Clean, typed, no generics GetVehicleResponse res = client.queries.getVehicle("42"); System.out.println(res.getGetVehicle().getBrand()); // Toyota System.out.println(res.getGetVehicle().getYear()); // 2023 System.out.println(res.getGetVehicle().getFuelType()); // GASOLINE ``` Compare this to the boilerplate required by most other clients: ```java title="The same query — typical other client" // What you're forced to write with most Java GraphQL clients GraphQLResponse> response = client.query(new SimpleGraphQLRequest<>( "query getVehicle($id: ID!) { getVehicle(id: $id) { id brand model year fuelType } }", Map.of("id", "42"), new TypeReference>>() {} )); @SuppressWarnings("unchecked") Map vehicleMap = (Map) response.getData().get("getVehicle"); String brand = (String) vehicleMap.get("brand"); Integer year = ((Number) vehicleMap.get("year")).intValue(); ``` ## Nullable queries When the schema declares a nullable return type (no `!`), the corresponding getter on the response class returns a nullable type: ```java title="getPerson — nullable result" // Schema: getPerson(id: ID!): Person <-- nullable return GetPersonResponse res = client.queries.getPerson("99"); Person p = res.getGetPerson(); // can be null — check before use if (p != null) { System.out.println(p.getName()); System.out.println(p.getEmail()); } else { System.out.println("Person not found"); } ``` ## Mutations — builder pattern All input types are generated with an inner `Builder` class. Required fields (non-nullable in the schema) are validated with `Objects.requireNonNull` when `build()` is called: ```java title="Adding a vehicle" import com.example.generated.inputs.AddVehicleInput; import com.example.generated.enums.FuelType; AddVehicleResponse added = client.mutations.addVehicle( AddVehicleInput.builder() .brand("Toyota") .model("Camry") .year(2023) .fuelType(FuelType.GASOLINE) // ownerId is nullable — omit for null .build() ); System.out.println(added.getAddVehicle().getId()); // server-assigned ID System.out.println(added.getAddVehicle().getBrand()); // Toyota ``` The generated `AddVehicleInput` class: ```java title="generated/inputs/AddVehicleInput.java" public class AddVehicleInput { private final String brand; private final String model; private final Integer year; private final FuelType fuelType; private final String ownerId; public AddVehicleInput(String brand, String model, Integer year, FuelType fuelType, String ownerId) { Objects.requireNonNull(brand); Objects.requireNonNull(model); Objects.requireNonNull(year); Objects.requireNonNull(fuelType); this.brand = brand; this.model = model; this.year = year; this.fuelType = fuelType; this.ownerId = ownerId; } public static Builder builder() { return new Builder(); } public static class Builder { private String brand; private String model; private Integer year; private FuelType fuelType; private String ownerId; public Builder brand(String brand) { this.brand = brand; return this; } public Builder model(String model) { this.model = model; return this; } public Builder year(Integer year) { this.year = year; return this; } public Builder fuelType(FuelType fuelType) { this.fuelType = fuelType; return this; } public Builder ownerId(String ownerId) { this.ownerId = ownerId; return this; } public AddVehicleInput build() { return new AddVehicleInput(brand, model, year, fuelType, ownerId); } } } ``` ## Lists List queries return a typed `List` — no casting required: ```java title="List query" ListVehiclesResponse res = client.queries.listVehicles(); List vehicles = res.getListVehicles(); // List — no raw types for (Vehicle v : vehicles) { System.out.printf("%s %s (%d) — %s%n", v.getBrand(), v.getModel(), v.getYear(), v.getFuelType()); } // Or with streams vehicles.stream() .filter(v -> v.getFuelType() == FuelType.ELECTRIC) .map(Vehicle::getBrand) .forEach(System.out::println); ``` ## The response wrapper pattern Every query, mutation, and subscription operation generates a dedicated response class named `{OperationName}Response`. For example, `getVehicle` generates `GetVehicleResponse`. This pattern mirrors the GraphQL JSON response structure, which always wraps results in a `data` field: ```json title="GraphQL HTTP response JSON" { "data": { "getVehicle": { "id": "42", "brand": "Toyota", "model": "Camry", "year": 2023, "fuelType": "GASOLINE", "ownerId": null } } } ``` The generated `GetVehicleResponse.fromJson()` navigates into the `data` object and deserializes `getVehicle` as a `Vehicle`. From your code's perspective, you simply call `res.getGetVehicle()` — the JSON unwrapping is invisible. Notice the double "get" in `getGetVehicle()` — the first is the Java getter prefix, the second is the operation name. This is consistent and predictable: the method name is always `get` + the operation name with a capital first letter. !!! info "toJson/fromJson generation is mandatory (v5.0.0+)" Every generated type, input, enum, and interface always gets `toJson`/`fromJson` — there is no config flag to suppress it. This matters because [identifier normalization](configuration.md#identifier-normalization) can make a field's generated Java name diverge from its wire name (e.g. a keyword-colliding or non-canonically-cased GraphQL field): code that calls generated getters is always working with the normalized name, and only `toJson`/`fromJson` know how to bridge back to the wire format. ## Subscriptions Subscriptions are available via `client.subscriptions` and use the `GraphLinkWebSocketAdapter` interface. The generated `DefaultGraphLinkWebSocketAdapter` implements this interface out of the box: ```java title="Subscribing to new vehicles" client.subscriptions.vehicleAdded(event -> { VehicleAddedResponse res = event; System.out.println("New vehicle: " + res.getVehicleAdded().getBrand()); }); ``` The generated WebSocket adapter handles the [graphql-ws subprotocol](https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md) automatically — connection init, ping/pong, and exponential-backoff reconnect on disconnect. !!! info "Deriving the WebSocket URL" The convenience constructors that take only an HTTP URL automatically derive the WebSocket URL by replacing `http` with `ws` (and `https` with `wss`). Pass an explicit `wsUrl` if your WebSocket endpoint differs from the HTTP endpoint. ## Operation name in URL Set `"operationNameAsParameter": true` to append the operation name as a URL query parameter on every request — useful for server-side logging and APM tracing: === "JSON" ```json title="glink.json" { "clientConfig": { "java": { "operationNameAsParameter": true } } } ``` === "YAML" ```yaml title="glink.yaml" clientConfig: java: operationNameAsParameter: true ``` The generated adapter interface gains an `operationName` parameter and the client appends `?operationName=OperationName` to each HTTP request URL. ## Nullable fields By default, nullable schema fields accept `null` without any runtime enforcement. Set `"nullableFieldsRequired": true` to emit `Objects.requireNonNull` on every field, including nullable ones: === "JSON" ```json title="glink.json" { "clientConfig": { "java": { "nullableFieldsRequired": true } } } ``` === "YAML" ```yaml title="glink.yaml" clientConfig: java: nullableFieldsRequired: true ``` ## Java records Set `"inputAsRecord": true` and/or `"typeAsRecord": true` to generate inputs and types as Java records instead of classes with builders. Records use component accessor syntax (`field()`) instead of `getField()`: === "JSON" ```json title="glink.json" { "clientConfig": { "java": { "inputAsRecord": true, "typeAsRecord": true } } } ``` === "YAML" ```yaml title="glink.yaml" clientConfig: java: inputAsRecord: true typeAsRecord: true ``` ```java title="Generated input as record" public record AddVehicleInput( String brand, String model, Integer year, FuelType fuelType, String ownerId ) {} // Usage — no builder needed AddVehicleInput input = new AddVehicleInput("Toyota", "Camry", 2023, FuelType.GASOLINE, null); System.out.println(input.brand()); // component accessor, no get prefix ``` Records require Java 16 or later. Mapping methods generated by `@glMapsTo` use component accessors automatically when `typeAsRecord` is `true`. ## Reactive client (v5.0.0+) By default the Java client is blocking — every query/mutation method returns its result directly and subscriptions notify via a listener callback (see above). Set `"asyncStyle"` in `clientConfig.java` to generate a reactive client instead: === "JSON" ```json title="glink.json — reactive client" { "clientConfig": { "java": { "asyncStyle": "reactor", "reactiveHttpClient": "jdk" } } } ``` === "YAML" ```yaml title="glink.yaml — reactive client" clientConfig: java: asyncStyle: reactor reactiveHttpClient: jdk ``` | `asyncStyle` | Query/mutation return type | Subscription return type | Library | |---|---|---|---| | `"blocking"` (default) | `T` | listener callback | — | | `"reactor"` | `Mono` | `Flux` | Project Reactor | | `"rxjava3"` | `Single` | `Observable` | RxJava 3 | | `"mutiny"` | `Uni` | `Multi` | SmallRye Mutiny | ```java title="Reactive query — asyncStyle: reactor" Mono res = client.queries.getVehicle("42"); res.subscribe(r -> System.out.println(r.getGetVehicle().getBrand())); ``` `reactiveHttpClient` selects the default adapter's transport: - `"jdk"` (default) — uses `HttpClient.sendAsync`. Works with every `asyncStyle`, no extra dependency beyond the reactive library itself. - `"webclient"` — uses Spring `WebClient`. Only compatible with `asyncStyle: "reactor"`. File upload support is available for the Java client regardless of `asyncStyle` — see below. ## Custom scalar codecs A scalar can map to a Java type with `fromJson` and `toJson` expressions, where `{}` stands for the value: ```json "typeMappings": { "Opaque": { "type": "String", "jsonType": "String", "fromJson": "\"opaque:\" + {}", "toJson": "{}.substring(7)" } } ``` `jsonType` names the JSON type the server sends: `String`, `Int`, `Double`, `Bool`, `Map` or `List`. It is different from `type`, which is the Java type your code uses. With `jsonType`, the generated decoder casts the value to `String`, `Boolean`, `Map` or `List`; `Int` becomes a `long` converted exactly (a fractional or out-of-range number fails) and `Double` a `double`. The expression needs no cast. A value of another type makes the decode fail instead of reaching your expression; on a nullable field `null` stays `null`. Without `jsonType`, `{}` is an `Object` and a hard cast such as `(String) {}` is yours to write. A `null` for a non-null `jsonType` fails with a `NullPointerException`, a wrong type with a `ClassCastException`. `jsonType` only checks the value that arrives; an expression that can fail on a valid wire value still has to handle that itself. A codec with no `fromJson` is a plain type alias and ignores `jsonType`. !!! danger "Breaking change in v6.0.0: `DefaultGraphLinkClientAdapter` takes a decoder" The adapter now needs a `GraphLinkJsonDecoder`, like `DefaultGraphLinkGetAdapter` already did, so it can tell a GraphQL response from a gateway page. The constructors are `new DefaultGraphLinkClientAdapter(url, decoder)`, `(url, headersProvider, decoder)` and `(url, headersProvider, decoder, httpClient)`. The `GraphLinkClient` factory methods are unchanged. ## Decoding and HTTP errors - A GraphQL call whose response has no `data` and no errors, or whose HTTP status is not 2xx and carries no GraphQL body, throws a `GraphLinkException` (it used to return `null` or throw a cast error). `GraphLinkError` has two new nullable fields, `getStatus()` and `getBody()`. A response with `"errors": []` is a success. - Decoding an `int`, `long`, `Integer` or `Long` field is exact. A value outside the type's range (`99999999999999999999999`) or with a fraction (`19.5`) throws an `ArithmeticException`; `19.0` and `1e3` are accepted. These used to wrap or truncate silently. To get another type, declare a [scalar codec](#custom-scalar-codecs). - A leading UTF-8 byte order mark is ignored. - The WebClient adapters wrap the `WebClient` (yours too) with a generated `GLWebClientContentType` filter that drops a `Content-Type` charset Spring cannot parse, so `application/json; charset=nonsense` decodes as UTF-8. ## File uploads When your schema uses the built-in `Upload` scalar, GraphLink generates a `GLUpload` class and handles multipart encoding in the generated adapter: ```java title="Using GLUpload in Java" import com.example.generated.types.GLUpload; import java.io.FileInputStream; import java.io.File; File file = new File("/path/to/document.pdf"); GLUpload upload = new GLUpload( new FileInputStream(file), file.length(), "document.pdf", "application/pdf" ); UploadDocumentResponse result = client.mutations.uploadDocument(upload); System.out.println(result.getUploadDocument()); // URL or ID returned by server ``` The generated `DefaultGraphLinkClientAdapter` automatically encodes mutations containing `GLUpload` as multipart/form-data following the [GraphQL multipart request spec](https://github.com/jaydenseric/graphql-multipart-request-spec). No extra configuration needed. ## Persisted queries Set `persistedQueries: "apq"` under `clientConfig.java` to send long operations as a SHA-256 hash and retry once with the query text on `PersistedQueryNotFound`. `persistedQueriesUseGet: true` sends hash-only queries over GET. Both are off by default. See [Persisted queries](persisted-queries.md). ## REST operations Fields of `Query` and `Mutation` annotated with `@glRest` are served over HTTP/JSON through this same client, with the same call syntax as GraphQL operations. See [REST operations](rest-client.md#java) for the directive, argument roles, responses, caching and the adapter.