-
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
Merge function never brings existing value for an identified fragment on cache #9086
Comments
@Richacinas I'm not sure if this is a typo or an explanation, but I think your first fragment has an unnecessary fragment Impagados on Impagados {
impagados { # Is this necessary?
id
__typename
# ...
}
} The second fragment you showed (in the fragment Impagados on Impagados {
id
__typename
# ...
} The |
Thanks for your answer.
Sorry, that was a typo. What I'm trying to achieve is that, when the second query is launched, I can read the value that I got on the first query for this fragment. Once I have that, I would like to choose which one I keep. Should I keep the Thanks a lot |
Any insight about this? |
@Richacinas Thanks for the elaboration! Sorry in advance for the wall of text… First some background: A This separation of field values by field name would still happen whether or not you request
Based your explanation, I think you might be looking for something (very roughly) along these lines, where your const otherFieldNames = new Map()
.set("foo", "bar")
.set("bar", "foo");
new InMemoryCache({
typePolicies: {
Impagados: {
// Note that this merge function might run for any field that holds an Impagados
// object, such as Query.foo or Query.bar.
merge(existing, incoming, { fieldName, readField, args, toReference }) {
const otherName = otherFieldNames.get(fieldName);
if (otherName) {
// If you don't need to pass args to readField:
// const otherValue = readField(otherName, toReference("ROOT_QUERY"));
// If you need to pass args to readField, it has a more flexible options API:
const otherValue = readField({
fieldName: otherName,
args,
// Specifying options.from is unfortunately necessary when calling readField
// from a merge function, since there's more than one possible parent object
// (existing or incoming).
from: toReference("ROOT_QUERY"),
});
if (typeof otherValue !== "undefined") {
// TODO Decide whether to return otherValue here or continue processing incoming.
}
}
if (existing) {
// TODO Decide whether to return existing or incoming or some combination.
}
return incoming;
},
},
}) I have to admit I'm not thrilled with the boilerplate/technicality/amount of this code. In particular, I regret the hard-coding of It would be convenient if With all of that said, I do see the value in your use case, and would love to keep talking about how to make it easier. |
Let me have a proper read about this again.. I would like to keep talking about it as well. |
I have 2 queries which I use with the same variable/input, like this:
I use it, let's say:
Intended outcome:
I would expect my merge function to bring existing data from previous queries. This sample merge function triggers ok:
Actual outcome:
After
GET_MY_DATA_FOO
query was launched, retrieved the data, and triggered themerge
function (withexisting
value undefined, of course), I launchGET_MY_DATA_BAR
which comes withexisting
value undefined as well.I have tried removing the
id
field from the fragment, so it wouldn't become an entity, so to say, and I could maybe access the same fragment already cached on the other query. But I cannot find a way to do it.If I keep the
id
field there, the merge function triggers, there is noexisting
value and trying to read the fragment from cache already brings the new value that was already written on cache:Versions
System:
OS: Linux 5.4 Ubuntu 20.04.3 LTS (Focal Fossa)
Binaries:
Node: 14.15.0 - ~/.nvm/versions/node/v14.15.0/bin/node
Yarn: 1.22.15 - ~/.nvm/versions/node/v14.15.0/bin/yarn
npm: 8.1.3 - ~/.nvm/versions/node/v14.15.0/bin/npm
Browsers:
Chrome: 94.0.4606.71
npmPackages:
@apollo/client: ~3.2.5 => 3.2.9
The text was updated successfully, but these errors were encountered: