What is the Apollo Cache and how does normalized caching work?
Answer
Apollo Client's InMemoryCache uses a normalized cache that stores objects by their unique identity, not by query. When a query result is received, every object with an id field is flattened and stored under a cache key like User:42. Subsequent queries or mutations that return the same user (by ID) automatically update the same cache entry — UI components that display that user update automatically. This means: if you update User 42's name via a mutation that returns the updated user, all queries referencing User 42 reflect the change without refetching. Cache behavior is configured via typePolicies: define custom key fields, merge functions for paginated lists, and read functions for computed fields. The keyFields option specifies which fields to use for identity (default: ['__typename', 'id']).
More GraphQL Questions
View all →- Intermediate How do you implement pagination in GraphQL?
- Intermediate What is the Relay specification in GraphQL?
- Intermediate How do you implement authorization in GraphQL resolvers?
- Intermediate What is graphql-shield and how does it work?
- Intermediate How do you implement real-time subscriptions with GraphQL?