Merge function never brings existing value for an identified fragment on cache
See original GitHub issueI have 2 queries which I use with the same variable/input, like this:
query GET_MY_DATA_FOO($id: String!) {
foo(id: $id) {
id
...Impagados
}
}
query GET_MY_DATA_BAR($id: String!) {
bar(id: $id) {
id
...Impagados
}
}
fragment Impagados on Impagados {
impagados {
id
__typename
ebe {
error
nota
}
icired {
error
nota
}
rai {
error
nota
}
}
}
I use it, let’s say:
const { data, error, loading } = useQuery(GET_MY_DATA_FOO, {
variables: { id: '1' },
});
const { data, error, loading } = useQuery(GET_MY_DATA_BAR, {
variables: { id: '1' },
});
Intended outcome:
I would expect my merge function to bring existing data from previous queries. This sample merge function triggers ok:
merge(existing = {}, incoming = {}, { cache, toReference, readField }) {
if (existing) return { ...existing };
return { ...incoming };
}
Actual outcome:
After GET_MY_DATA_FOO
query was launched, retrieved the data, and triggered the merge
function (with existing
value undefined, of course), I launch GET_MY_DATA_BAR
which comes with existing
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 no existing
value and trying to read the fragment from cache already brings the new value that was already written on cache:
const existing = cache.readFragment({
id: incoming.__ref,
fragment: gql`
fragment Impagados on Impagados {
id
ebe {
error
nota
}
icired {
error
nota
}
rai {
error
nota
}
}
`,
});
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
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
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
__typename
andid
fields on this fragment in order to to that, or should I tossid
so they stay separate? How could I get the value of the other fragment either way?Thanks a lot
Let me have a proper read about this again… I would like to keep talking about it as well.