Server
GraphQL over HTTP
On this page
gratos-server speaks GraphQL over HTTP: a POST with a JSON body, answered as JSON, as multipart/mixed for @defer and @stream, or as Server-Sent Events for a subscription. The Accept header chooses.
The request
The body is a JSON object with query, operationName, variables and extensions, or a documentId in place of the query (a persisted document, see Persisted queries):
export interface Params {
readonly query?: string | undefined;
/**
* The id of a persisted (trusted) document, a top-level string member as in the GraphQL over
* HTTP persisted documents proposal. The core only reads it: a request that sends it with a
* `query` is a 400, and one that sends anything but a string in it is a 400. A plugin turns it
* into a query in `onParams` (`trustedDocuments()`); without one, a request with a
* `documentId` and no `query` gets the "no query" 400.
*/
readonly documentId?: string | undefined;
readonly operationName?: string | undefined;
readonly variables?: JsonObject | undefined;
readonly extensions?: JsonObject | undefined;
}As with every member of the body, null is the same as absent: { "documentId": "…", "query": null } is a documentId alone.
The HTTP matrix
The statuses below are what a JSON client gets. A client that accepts text/event-stream and no JSON type (an SSE-only client) gets every GraphQL request error (a parse, validation or variable error, an onParams throw, the drain, a refusal, a context or executor failure) as a 200 with one SSE next event holding the errors, then complete. Transport errors stay JSON with their status for every client: a 406, a 415, a 413, and a 400 for the body or a missing query. A 405 and a 499 have a null body.
| Request | Answer |
|---|---|
| GET, HEAD or any other method than POST | 405, Allow: POST, a null body |
| A body that is not JSON, not an object, or has a member of the wrong type | 400 |
A documentId that is not a string, or a documentId and a query in the same body |
400 |
No query once every onParams hook has run |
400 |
A Content-Type other than application/json |
415 |
| An Accept that allows none of the four types below | 406 |
A body over maxBodyBytes |
413, before onParams: unread when a plain-decimal Content-Length says so, else counted |
application/graphql-response+json (the default): a parse, validation or variable error (no data) |
400 |
application/graphql-response+json: a result with a data entry, null included |
200 |
application/json, any well-formed request |
200 |
gqlError(message, status) on a result whose data is absent or null |
That status (200 to 599, not 204, 205 or 304); with data, the status is left alone |
A mutation once control.drain() has begun |
503, SERVER_DRAINING |
A subscription once control.closeStreams() has been called, or its source setup still pending |
503, SERVER_SHUTTING_DOWN, JSON for every client (SSE-only included) |
| The client left while the operation was being processed | 499, a null body; a mutation whose executor has started still finishes |
A throw from onRequest, a failed async startup, or a non-GraphQL error from the context factory |
500, a masked "Unexpected error." |
@defer or @stream on a mutation |
A validation error: 400, or 200 under application/json |
| A subscription | 200, GraphQL over SSE (event: next …, then event: complete) |
What a client accepts
The four types are application/graphql-response+json, application/json, multipart/mixed and text/event-stream, each exported as a constant (GRAPHQL_RESPONSE_JSON, APPLICATION_JSON, MULTIPART_MIXED, TEXT_EVENT_STREAM). What a client must accept depends on the operation:
- A single result (a query or a mutation): a JSON type. A client that accepts no JSON type gets it as one SSE event when it accepts
text/event-stream, else as one multipart part. - A query whose
@deferor@streamdefers part of its result:multipart/mixed, in one of the two incremental formats below, else 406. A query that defers nothing (@defer(if: false)) gets a single result. - A subscription:
text/event-stream, else 406, before its source starts. - No Accept header allows every type: a single result is
application/graphql-response+json, a subscription is SSE, and an incremental result is in the 2022 format.
The incremental format
multipart/mixed carries incremental results in one of two formats, and the Accept header picks one, once, in the core:
| Format | Accept that selects it | Executor |
|---|---|---|
The 2022 format (deferSpec=20220824), the default |
multipart/mixed; deferSpec=20220824, a bare multipart/mixed, multipart/*, */*, or none |
legacyExecuteIncrementally |
graphql 17's format (incrementalSpec=v0.2) |
multipart/mixed; incrementalSpec=v0.2 |
experimentalExecuteIncrementally |
- The 2022 format is the one Apollo Client reads (Apollo Client 4 with
Defer20220824Handler): each subsequent payload hasincremental: [{ data | items, path, label }]. - graphql 17's format announces each deferred fragment or stream in
pending: [{ id, path, label }], then sendsincremental: [{ id, data | items, subPath? }]andcompleted: [{ id, errors? }]. It runs a field selected both deferred and not once, where the 2022 format runs it once per branch, and it reports a fragment or a stream that failed incompleted[].errors. This site asks for it (see @defer and @stream with Relay). - Both listed. Quality values decide, and at equal quality the 2022 format wins.
q=0refuses one format only. Only a range that namesincrementalSpec=v0.2selects graphql 17's format, and only those ranges give its quality. A range with an unknownincrementalSpecordeferSpecaccepts neither. - The Content-Type names the format sent:
multipart/mixed; boundary="-"; deferSpec=20220824ormultipart/mixed; boundary="-"; incrementalSpec=v0.2. The framing is the same in both. - Executors.
ExecuteInput.incrementalFormatis the negotiated format, orundefinedwhen the client accepts nomultipart/mixedtype. An executor of your own returns its incremental results in that format: before it sends a byte, the core checks that the first payload has the negotiated shape, and answers a 500 when it does not. - Errors.
payloadErrors(payload)andmapPayloadErrors(payload, map)read and rewrite a payload's own errors, eachincremental[].errorsand eachcompleted[].errors, somaskErrors(),reportErrors()and the Effect layer cover both formats. - Not over SSE. Incremental delivery is multipart only.
This site's fetch function offers both formats, graphql 17's first:
export const ACCEPT =
"multipart/mixed; incrementalSpec=v0.2, multipart/mixed; deferSpec=20220824; q=0.9, application/graphql-response+json, application/json";Streams
A GraphQL-over-SSE stream that has sent nothing for sseHeartbeatMs (30 s by default) gets a comment line, :, which clients ignore and which keeps a proxy that closes idle responses (Railway's, after 5 minutes) from closing it. The heartbeat follows the client's reads: the body is pulled on demand, each pull waits for the source's next event at most one interval, and a client that stops reading is sent nothing until it reads again. false turns it off. multipart/mixed gets none.
Shutdown is the drain: control.drain({ warnAfterMs }) stops accepting mutations and resolves once every mutation in flight has settled. It has no deadline, and queries and subscriptions are not drained. A drained server stays drained, so build a new one rather than reuse it.
Subscriptions are ended instead, by control.closeStreams(), so a deploy never waits for a stream. An open subscription stream ends cleanly, without event: complete, and its source is stopped; a subscription whose source setup is still pending, and every new one, gets a 503 SERVER_SHUTTING_DOWN at once. Either way the client subscribes again, and reaches the new instance. Call it before the HTTP server starts waiting for its connections: this site calls it from a finalizer registered after HttpRouter.serve, so NodeHttpServer's close then waits only for the page renders, queries and multipart responses in flight. Resolvers see the usual abort; the Effect layer records the end as RequestEnded, not as the client leaving.