suku
A production GraphQL-over-HTTP server built on the gql reference implementation.
suku owns the HTTP side of GraphQL: request parsing, content negotiation, batches, GraphiQL,
server-sent-event subscriptions, body limits and authentication hooks. It deliberately does not
reimplement execution; every operation is handed to gql.
Layers
| Layer | Purpose |
|---|---|
Server |
Executes queries, mutations and subscriptions. No HTTP types. |
suku::http |
Transport-agnostic handle function, status codes, SSE, GraphiQL. |
suku::axum |
Optional (default) Axum router and authentication helper. |
Mounting on Axum
use ;
#
CORS, tracing and compression are ordinary Axum layers; compose them around the router.
Server configuration
#
Server is cheap to clone and safe to share. Wrap it in Arc only if that suits your
application; cloning is equivalent.
Error formatting and masking
Resolver errors often carry details that are useful in logs but unsafe for clients: database
messages, filesystem paths, internal type names. Server::format_error installs one formatter
that runs over every error in every response, immediately before the response is returned or
streamed:
#
The formatter receives an ErrorContext with:
| Accessor | Meaning |
|---|---|
kind() / is_request() / is_execution() |
Whether the error happened before execution or during it. |
operation_name() |
The requested operation name, or the prepared operation's name. |
request_id() / prepared_id() |
The persisted-query / prepared-query identifier, when the request carried one. |
path() |
The response path attached to the error, when it has one. |
The context never exposes the schema, resolver value, user context or other server internals.
format_error_with(Arc<dyn ErrorFormatter>) is available when the formatter is a named type or
shared with other endpoints.
Formatting is applied to single operations, every response in a batch (in request order), persisted operations, subscription setup errors and every subscription event. It also covers request errors raised by the HTTP layer, including malformed JSON, missing queries, extractor denials and content-negotiation/method rejections. Transport-level body-read failures in the Axum adapter are formatted too.
mask_internal has explicit rules:
- errors with
extensions.codeare left untouched (the code marks an intentionally public error); - request errors are left untouched (parse, validation and coercion messages describe the caller's document);
- every other error becomes a fresh
"Internal server error"with the original source locations and response path; extensions are removed, because resolvers commonly put stack traces and backend details there.
gurafi marks its own authorization and argument-validation errors with UNAUTHORIZED and
BAD_USER_INPUT, so those client-facing messages survive the mask.
A resolver that attaches an extensions.code to an internal error opts that message and its
extensions back in, so attach codes only to errors that are safe to expose. Use format_error
when the default policy is not enough.
Formatting runs synchronously and deterministically, once per error, in response order.
Formatters must be Send + Sync + 'static. A panic is not caught: it unwinds through the
execution future (or fails the subscription stream) exactly like a panic in a resolver.
Executing without HTTP
use ;
# async
GraphQLRequest converts JSON variables into gql::Value. An integer outside GraphQL's 32-bit
Int range becomes gql::Value::BigInt instead of failing the request, so a custom scalar can
receive it exactly and Float/ID can accept it. The built-in Int scalar still rejects it
during GraphQL variable coercion.
Persisted operations
Load a manifest once at startup. PreparedQueries auto-detects the JSON shapes emitted by Relay,
graphql-codegen, Apollo tooling, Hive, Envelop and urql:
use ;
#
A request can then send only an identifier, in any of these shapes:
id, doc_id, documentId and queryId are also accepted in extensions, and
extensions.persistedQuery accepts sha256Hash, hash, id, queryId or documentId.
Identifiers and hashes are opaque keys: a sha256Hash entry is not hash-verified. The
X-Persisted-Query-Id and X-Query-Id headers are a fallback used only when the request carries
no identifier in the body or extensions; for a batch they fill in every item without one. GET
requests may omit query when they send an identifier, including an extensions.persistedQuery
hash in the query string.
When an identifier matches a registered query, the registered text is executed and any supplied
query text is ignored, so a client cannot substitute a different document. The prepared operation
is fixed: a request operationName must match it, and a disagreeing name is an error rather than a
silent switch. An unknown identifier with supplied query text falls back to normal execution by
default; that fallback does not register the operation. A prepared registry is tied to the schema
instance it was prepared with, and installing it on a server for another schema is rejected at
execution. To reject arbitrary query text, make the registry an allowlist:
#
In allowlist mode a missing or unknown identifier returns a GraphQL error with message
PersistedQueryNotFound and extensions.code = "PERSISTED_QUERY_NOT_FOUND". With
Accept: application/graphql-response+json that request error uses HTTP 400, matching the
transport's normal request-error handling; legacy application/json keeps HTTP 200.
Prepared subscriptions are supported: setup reuses the parsed document and each source event reuses it too, so a persisted subscription does not re-parse per event.
PreparedQueries::from_map, from_operations and from_json_value cover programmatic and
custom manifest loading. Invalid manifests fail at startup with the offending id, duplicate id or
query error.
Transport behaviour
| Request | Result |
|---|---|
POST application/json (single object) |
200 with a GraphQL response |
POST application/json (array) |
200 with an array of responses, executed with bounded concurrency |
GET with query, variables, operationName, extensions |
200 with a GraphQL response |
GET + Accept: text/html |
GraphiQL (after authentication) |
GET + mutation/subscription |
405; GET is query-only |
Accept: application/graphql-response+json |
Request errors use 400; application/json keeps 200 |
Accept: text/event-stream + subscription |
SSE: event: next, data: …, then event: complete |
| Unsupported method | 405 with Allow: GET, POST |
Missing/unsupported Content-Type |
415 |
No acceptable Accept |
406 |
| Body over the limit | 413 |
| Malformed JSON or invalid batch | 400 with {"errors":[…]} |
The Authorization header is never read by the transport. Authentication is always an
ExtractContext closure that receives a cloneable ContextRequest with the request method, URI,
headers, cookies and HTTP extensions.
Empty batches and limits
Batches must be non-empty. Server::max_batch_size (or HttpConfig::max_batch_size) rejects
oversized arrays, and Server::batch_concurrency bounds how many operations run at once.
Observability
suku has an optional tracing feature; it also enables gql/tracing, so a request span is
followed by the parser/executor tree:
[]
= { = "0.0.1", = ["tracing"] }
suku.request is INFO and records method, path, body size, operation, request id, batch size and
status. Its children cover parsing, execution, serialization and subscriptions. Transport and
extractor failures are WARN events with a status and code. Bodies, headers, tokens and variable
values are never recorded. See docs/plugins/tracing.md for the span
tree and the recommended tracing-subscriber setup.
Tests
The transport tests call suku::http::handle directly and the Axum tests use
tower::ServiceExt::oneshot, so no sockets are opened.