Class GraphQL

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

public final class GraphQL extends Object

A GraphQL engine: a GraphQLSchema with the resolvers of its fields, executing queries, mutations and subscriptions.

The build makes one for a backend with .graphqls files under src/main/resources/graphql, binding each @QueryMapping, @MutationMapping, @SubscriptionMapping and @SchemaMapping method as a DataFetcher, and serves it at /graphql. A program can also build one by hand:

GraphQL graphQL = GraphQL.newGraphQL(GraphQLSchema.parse(sdl))
    .fetcher("Query", "book", env -> books.find((String) env.getArgument("id")))
    .build();
Map result = graphQL.execute("{ book(id: \"1\") { title } }");

Results are maps in the specification's response shape: data, and errors when there were any, each with message, locations, path and extensions.classification. A resolver failure nulls its field and is reported without stopping the rest; a null in a non-null field nulls the nearest nullable parent. Mutation fields run in order, one after another.

An exception from a resolver is reported by classification, as Spring for GraphQL does: GraphQLException with its own message, an AccessDeniedException as FORBIDDEN, an AuthenticationException as UNAUTHORIZED, a ResponseStatusException by its status (NOT_FOUND, BAD_REQUEST, ...), and anything else as INTERNAL_ERROR with no detail, logged on the server.

  • Method Details

    • newGraphQL

      public static GraphQL.Builder newGraphQL(GraphQLSchema schema)
      Starts building an engine for schema.
    • getSchema

      public GraphQLSchema getSchema()
      The schema this engine executes.
    • isIntrospectionEnabled

      public boolean isIntrospectionEnabled()
      Whether __schema and __type can be queried.
    • execute

      public Map<String,Object> execute(String query)
      Executes a document with one operation and no variables.
    • execute

      public Map<String,Object> execute(String query, String operationName, Map variables, Object context)

      Executes one operation of query.

      Parameters
      • operationName: the operation to run; may be null when the document has one

      • variables: the variables as parsed from JSON; may be null

      • context: what resolvers see as DataFetchingEnvironment.getContext()

      Returns

      the response, a map with data and, when there were errors, errors. A subscription answers an error here; see subscribe(String, String, Map, Object, GraphQL.SubscriptionListener).

    • subscribe

      public Runnable subscribe(String query, String operationName, Map variables, Object context, GraphQL.SubscriptionListener listener)

      Starts a subscription: validates the document, calls the subscription field's resolver for its GraphQLStream, and resolves each event against the selection set into a response for listener.

      Returns

      what cancels the subscription; null when it failed at once, in which case listener has had error