Apollo Cache: Normalization and Updates
Understand Apollo's normalized InMemoryCache and update cached data after mutations without refetching.
How InMemoryCache Normalizes Data
InMemoryCache stores each object by a cache key composed of __typename and id: User:1, Post:42. When a query returns a User with id "1", it is stored once under this key regardless of how many different queries include it.
Any subsequent query that fetches the same User:1 reads from the single cache entry, ensuring all components see the same data.
Automatic Cross-Query Updates
When a mutation returns an updated User:1 object, Apollo writes it to the User:1 cache entry. Every active query that included User:1 automatically reflects the updated fields in the UI without any additional code.
This automatic propagation is the primary advantage of a normalized cache over a document (query-keyed) cache.
All lessons in this course
- GraphQL Fundamentals for React Developers
- Setting Up Apollo Client in React
- useQuery and useMutation Hooks
- Apollo Cache: Normalization and Updates