Skip to content
tezvyn:
Menu

Apollo's normalized cache

Source: interviewHardHow cards are made

Summary

understanding GraphQL client caching.

Key points

Apollo flattens query results into entities keyed by type plus id, so one object is stored once and shared, updating everywhere at once.

What's really being asked

Whether you understand that Apollo does not cache responses as opaque blobs but normalizes them into a flat, shared entity store, and why that matters for consistency across screens.

The full answer

When a query returns, Apollo's InMemoryCache breaks the nested result into individual objects, computing a cache id for each from its __typename plus its id or keyFields. It stores these objects in a flat map and replaces nested objects with references, so any entity exists exactly once regardless of how many queries returned it. Because storage is by identity, a mutation or another query that updates that entity is reflected instantly on every screen reading it, with no manual refetch. Overlapping queries reuse cached entities, reducing network calls, and the cache can serve a query partly or wholly from memory.

The mistakes people make

Thinking Apollo caches each query's full response separately, which would duplicate shared objects and let copies drift out of sync; assuming entities without an id are still merged; or believing updates always require refetching. Forgetting that you must configure keyFields for types lacking an id is a practical gap.

What usually comes next

What happens to objects that lack an id field? How do you update the cache after a mutation that adds to a list? How do field policies and type policies customize merging? How does this compare to React Query's caching?

A concrete example

A feed screen and a profile screen both fetch User 7. Apollo stores User:7 once. When a mutation renames User 7, the single cache entry updates and both screens re-render with the new name immediately, no refetch needed. Without normalization, each query would hold its own copy, so the profile screen would keep showing the stale name until manually refreshed.

Interview question

Why does renaming a user via one mutation instantly update every screen showing that user in Apollo's normalized cache?

  • a.Each screen polls the server every second to detect entity changes
  • b.Mutations broadcast a force-refresh event that remounts all components
  • c.The entity is stored once by its cache id, so all queries reference the same recordCorrect
  • d.Apollo refetches every active query automatically after any mutation
Why?

Normalization stores each entity a single time keyed by typename and id, so updating it propagates to every query referencing it. Apollo does not auto-refetch all queries, poll, or remount components for this, so those options are wrong.

Just read this? Test yourself on what you have been reading.

Read the original → apollographql.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles