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.

[v3.4 Regression] Changing variables uses nextFetchPolicy instead of fetchPolicy

See original GitHub issue

Intended outcome: I have a query with fetchPolicy: 'network-only' and nextFetchPolicy: 'cache-first'. When I change the variables, it should fetch new results from the server regardless of what’s in the cache.

Actual outcome: Changing the variables appears to use the nextFetchPolicy and does not fetch new data from the server.

How to reproduce the issue: https://github.com/dylanwulf/react-apollo-error-template/tree/changing-variables-nextFetchPolicy Please use branch changing-variables-nextFetchPolicy. Reproduction instructions: Open dev tools and watch the console. Click any of the unselected radio buttons, see that a request to the server is made (this is printed to the console instead of the network tab due to the apollo link). Click “All”, see that a request to the server is NOT made.

It looks like this problem first showed up in v3.4.0-rc.5 and still exists in v3.4.0-rc.14

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
benjamncommented, Jul 9, 2021

Alright, except for the weirdness about notifyOnNetworkStatusChange: false not working with React and useQuery, I believe this is fixed in @apollo/client@3.4.0-rc.18. Thanks for pushing this issue (and #6839) forward @dylanwulf!

2reactions
benjamncommented, Jun 28, 2021

@dylanwulf The operative change in 3.4.0-rc.5 was the addition of this line in PR #8346: https://github.com/apollographql/apollo-client/blob/f11a163a4e507441bd51c2e0f9571aad853d5856/src/react/data/QueryData.ts#L203 Calling applyNextFetchPolicy here advances immediately to nextFetchPolicy without ever using fetchPolicy, which seems incorrect to me. Removing this line fixes the reproduction you provided, though you might want to remove notifyOnNetworkStatusChange: true from your useQuery options to avoid receiving intermediate loading: true results with cache data.

On a more general note, since you mentioned #6839, I have to admit I’m not comfortable with the role useQuery plays in resetting fetchPolicy and nextFetchPolicy, which happens simply because useQuery is called again with (very likely) the same initial options object, every time the component rerenders. I happen to think fetchPolicy should be reset when variables change, but I don’t think useQuery is the right/best mechanism to accomplish that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Queries - Apollo GraphQL Docs
This nextFetchPolicy function will be called after each request, and uses the currentFetchPolicy parameter to decide how to modify the fetch policy. In...
Read more >
About Linear Regression | IBM
Linear regression analysis is used to predict the value of a variable based on the value of another variable. The variable you want...
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 >
Common pitfalls in statistical analysis: Linear regression ...
[3] This is done using “multiple linear regression. ... that the independent variable changes more for each unit increase in the predictor (ii...
Read more >
Understanding Apollo Fetch Policies | by Galen Corey - Medium
The default fetch policy for Apollo is cache-first. If you do not set a fetch policy yourself, this is what will be used....
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