Skip to content
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

useQuery and/or useLazyQuery with a fetchPolicy of 'cache-and-network' gets called twice. #7346

Closed
christo8989 opened this issue Nov 19, 2020 · 4 comments

Comments

@christo8989
Copy link

Intended outcome:
A single request is made when using 'network-and-cache' for the first time.

Actual outcome:
Two requests are made for the query with 'cache-and-network' when multiple useQuery(s) and/or useLazyQuery(s) are present. It also seems to be related to placing the 'cache-and-network' query below the other query.

How to reproduce the issue:
I have a unit test that uses MockProvider and it's throwing Error: No more mocked responses for the query.
This mock doesn't break in the unit test but you can see it when you look at the console and refresh the (inner) browser/app.
The loading state on query FOO_GQL goes from true -> false -> true.

https://codesandbox.io/s/sharp-darkness-dzy0b

Versions

System:
OS: macOS 10.15.6
Binaries:
Node: 11.15.0 - ~/.nvm/versions/node/v11.15.0/bin/node
Yarn: 1.22.4 - /usr/local/bin/yarn
npm: 6.7.0 - ~/.nvm/versions/node/v11.15.0/bin/npm
Browsers:
Chrome: 86.0.4240.198
Edge: 86.0.622.69
Firefox: 82.0.2
Safari: 14.0
npmPackages:
@apollo/client: ^3.2.7 => 3.2.7
apollo-link-tracer: ^1.0.26 => 1.0.26
apollo-upload-client: ^14.1.3 => 14.1.3

@jgan42
Copy link

jgan42 commented Nov 19, 2020

Hi, I had a similar issue and this can worth a try :

const bar = useQuery(BAR_GQL);
  const foo = useQuery(FOO_GQL, {
    fetchPolicy: "cache-and-network",
    nextFetchPolicy: "cache-first",
  });

I couldn't find any documentation about it but by testing it, it works as exepected. If you rerender the component, it will make a server call for "foo" and not "bar"

@benjamn
Copy link
Member

benjamn commented Nov 20, 2020

@christo8989 This should be fixed in @apollo/[email protected] (published earlier today), if you want to give it a try.

@christo8989
Copy link
Author

@benjamn
I updated the sandbox and it worked beautifully. Thank you very much for the quick response. 👍

@jcreighton
Copy link
Contributor

Looks like we're good to close this issue as it was resolved in @apollo/[email protected].

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants