-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
defaultOptions fetch policy 'cache-and-network' is not used #6677
Comments
you are using a graphql endpoint which spins down on no activity, so you first have to open https://48p1r2roz4.sse.codesandbox.io/ in order to see it working, but indeed this is a bug :) |
I can confirm this issue, I'm investigating with a teammate to see if we can find why 🤷 |
@Titozzz and I are pretty sure it's caused by this: apollo-client/src/core/QueryManager.ts Lines 836 to 852 in 50fa0b8
It would mean that when using
That doesn't seem right, or maybe I misunderstood something? 🤔 |
Some (somewhat organized) thoughts on this issue, after thinking about it this week-end. Please note I do not have full knowledge of the codebase, and while making assumptions I know I might not grasp the whole complexity of the code involved.
Yes, the data has changed since the last call (which was the first call, in a loading state, that returned null), but not compared to what I have in the cache (the objects are the same). Also, the condition is based on the loading state, so the tick will get updated anyway. => Right now, updating the tick re-renders AND triggers
Not commented: as stated in previous comments, the queries won't fire again when the variables change and the cache can be hit (cache-first after first call). Commented: apollo-client/src/react/hooks/utils/useDeepMemo.ts Lines 11 to 22 in 50fa0b8
I would suggest using hashing the I think making these 3 changes would:
Please let me know what you think, and if you agree to at least part of what I think, let me know how I could help 🙂 |
PR #6353 seemed like a clever zero-configuration way to improve the default behavior of the cache-and-network and network-only fetch policies. Even though the word "network" is right there in the policy strings, it can be surprising (see #6305) to see network requests happening when you didn't expect them. However, the wisdom of #6353 was contingent on this new behavior of falling back to cache-first not creating problems for anyone, and unfortunately that hope appears to have been overly optimistic/naive: #6677 (comment) To keep everyone happy, I think we need to revert #6353 while providing an easy way to achieve the same effect, when that's what you want. The new options.nextFetchPolicy option allows updating options.fetchPolicy after the intial network request, without calling observableQuery.setOptions. This could be considered a breaking change, but it's also a regression from Apollo Client 2.x that needs fixing. We are confident options.nextFetchPolicy will restore the #6353 functionality where desired, and we are much more comfortable requiring the explicit use of options.nextFetchPolicy in the future.
@adrienharnay Thanks for investigating and sharing your reasoning! We are strongly considering reverting #6353, with an easy way to restore that behavior if you really want it: #6712. Curious to hear if you have any thoughts about that. As for |
PR #6353 seemed like a clever zero-configuration way to improve the default behavior of the cache-and-network and network-only fetch policies. Even though the word "network" is right there in the policy strings, it can be surprising (see #6305) to see network requests happening when you didn't expect them. However, the wisdom of #6353 was contingent on this new behavior of falling back to cache-first not creating problems for anyone, and unfortunately that hope appears to have been overly optimistic/naive: #6677 (comment) To keep everyone happy, I think we need to revert #6353 while providing an easy way to achieve the same effect, when that's what you want. The new options.nextFetchPolicy option allows updating options.fetchPolicy after the initial network request, without having to call observableQuery.setOptions. Specifically, passing { nextFetchPolicy: "cache-first" } for network-only or cache-and-network queries should restore the behavior of #6353. This could be considered a breaking change, but it's also a regression from Apollo Client 2.x that needs fixing. We are confident options.nextFetchPolicy will enable the #6353 functionality where desired, and we are much more comfortable requiring the explicit use of options.nextFetchPolicy going forward.
Thank you for your answer, #6712 seems greats! As for |
I think it would be a wise decision to document the |
It doesn't sound like there is an outstanding issue here, so closing. Thanks! |
Well I guess it is still related to this unresolved issue: #7437 |
Intended outcome:
useQuery (and probably others) should use default fetch policy (in this case 'cache-and-network')
Actual outcome:
It does request only once, just like 'cache-first' fetch policy. It seems like it ignores default setting.
How to reproduce the issue:
I edited example from docs to show this bug. Press "Change" button to switch currency. It should make request everytime, but it does only once per currency.
https://codesandbox.io/s/default-options-u7jut
Versions
Apollo-Client: 3.0.2
React: 16.13.0
The text was updated successfully, but these errors were encountered: