[v3.4 Regression] Changing variables uses nextFetchPolicy instead of fetchPolicy
See original GitHub issueIntended 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:
- Created 2 years ago
- Reactions:4
- Comments:7 (7 by maintainers)
Top GitHub Comments
Alright, except for the weirdness about
notifyOnNetworkStatusChange: false
not working with React anduseQuery
, I believe this is fixed in@apollo/client@3.4.0-rc.18
. Thanks for pushing this issue (and #6839) forward @dylanwulf!@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 CallingapplyNextFetchPolicy
here advances immediately tonextFetchPolicy
without ever usingfetchPolicy
, which seems incorrect to me. Removing this line fixes the reproduction you provided, though you might want to removenotifyOnNetworkStatusChange: true
from youruseQuery
options to avoid receiving intermediateloading: 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 resettingfetchPolicy
andnextFetchPolicy
, which happens simply becauseuseQuery
is called again with (very likely) the same initial options object, every time the component rerenders. I happen to thinkfetchPolicy
should be reset when variables change, but I don’t thinkuseQuery
is the right/best mechanism to accomplish that.