-
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
useQuery with changed variable on Apollo Cache doesn't work #6734
Comments
You may be right that all the data is in the cache already, but the cache can't guess which variables are relevant for reading this particular query, so it's unsafe to return data when the variables don't match exactly. Fortunately, in AC3, you can define a custom const cache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
products: {
// Prevent the cache from using the arguments to store separate values for this field:
keyArgs: false,
// Define how to use args.{where,take,...} to return flexible views of the existing data:
read(existing, { args }) {
// Note: existing is whatever merge returns, and may be undefined if no data has been written yet.
return readWithArgs(existing, args);
},
// Define how new data is combined with existing data to support reading:
merge(existing = emptyData(), incoming, { args }) {
return mergeWithArgs(existing, incoming, args);
},
},
},
},
},
}); To be clear, See the documentation for further explanation, this recent comment for a related discussion of I know that's a lot to digest, but we're happy to answer any questions you have! |
Ok, thank you @benjamn. You're right, it's a lot to digest. I was really hoping I was just missing something obvious, and that an elegant solution already existed for automatically interacting with cache. Not to worry though, I will study this. EDIT:
I assume that what I need to do is something like this (if I were trying to filter based on the property
One curious thing, when I attempt to console log the parameters, the
|
Ok so, the By the way, the |
You're the man @benjamn ! You gave me just enough info and context to get me on the right track and learn! Successful implementation! Definitely a bit more work than just calling useQuery, but not too much to make the use of cached data a waste of resources. Thank you again. Once I have finished writing the custom logic, I'll share here for anyone in the future who's curious. The other Issues here helped immeasurably. |
Intended outcome:
Populated Apollo Cache could be filtered with useQuery's refetch or useLazyQuery.
Actual outcome:
Only useLazyQuery and useQuery's refetch with identitical variables will return cache. Otherwise, despite all the data being there, nothing will be returned.
How to reproduce the issue:
This basic useQuery breaks the website:
MutationsQueries.js
_app
apolloClient
(for initializing Apollo Store via Next.js' getStaticProps)Schema
Versions
System:
OS: Windows 10 10.0.18362
Binaries:
Node: 10.16.0 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.4 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
npm: 6.9.0 - C:\Program Files\nodejs\npm.CMD
Browsers:
Edge: Spartan (44.18362.449.0)
npmPackages:
@apollo/client: ^3.0.2 => 3.0.2
@apollo/link-context: ^2.0.0-beta.3 => 2.0.0-beta.3
@apollo/link-ws: ^2.0.0-beta.3 => 2.0.0-beta.3
apollo-cache-persist-dev: ^0.2.1 => 0.2.1
apollo-link: ^1.2.13 => 1.2.14
apollo-link-context: ^1.0.19 => 1.0.20
The text was updated successfully, but these errors were encountered: