⬡ GraphQL Advanced

How does client-side caching work with Apollo Client's normalized cache?

Answer

Apollo Client's InMemoryCache normalizes all query results into a flat key-value store where each object is stored by a unique cache key (default: TypeName:id, e.g., User:42). When multiple queries return the same User object, they all reference the same cache entry — updating it in one query automatically updates it everywhere. The cache's read and write methods enable optimistic UI: write the expected result before the mutation completes, then reconcile with the actual server response. Field policies customize how fields are read/written — for example, the keyArgs option controls which arguments are included in the cache key for paginated fields. Cache policies (cache-first, cache-and-network, network-only, no-cache) control staleness behavior. The Apollo Client DevTools extension lets you inspect, search, and modify the cache at runtime.