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.

Cache Redirect not causing query to run on partial data when using lists

See original GitHub issue

Intended outcome: Query should be sent to the server to receive the missing reference

Actual outcome: Query not sent to server and I get no data

How to reproduce the issue:

  1. Create a query that fetches a list of items based on a condition:
items(where: { id: $id }) {
    ...CoreItemFields
  }

This will return [item]

  1. Create a cache redirect to the query
new InMemoryCache({
        typePolicies: {
            Query: {
                fields: {
                    items: {
                        read(_, { args, toReference }) {
                            if (!!args.where?.id) {
                                return [toReference({ __typename: "Item", id: args.where.id })];
                            }
                        }
                    }
                }
            }
        }
    }

If you run a query that queries items that do not exist in the cache, the read function will run, returning a list of 1 reference as expected. But since the reference references to something that does not exist in cache, the query should run. But it doesn’t.

Versions System: OS: macOS 12.0.1 Binaries: Node: 14.18.1 - ~/.nvm/versions/node/v14.18.1/bin/node Yarn: 1.22.15 - ~/.yarn/bin/yarn npm: 6.14.15 - ~/.nvm/versions/node/v14.18.1/bin/npm Browsers: Chrome: 95.0.4638.69 Safari: 15.1 npmPackages: apollo-server-express: ^3.3.0 => 3.4.0

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
sebastienvacommented, Dec 23, 2021

Same here since we upgraded to 3.x (no issue in 2.x), but I use a workaround. If one ref is missing from cache, I disable the cache redirect to trigger the query.

Something like that :

read(_, { args, toReference, canRead }) {
   if (!!args.where?.id) {
     const refs =  [toReference({ __typename: "Item", id: args.where.id })];

     if (refs.find(ref => !canRead(ref))) {
       return undefined;
     }

     return refs;
  }
}

I hope this can help.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced topics on caching in Apollo Client
Cache redirects ​​ In some cases, a query requests data that already exists in the cache under a different reference. For example, your...
Read more >
Apollo Client - Cache Redirects for sub-types - Stack Overflow
I would like to configure cacheRedirects so that when I demand the Book.CoverColor or Cup.CoverColor (via allBooks for instance ); it will first ......
Read more >
React Query 3: A Guide to Fetching and Managing Data
For every API endpoint, you'll need to deal with state management, synchronization, caching and error handling. In this article, you'll learn ...
Read more >
Cache-Control - HTTP - MDN Web Docs
If you want caches to always check for content updates while reusing stored content, no-cache is the directive to use.
Read more >
How can I make Chrome stop caching redirects? - Super User
It's not a perfect solution, but I was able to prevent Chrome from using the cached redirect by passing a bogus query string,...
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