-
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
[AC3] FieldPolicy merge function with external variables cache issues #7173
Comments
@offirgolan It sounds like you're overusing This is a safe default policy, since the cache doesn't know what any of your arguments mean, so it has to assume every possible combination of key arguments is logically distinct (unless you tell it otherwise), but it's not a great policy when you want to reuse existing cache data with slightly different arguments. Guessing from your argument names, I suspect All other arguments can be examined at runtime in Here's a draft of some new documentation that should help visualize what happens to your |
Hi @benjamn, thank you for your response and the link to the updated docs but this doesn't really solve the issue at hand. I understand how keyArgs affect the cache and from my testing, the pagination and merge function behave as expected with the keyArgs I've specified. The issue is when variables that aren't part of the keyArgs change, the new page is being merged with an existing previous page where instead, |
I'm not really sure I understand what you mean by this. How would examining There is also no reference to the previous args so there is nothing to compare them to... |
@offirgolan My suggestion about examining That said, I now realize this issue is about a much narrower question: how to make sure the One immediate answer to this question is that new InMemoryCache({
typePolicies: {
Result: { // guessing this __typename
fields: {
foos: {
keyArgs({ fooIds }, { variables: { hasFooIds } }) {
return JSON.stringify({ fooIds, hasFooIds });
},
},
},
},
},
}) I've configured this Is that closer to answering your question? |
I think we're getting closer but I don't believe this is quite it. Currently the pagination works as expected when any of the args listed in the search keyArgs change (filter, query, ranking, and index). The main issue is that I want the same behavior to occur when In the example you've given above, you're handling Also, would the nested keyArgs of |
@offirgolan I'm out of guesses, but I would like to continue the discussion here, so I think a simplified reproduction would really help.
From the perspective of the Since your I recommend setting a breakpoint in your |
Hi @offirgolan - regarding your original comment:
If you write a custom const cache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
search: {
keyArgs(
{ index, query, filter, ranking, perPage },
{ variables: { fooIds } }
) {
return JSON.stringify({
index,
query,
filter,
ranking,
perPage,
fooIds,
});
},
// ... everything else
},
},
},
},
}); This This use of a |
Let us know if this is still a concern with |
I have the following search query as well as a custom field policy for the
search
query with a merge function to handlefetchMore
pagination. When any of the args listed in thekeyArgs
changes, the cache "resets" and the hook requests the first page of data with the new args.The issue here is that I also need to have
hasFooIds
andfooIds
to have the same effect as the other arguments added tokeyArgs
. Unfortunately, these arguments aren't passed directly to thesearch
query so I can't add them to thekeyArgs
.Search Query
Field Policy with merge function
useQuery
OptionsIntended outcome:
Changing
fooIds
which is not part of keyArgsChanging
filter
which is part of keyArgsActual outcome:
Changing
fooIds
which is not part of keyArgs 👎Changing
filter
which is part of keyArgs 👍How to reproduce the issue:
Versions
ApolloClient: v3.2.4
System:
OS: macOS 10.15.6
Binaries:
Node: 10.18.1
Yarn: 1.22.4
npm: 6.13.4
Browsers:
Chrome: 86.0.4240.80
Firefox: 72.0.2
Safari: 14.0
The text was updated successfully, but these errors were encountered: