The Apollo documentation discusses the use of cacheRedirects to tell Apollo how to access data that's already in the cache from other query.
It gives an example of this:
In some cases, a query requests data that already exists in the client store under a different key. A very common example of this is when your UI has a list view and a detail view that both use the same data. The list view might run the following query:
query ListView { books { id title abstract } }
When a specific book is selected, the detail view displays an individual item using this query:
query DetailView { book(id: $id) { id title abstract } }
We know that the data is most likely already in the client cache, but because it’s requested with a different query, Apollo Client doesn’t know that. In order to tell Apollo Client where to look for the data, we can define custom resolvers
I'm trying to understand why this is necessary for this example. If the books query returns an array of type Book, and the book request returns a single object of type Book, then surely the normalised cache will already have data for each of the books (from the ListView query) based on the typename and id, and the DetailView query can use that information directly without any further intervention. Instead, we're told for write some code to help it:
const cache = new InMemoryCache({
cacheRedirects: {
Query: {
book: (_, args, { getCacheKey }) =>
getCacheKey({ __typename: 'Book', id: args.id })
},
},
});
Underwhat exactly which circumstance is the ApolloClient not able to figure this out for itself, and why?