Server
Plugins
On this page
Everything beyond plain GraphQL over HTTP is a plugin: @defer and @stream, persisted queries, error masking, graphql-jit, limits. Each one is a few hooks (see The server core), and the list is ordered.
The shipped plugins
Every plugin comes from gratos-server, except jit(), which is the gratos-server/jit entry: the main entry never loads graphql-jit, an optional peer that only an app using jit() installs.
| Plugin | What it does |
|---|---|
apq({ store, max }) |
Apollo's automatic persisted queries. Stores a query only once it has passed validation. Never claims an operation |
incremental() |
Adds @defer and @stream to the schema and claims the queries that use them, in the negotiated format; rejects them on mutations |
jit({ after, isSafe }) |
graphql-jit from the after-th use (default 3). Declines subscriptions, @defer/@stream, @oneOf variables and jit: false fields |
reportErrors(log) |
Hands every error of every payload to log, skipping cancelled ones |
maskErrors({ message }) |
Replaces every unexpected error's message (default "Unexpected error."), and turns on hideSuggestions |
serverTiming() |
A Server-Timing header with each phase and the total |
maxDepth(limit) |
Refuses an operation nested deeper than limit (400, MAX_DEPTH_EXCEEDED) |
readOnly() |
Refuses every mutation (403, READ_ONLY) |
trustedDocuments({ documents }) |
Runs only the documents of a map of id to text (relay-compiler's persisted queries); refuses a text query and an unknown documentId |
Their order
apq()first, so it sees the query before any executor. A server that takes trusted documents only has no use for it.incremental()beforejit(): graphql-jit inlines@defer.reportErrors()beforemaskErrors(), so the report sees the unmasked errors.- Plugins that only observe or refuse (
serverTiming(),maxDepth(),readOnly(), the Effect layer'seffect()) go anywhere: everyonExecutehook runs before any executor.
This site's list: incremental delivery, trusted documents only, the Effect layer, then the errors reported and masked.
plugins: [
incremental(),
trustedDocuments({ documents: options.documents }),
// `context`: resolvers find the app's services also when the server is used without a
// route (makeServer(...).handle(request) in a test); a route forks with its own context.
effect({ context: services }),
reportErrors((error) => {
report(error);
}),
maskErrors(),
],Trusted documents
trustedDocuments({ documents }) makes the server run only the documents your client's compiler produced. documents is a map of id to document text, the shape relay-compiler's persist step writes:
- In
onReadyit validates every entry against the schema the server is about to serve, with the server's rules, so a stale map refuses the schema:createServer()throws (or the async startup fails), and areplaceSchemathat an entry fails against rethrows and keeps the current schema. - A request whose
documentIdis in the map runs that document, over JSON,multipart/mixedand SSE alike. - A request whose
documentIdis not in the map gets a 400 withextensions.code: "PERSISTED_DOCUMENT_NOT_FOUND"(what a page built before a deploy sees). A request that sends its query as text gets a 400 withPERSISTED_DOCUMENT_REQUIRED. Both come fromonParams, so no executor runs. - Its
onExecutealso refuses any document that is not one of the map's texts, so a lateronParamshook cannot swap one in, whatever the order.
It has no per-route exception: a route that takes text queries (a playground) is a second createServer without it. How this site builds its map is in Persisted queries.
graphql-jit
jit({ after, isSafe }) compiles an operation with graphql-jit once it has been run after times, and runs the compiled query from then on. It declines what graphql-jit does not support (subscriptions, @defer and @stream, @oneOf variables) and any operation that selects a field Gratos marked jit: false (a resolver that takes GqlInfo). graphql-jit reads a field without a resolver as a plain property, where graphql-js calls a function-valued property; Gratos gives every method field its own resolver, so its schemas are safe, and a hand-written schema with function-valued fields keeps them on graphql-js with isSafe.
Errors
maskErrors() replaces the message of every unexpected error, one whose original error is not a GraphQLError (a plain Error a resolver threw, say), with "Unexpected error.". It keeps the error's path and locations and drops its extensions. An error raised on purpose, a GraphQLError such as one from gqlError, passes unchanged. It also turns on hideSuggestions, so validation errors do not suggest the schema's field names. reportErrors(log) hands each unmasked error to log first, skipping the cancellations the Effect layer reports when a client leaves. With the Effect layer, the typed failures your failure mapper turns into gqlErrors reach the client as they are, and every other failure is masked.