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.

Unexpected Behavior with `fetchPolicy` "network-only" and `nextFetchPolicy` "cache-first" In 3.7.x

See original GitHub issue

I’m in the middle of doing an upgrade of AC 3.6.8 to 3.7.x (currently tested and template made with 3.7.1) and I found something that I think is a bug, but I’m not sure.

Intended outcome: I have useQuery inside a component that is conditionally shown that uses the fetchPolicy of "network-only" and nextFetchPolicy of "cache-first". In the demo below, I’ve made the querying component conditionally mount with a toggle button. I expect that every time the component is mounted fresh, it should always fetch from the network and ignore the cache for that first load.

Actual outcome: It appears that, instead, the component comes to life, fetches from the network, fills the cache, then when I hide and fully unmount the component, then when I toggle it back to mounted, it does in fact start fetching from the network but seems to “apply” the "cache-first" policy too quickly and displays the cached data before the data comes back from the network. In the demo below, I’ve raised the delay on the network to 1.5s so it’s more obvious and made the length of the people array a count of the number of times the server data is used.

In other words, I think I expect:

  • click to show the querying component, I see Loading... for 1.5s, then 1 as the count.
  • click to hide the querying component
  • click to show the querying component, I see Loading... for 1.5s, then 2 as the count.

but instead, what actually happens on that last bullet is I never see Loading..., I see 1 as the count, then 1.5s later it updates to 2.

How to reproduce the issue: https://github.com/dairyisscary/react-apollo-error-template

Versions 3.7.x

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
skrivlecommented, Dec 20, 2022

Hi All 👋

We are also experiencing this issue it seems. We were able to reproduce this both in a test case and in browser here: https://github.com/skrivle/apollo-next-fetch

This is a regular CRA project. You can run npm test and you’ll see the test failing. To make the test pass you can comment out the nextFetchPolicy config item in App.tsx

For the in browser reproduction please follow these steps:

  • npm start
  • click contact 1
  • click contact 2
  • click unload (which will unmount the app component)
  • click contact 1
  • observe that “loading” is shown very briefly and then it incorrectly shows the cached data for contact 2 until data for contact 1 is loaded.

Note: the conversation items have the same id but different content across the two requests. This is to illustrate the scenario where the properties of an object with a consistent id change over time.

1reaction
levrikcommented, Nov 18, 2022

@jerelmiller Exactly. I’ll try to assemble an example highlighting the issue latest next week.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Queries - Apollo GraphQL Docs
If all data is available locally, useQuery returns that data and doesn't query your GraphQL server. This cache-first policy is Apollo Client's default...
Read more >
@apollo/client | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >
Prevent an Unnecessary Refetch with nextFetchPolicy and ...
I recently figured out the cause of a strange issue due to a change in how the cache-and-network fetch policy works in Apollo...
Read more >
React Apollo: What Is It & How to Use It in 2022 - Pagepro
const { loading, error, data } = useQuery(GET_PRODUCT_LIST, { fetchPolicy: "network-only", // Used for first request nextFetchPolicy: "cache- ...
Read more >
Pitfalls I fell into during Apollo Client 3.0 migration
This will prevent requesting the same data again for the initial query. options: { fetchPolicy: 'network-only', nextFetchPolicy: 'cache-first' }, ...
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