# Class GraphQL

java.lang.Object
com.codename1.io.graphql.GraphQL

public final class GraphQL extends java.lang.Object

High-level GraphQL-over-HTTP invoker used by generated `@GraphQLClient` implementations. Queries and mutations are sent as an HTTP `POST` with a JSON body `{"query":...,"operationName":...,"variables":{...}}` and a `Content-Type: application/json` header; the JSON response envelope (`{"data":...,"errors":[...]}`) is parsed into a typed `GraphQLResponse`. Subscriptions are delegated to `GraphQLSubscription` over a WebSocket.

Mirrors `GrpcWeb`. All methods are static; generated impls call `execute(String, String, String, String, Map, Class, OnComplete)` / `subscribe(String, String, String, String, Map, Class, GraphQLSubscription.Handler)` and never touch `ConnectionRequest` directly.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final String`
  `CONTENT_TYPE`
  Content type for GraphQL-over-HTTP requests.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static String`
  `buildRequestBody(String operationName, String document, Map<String, java.lang.Object> variables)`
  Builds the JSON request body.
  `static <T> GraphQLResponse<T>`
  `decodeJson(byte[] body, int httpCode, java.lang.Class<T> dataType)`
  Decodes a GraphQL JSON response envelope into a typed `GraphQLResponse`.
  `static String`
  `encodeVariables(Map<String, java.lang.Object> variables)`
  Serialises a variable-name -> value map to a JSON object string.
  `static <T> void`
  `execute(String endpoint, String bearerToken, String operationName, String document, Map<String, java.lang.Object> variables, java.lang.Class<T> dataType, OnComplete<GraphQLResponse<T>> callback)`
  Sends a unary query or mutation and invokes `callback` with the decoded `GraphQLResponse`.
  `static <T> GraphQLSubscription`
  `subscribe(String endpoint, String bearerToken, String operationName, String document, Map<String, java.lang.Object> variables, java.lang.Class<T> dataType, GraphQLSubscription.Handler<T> handler)`
  Opens a GraphQL subscription over a WebSocket and streams mapped `data` payloads to `handler`.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### CONTENT_TYPE

    public static final String CONTENT_TYPE
    Content type for GraphQL-over-HTTP requests.
    See Also:
      - Constant Field Values

- ## Method Details

  - ### execute

    public static <T> void execute(String endpoint, String bearerToken, String operationName, String document, Map<String, java.lang.Object> variables, java.lang.Class<T> dataType, OnComplete<GraphQLResponse<T>> callback)
    Sends a unary query or mutation and invokes `callback` with the decoded `GraphQLResponse`. `variables` may be null or empty. The `operationName` may be null when the document declares a single operation.

  - ### subscribe

    public static <T> GraphQLSubscription subscribe(String endpoint, String bearerToken, String operationName, String document, Map<String, java.lang.Object> variables, java.lang.Class<T> dataType, GraphQLSubscription.Handler<T> handler)

    Opens a GraphQL subscription over a WebSocket and streams mapped `data` payloads to `handler`. Returns a handle whose `GraphQLSubscription.cancel()` tears the subscription down.

    The `endpoint` is the GraphQL HTTP endpoint; its scheme is rewritten to `ws`/`wss` for the WebSocket connection. Pass a `ws://`/`wss://` URL directly to override that heuristic.

  - ### buildRequestBody

    public static String buildRequestBody(String operationName, String document, Map<String, java.lang.Object> variables)
    Builds the JSON request body. Public so tests can verify it without the network path.

  - ### encodeVariables

    public static String encodeVariables(Map<String, java.lang.Object> variables)
    Serialises a variable-name -> value map to a JSON object string. Strings, numbers, booleans, `List`s, `Map`s and null pass through the framework JSON writer; enums serialise as their `name()`; any other object is assumed to be `@Mapped` and is converted via `Mappers.toJson(Object)` and spliced in verbatim. Public so generated impls and tests can reuse it.

  - ### decodeJson

    public static <T> GraphQLResponse<T> decodeJson(byte[] body, int httpCode, java.lang.Class<T> dataType)
    Decodes a GraphQL JSON response envelope into a typed `GraphQLResponse`. Public and side-effect free so unit tests can replay canned bodies without a network round-trip.