GraphQL over HTTP
How the endpoint answers a request, which status each failure gets, and how to serve a second schema.
GraphqlModule mounts one controller at the configured path. Because it is an ordinary HTTP route, HTTP guards, interceptors and global pre-dispatch entries apply to it as to any other route.
use ulo_graphql_http::{GraphqlConfig, GraphqlModule};
GraphqlModule::for_root(GraphqlConfig::at("/graphql").playground(false).engine_from(ApiModule))GraphqlConfig method | Effect |
|---|---|
at(path) | The endpoint's path. |
engine_from(module) | The module that binds and exports dyn Engine. GraphqlModule imports it. |
playground(bool) | Whether the engine's playground page is served. Defaults to cfg!(debug_assertions). |
subscriptions(path) | Mounts the WebSocket gateway. See Subscriptions. |
connection_init_timeout(Bound) | How long a subscription connection may wait before connection_init: 3 seconds at Bound::Default. Bound::After(Duration::ZERO) is refused when the application wires. |
engine::<Q>() | Serves the engine bound as dyn Engine @ Q. |
The configuration is the module's identity: the same configuration imported twice is one module, and two paths are two modules.
Requests
A POST carries application/json with query, and optionally operationName, variables and extensions. A GET carries the same four as query parameters, variables and extensions as JSON text.
Statuses
The statuses follow the GraphQL-over-HTTP working draft, read as a shape rather than a contract: the draft is unfinished and every status in it is a SHOULD.
| Outcome | Status |
|---|---|
| The request executed, field errors included | 200 |
| The JSON body does not parse, or the document does not parse | 400 |
| The request is not a well-formed GraphQL request, fails validation, names no single operation, or carries variables that cannot be coerced | 422 |
| The server could not run the request, such as a context that failed to build | 500 |
Accept names neither supported media type | 406 |
A POST whose body is not application/json | 415 |
| A GET whose operation is a mutation | 405, with Allow |
The draft's 294 for a result with both data and errors is not used. An executed request answers 200.
A request that fails before execution carries no data key and its errors name no field path. A required variable left out, for instance, answers 422 with the variable named and its location in the document:
{
"errors": [{
"message": "Variable \"$n\" of required type \"String!\" was not provided.",
"locations": [{ "line": 1, "column": 10 }]
}]
}The 406, 415 and 405 refusals go through the error handlers as the router's own refusals do, so an error handler can reshape them.
Media type
A request with no Accept header reads as accepting application/graphql-response+json, and the response carries it. A client that accepts application/json and not application/graphql-response+json gets application/json on a 2xx answer and application/graphql-response+json on every other, as the draft has it. The status is the same under either type.
Playground
A GET with Accept: text/html and no query parameter is answered with the engine's playground page, async-graphql's GraphiQL or juniper's. playground(false) serves none. The page connects to the subscription path when one is configured.
A second schema
One GraphqlModule serves one engine. A second schema is bound under a qualifier and served by a second module at another path:
GraphqlModule::for_root(GraphqlConfig::at("/admin/graphql").engine::<Admin>().engine_from(AdminSchemaModule))AdminSchemaModule binds the second engine as dyn Engine @ Admin and exports it.
From another transport
Engine names no transport. A handler on RPC takes Dep<dyn Engine> and Payload<GqlRequest> and returns a GqlResponse. GqlRequest is Deserialize and GqlResponse is Serialize.