How it works
The explanation, with examples you can run.
Making HTTP caching work again
The GraphQL over HTTP draft allows GET for queries, with the document and variables in the query string. Long documents make long URLs, so most setups use persisted queries: the client sends a hash of a document the server already knows.
GET /graphql?extensions={"persistedQuery":{"sha256Hash":"9f2c..."}}&variables={"id":"p1"}Cache-Control: public, max-age=60Now the URL is short and stable per query and variables, which is a key a CDN can use. When the documents are registered at build time and only those are accepted, they are called trusted documents and also serve as an allowlist (see the security topic). The response's Cache-Control must reflect its most sensitive field: if any part is per-user, the whole response is private. Some servers let you annotate fields with cache hints and compute the header from the fields actually selected.
The client's normalised cache
Press Run to see what your code prints, in order.
Runs in this browser, in an isolated frame with no access to this page. console.log, info, warn and error are shown; code runs as a classic script, so use an async function for await.
This is the idea behind the caches in Apollo Client, Relay and urql's graphcache: split responses into objects keyed by type and id, store each once, and rebuild query results from references. It only works if the schema gives objects stable identities, which is why graphql.org recommends a unique id on object types. The hard cases are lists: after createPost, the cache can't know which lists the new post belongs in, so the client updates them explicitly or refetches.
On the server
Whole-response caches on the server rarely pay off, because two clients seldom send exactly the same query and variables. What does pay off is caching below the resolvers: DataLoader de-duplicates within a request, and an application cache (Redis, an in-memory LRU) in front of slow services helps every query that touches the same data, whatever its shape.
Check yourself
3 questions. Get them all right to finish this topic.
0 of 3 right so far. Get all three to finish this topic.