API reference
Handler options, callback results, and HTTP behavior
Import createGraphQLHandler from @octopus-energy/graphql/handler. See Getting started to mount a route.
createGraphQLHandler
createGraphQLHandler(options) returns a GraphQLHandler, a function that takes a standard Request and returns a Promise<Response>.
CreateGraphQLHandlerOptionsProp
Type
Standard Fetch options apply to upstream requests. The handler supplies the method, body, and signal. Factory headers provide defaults, and headers returned by onRequest override them by name. The redirect option defaults to "error".
onRequest
The handler calls onRequest after validation and passes it the original request. The body is still available. If your callback needs to read it and continue forwarding, read a clone of the request. The callback can return a GraphQLOnRequestResult directly or as a promise.
export type GraphQLOnRequestResult =
GraphQLOnRequestContinue | GraphQLOnRequestReject | GraphQLOnRequestRespond;GraphQLOnRequestContinueProp
Type
Use responseHeaders to set headers on the outgoing response. The handler forwards credentials supplied in requestHeaders. The handler configuration guide shows how to load a session in onRequest.
GraphQLRequestRejectionProp
Type
Request validation
Before calling onRequest, the handler checks the HTTP method and origin. It accepts POST and returns 405 with Allow: POST for other methods. A POST request with a missing or disallowed Origin produces 403. Use onResponse to inspect or replace these responses.
The handler compares the host and port in Origin with the first x-forwarded-host value, falling back to host. Each entry in allowedOrigins matches a full origin. See Allow additional origins for a configuration example. If your reverse proxy sets X-Forwarded-Host, configure it to replace any value supplied by the caller. Configure cross-origin resource sharing (CORS) in your routing layer.
The handler streams the body to the configured upstream URL, including any query parameters in endpoint, without parsing or validating the GraphQL payload. The upstream service determines which payloads and content types it accepts.
Response headers and cookies
Use Headers.append("set-cookie", value) to set multiple cookies in responseHeaders. A response returned by respond wins for shared header names, with its cookies appended after cookies supplied in responseHeaders.
The handler sets cache-control: no-store before calling onResponse. The relay forwards GraphQL JSON media types from upstream and serves other bodies as plain text. Other upstream headers, including cookies, stay upstream.
Upstream responses
When onRequest returns continue or is omitted, the handler attempts one upstream request with the incoming abort signal. It refuses redirects by default and makes no retries.
The handler forwards the upstream status and body without parsing the response. It forwards the incoming Accept and Content-Type headers unless factory headers or onRequest supply overrides. Transport failures and refused redirects produce a GraphQL error with status 502.
Error responses
The handler sends locally generated errors as application/json with an errors field.
| Failure | Response |
|---|---|
onRequest throws | 500 |
| Upstream network failure or refused redirect | 502 |
onResponse
The handler calls onResponse({ request, response, error }) once after it builds a response, including successful responses and early rejections. The callback receives a clone, so it can inspect headers or read the body independently. Return a Response to send it as supplied. You can return the clone after changing its headers or construct a new response to change its status or body. Return nothing to keep the original. If the callback throws or rejects, the handler sends the original response.
error contains local rejection, transport failure, or upstream HTTP error details. To report GraphQL errors returned with HTTP 200, inspect the response clone in onResponse.
GraphQLHandlerResponseProp
Type
GraphQLHandlerErrorProp
Type
export type GraphQLHandlerErrorSource = "validation" | "onRequest" | "upstream";If the incoming request fails validation, cause is a GraphQLRequestRejection with the HTTP status, public message, and optional error code.
See Report handler errors for an example using Next.js.