Class GraphQL
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceResolves one field for many sources in one call -- what@BatchMappingbinds, and Spring's answer to the N+1 problem: the field of every object in a list is loaded together, before any of them is completed.static final classBuilds aGraphQL.static interfaceReceives what a subscription produces. -
Method Summary
Modifier and TypeMethodDescriptionExecutes a document with one operation and no variables.Executes one operation ofquery.The schema this engine executes.booleanWhether__schemaand__typecan be queried.static GraphQL.BuildernewGraphQL(GraphQLSchema schema) Starts building an engine forschema.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 itsGraphQLStream, and resolves each event against the selection set into a response forlistener.
-
Method Details
-
newGraphQL
Starts building an engine forschema. -
getSchema
The schema this engine executes. -
isIntrospectionEnabled
public boolean isIntrospectionEnabled()Whether__schemaand__typecan be queried. -
execute
-
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 asDataFetchingEnvironment.getContext()
Returns
the response, a map with
dataand, when there were errors,errors. A subscription answers an error here; seesubscribe(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 forlistener.Returns
what cancels the subscription; null when it failed at once, in which case
listenerhas haderror
-