Deploying GraphQL

Intermediate · 2 min

A GraphQL server is a fetch handler that runs on Node or the edge. Configure by environment, add health checks, and roll out schema changes safely.

Before thisA server with GraphQL YogaDisabling introspection and hardeningObservability and tracing

Jamie Barton · 2 min

Deploying GraphQL

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Jamie BartonReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • GraphQL Yoga is a standard fetch handler, so the same schema runs behind Node's http server, in a container, or on an edge runtime such as Cloudflare Workers.
  • Switch behaviour by environment: GraphiQL and introspection on in development, off in production.
  • A deployed server needs health endpoints, structured logs, request timeouts and a graceful shutdown that stops accepting traffic before exiting.
  • Edge runtimes suit stateless servers but restrict long-lived connections, native modules and in-memory state, which affects subscriptions, rate limiters and DataLoader caches.
  • Roll out schema changes additively and in order: server first, clients after, removals last.

A handler, not a framework

Deploying a GraphQL server turns out to be deploying a function from request to response. GraphQL Yoga exposes exactly that. In earlier lessons I called yoga.fetch(request) directly in tests, and createServer(yoga) hands the same handler to Node’s HTTP server. Because it uses the standard Request and Response types, any runtime that can call a fetch handler can host it: Node in a container, a serverless function, Bun, Deno, or an edge worker.

That decides the real questions. Not “how do I deploy GraphQL” but “what does my runtime give me, and what does it take away”.

One config, two environments

I ran this server with Node 26 and Yoga 6.1.0, switching on NODE_ENV:

const prod = process.env.NODE_ENV === "production";
const yoga = createYoga({
  schema,
  graphiql: !prod,
  plugins: prod ? [useDisableIntrospection()] : [],
});
createServer(yoga).listen(Number(process.env.PORT ?? 4000), "0.0.0.0");

With NODE_ENV=production, an introspection query came back with the “introspection has been disabled” error, a browser-style request for the GraphiQL page got HTTP 406, and Yoga’s built-in /health endpoint answered 200. In development the same file serves GraphiQL. Keep that switch in one place so production hardening from the hardening lesson is the default instead of something to remember. Bind to 0.0.0.0 inside a container, or the platform cannot reach the process, and read the port from the environment.

On Node, in a container

The usual shape is a small image: install dependencies, copy the code, run as a non-root user, CMD ["node", "server.js"]. I did not build a container here, so the Dockerfile details are standard practice and not verified in this session. What matters for GraphQL specifically:

  • Health checks. Yoga ships /health (liveness) and /readiness endpoints. I saw /health return 200 and did not test readiness. Point the orchestrator’s probes at them. A readiness check that confirms the database connection is worth writing yourself.
  • Logs. Yoga logged each request with operation type, status, error count and duration. Ship these as structured JSON in production, and pair them with traces.
  • Timeouts. Set a request timeout and database statement timeouts. Limits refuse bad queries early; timeouts stop good-looking queries that run long.
  • Graceful shutdown. On SIGTERM, stop accepting new connections and finish in-flight ones before exiting. server.close() does this, but it waits for idle keep-alive connections too, so set a deadline. When I tried a SIGTERM handler with a client that had left a connection open, my test shell hung until I stopped it by hand, which is the exact failure to design around: close idle connections or force exit after a grace period.
  • Secrets and config from the environment, as in configuration and environment variables.

Keep stateful pieces in mind when you run several replicas. The in-memory rate limiter and response cache from earlier lessons are per process, so each replica holds its own copy; use a shared store when you need one global answer. DataLoader caches stay per-request and are unaffected. Deploying Node applications covers the platform side in more detail.

On the edge

Cloudflare Workers and similar runtimes run your handler close to users with no server to manage. Yoga supports them because it is a fetch handler; the export is the handler itself. The trade-offs are the runtime’s:

  • No long-lived processes, so in-memory caches and limiters reset or split across isolates.
  • Limits on CPU time and memory per request.
  • No native Node modules, and the database must be reachable over HTTP or a supported driver. A connection pooling service or HTTP database API is usually needed.
  • Subscriptions need support for long-lived connections or SSE, which varies by platform.

For a stateless server over an HTTP data source, the edge is simple and fast. For one with heavy database work or WebSockets, a regular container is easier. I did not deploy to Workers; that part comes from the video and Yoga’s documentation.

A federated deployment

With a router, each subgraph deploys like any service, the router deploys as its own service, and the supergraph schema is the glue. Make subgraphs reachable only from the router. Deploy new subgraph versions first, publish the new supergraph after the checks pass, and let the router pick it up.

Rolling out schema changes

Deploys are where schema evolution meets reality, because for a while old and new versions are both live:

  1. Ship server changes that only add: new fields, new types, deprecations.
  2. Ship clients that use them.
  3. Remove deprecated fields only after usage is zero, with CI checks holding the line.

Mobile clients stay old for months, so “clients” includes versions you cannot update. Persisted-query manifests from that lesson must stay live for as long as those clients do.

What the video covers

Jamie Barton’s “Build a GraphQL API with Cloudflare Workers at the edge with Yoga 3” is a two-minute video from January 2023. It has no chapters and I could not read a transcript, so I rely on its title: building a GraphQL API with Yoga on Cloudflare Workers. It is a short demonstration, not a production guide, and it predates current Yoga and Workers tooling, so expect commands to differ. It is the weakest pick in this group and was chosen because few videos cover a Yoga deployment at all. A four-minute follow-up on deploying through GitHub Actions sits under “go deeper”. The container and health-check material above comes from my own run and general practice, not the video.

Where this fits

This closes the roadmap: from a first query to a guarded, observable, composed graph in production. A natural next step is the API design roadmap, to compare GraphQL with other API styles.

Check yourself

  1. 01Why does the same Yoga schema run on both Node and Cloudflare Workers?

    Yoga is built on the standard Request and Response objects, so any runtime that calls a fetch handler can host it.

  2. 02Why should in-memory rate limiting be reconsidered when you deploy several instances or use an edge runtime?

    Each instance or isolate counts separately, so the real limit is multiplied. A shared store is needed for a global limit.

  3. 03What order do you deploy an added field and a client that uses it?

    The server with the new field first, then the client, so the client never asks for a field the live server lacks.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark