question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Demystifying Cache Normalization" blog article seems to be wrong

See original GitHub issue

This article from apollographql.com says:

For example, if we were to:

  1. Perform a GetAllTodos query, normalizing and caching all todos from a backend
  2. 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:closed
  • Created a year ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
dylanwulfcommented, Sep 13, 2022

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)

1reaction
srmaguracommented, Sep 13, 2022

@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 the Todo 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 the Todo with ID=“123” in this case!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Demystifying Cache Normalization - Apollo GraphQL Blog
In this article, we're going to learn: The architecture of caching in Apollo Client; The algorithm that the cache uses to normalize objects ......
Read more >
Should a query in Apollo Client look for the results cached by ...
I guess the Apollo cache is not as smart as I thought! I feel the Apollo team should update the blog article I...
Read more >
Apollo on Twitter: "Apollo Client normalizes the data that ...
Apollo Client normalizes the data that comes from your backend auto-magically To learn more about how this works, read "Demystifying ...
Read more >
Configuring the Cache – Angular - GraphQL Code Generator
This article describes cache setup and configuration. ... arbitrary string, or // false to disable normalization for objects of this type.
Read more >
Demystifying Debugging in SQL Server 2005 | ITPro Today
The examples in this article use SQL Server 2005's AdventureWorks sample ... Server Explorer, which appears on the left, shows information ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found