"Demystifying Cache Normalization" blog article seems to be wrong
See original GitHub issueThis article from apollographql.com says:
For example, if we were to:
- Perform a GetAllTodos query, normalizing and caching all todos from a backend
- Call GetTodoById on a todo that we had already retrieved with GetAllTodos
… then Apollo Client could just reach into the cache and get the object directly without making another request.
I tested this and GetTodoById
did NOT retrieve the todos from the Apollo Cache.
Intended outcome:
The official blog post accurately documents the behavior of Apollo Client.
Actual outcome:
The official blog post does NOT accurately document the behavior of Apollo Client.
I created a project to show this: https://github.com/srmagura/how-does-apollo-work. It is a trivial GraphQL API server and a React client which calls the server. The client first calls getAllTodos
which returns 5 todos. When you click the button, the client calls getTodoById
for each todo. Each GraphQL request for getTodoById
results in a network call. This contradicts what the blog post says should happen.
For what it’s worth, I think Apollo Client is behaving correctly and the blog post is just wrong.
How to reproduce the issue: Clone and run the repository I linked to in the previous section.
Versions
System:
OS: macOS 12.5.1
Binaries:
Node: 16.16.0 - ~/.nvm/versions/node/v16.16.0/bin/node
Yarn: 1.22.19 - ~/.nvm/versions/node/v16.16.0/bin/yarn
npm: 8.16.0 - ~/.nvm/versions/node/v16.16.0/bin/npm
Browsers:
Chrome: 105.0.5195.102
Firefox: 104.0.1
Safari: 15.6.1
Additional reference StackOverflow answer that backs up what I said: https://stackoverflow.com/a/50839798/752601
Issue Analytics
- State:
- Created a year ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
This is mentioned in the stackoverflow link in the issue description, but for anyone who finds this thread looking for a solution: this functionality does exist in apollo client, it just needs a little bit of manual setup. You can find instructions here: Cache Redirects. (This is not mentioned in the article, so I agree that the article should be changed)
@markusgabriel Apollo cannot know the true behavior of your GraphQL server (i.e. resolvers). So Apollo cannot assume
getTodoById(id: "123")
returns the todo with ID=“123”. Therefore, Apollo must make the network call — it cannot simply look up theTodo
in the cache.If it helps, imagine you had a query like
getAllTodosExcept(id: "123")
. You would be upset if Apollo used the cache and returned theTodo
with ID=“123” in this case!