-
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
When more than one useQuery hook is used network requests double #7196
Comments
@kolesan You have a couple of good options here, but the root of the issue is that the cache can't tell when it's safe to merge two First, have a look at the browser console, where you'll see a detailed warning about this problem (thanks to #6372):
Giving the Alternatively, if there is only ever one If neither of those options works for you, you can explicitly permit the values of the new InMemoryCache({
typePolicies: {
Query: {
fields: {
SiteStatistics: {
merge: true,
},
},
},
},
}) Finally, if you update to the latest beta of Apollo Client 3.3 ( new InMemoryCache({
typePolicies: {
SiteStatistics: {
merge: true,
},
},
}) Whatever approach you choose, the important thing is to prevent the second query's results from replacing the first query's results, which is what causes the first query to attempt a second network fetch. |
@benjamn Thank you for the quick answer. I tried your suggestions on my actual project, and through a combination of them managed to solve some of the issues, but not all. My schema is probably not particularly well designed, due to my lack of experience with graphql and caching. The last issue I ran in to was related to aliasing. I request the same object with same fragment 3 times, but give it a different alias each time. In this case the keyfields array I provided becomes not as unique as I thought. Is there a way to just tell apollo client to cache by query name or something similarly simple? It will not be as performant, but it would probably suffice in my case. On a side note, if this might interest you, I managed to reproduce these additional requests without getting the warning in the console.
And 2 queries:
|
+1 for this as I'm also experiencing aliasing issues whenever I use |
I also want something like this, so I sent a feature request: #8802 |
Intended outcome:
Two useQuery hooks with different queries (difference has to be in the sub fields) produce two network requests
Actual outcome:
Two useQuery hooks with different queries (difference has to be in the sub fields) produce three network requests
How to reproduce the issue:
Repo with reproduction: https://github.com/kolesan/react-apollo-error-template
npm start
go to network tab
refresh the page
observe three requests to the api instead of two
Versions
System:
OS: Windows 7 6.1.7601
Binaries:
Node: 13.8.0 - D:\nodejs\node.EXE
Yarn: 1.22.4 - ~\AppData\Roaming\npm\yarn.CMD
npm: 6.13.6 - D:\nodejs\npm.CMD
Browsers:
Chrome: 86.0.4240.75
npmPackages:
@apollo/client: ^3.2.0 => 3.2.0
reproducible on 3.2.1 and 3.2.5 as well
The text was updated successfully, but these errors were encountered: