apollo-client
apollo-client copied to clipboard
Type-level merge function not called for nested array items
Intended outcome:
When defining a Type-level merge function:
new InMemoryCache({
typePolicies: { LevelB: { merge: mergeB } }
});
I expect mergeB
function to get called any time a LevelB item is written to the cache normally.
Actual outcome:
Currently mergeB is only called if LevelB item is returned as a single value, but not when it is returned inside of an array.
query { querySingle: { singleB: { id } } }
Does work and calls the mergeB merge function, but:
query { queryAll: { allBs: { id } } }
does not call the merge function for the LevelB items.
How to reproduce the issue: Reproduction at: https://github.com/Huulivoide/apollo-nested-merge When run, one expects to see the merge function to be run for all Item levels, but merging is omitted for levels/items that are returned in arrays (Items B and D).
Versions System: OS: Linux 5.18 Arch Linux Binaries: Node: 18.3.0 - /usr/bin/node Yarn: 1.22.19 - /usr/bin/yarn npm: 8.5.5 - /usr/bin/npm Browsers: Chrome: 102.0.5005.115 Firefox: 101.0.1 npmPackages: @apollo/client: ^3.6.8 => 3.6.8 npmGlobalPackages: apollo: 2.33.3
I belive the problem lies around here https://github.com/apollographql/apollo-client/blob/67a13b5331047264797cb50e2b19bf1ae6bacd85/src/cache/inmemory/writeToStore.ts#L321
StoreWriter.processSelectionSet().flattenFields
If the value is an array it is treated like a scalar, instead of being recursed into.
Is the entity returned from the querySingle
be also present in the array of entities of allBs
?
If no - merge function should not get called I believe
@Huulivoide Thank you for reporting this! You seemed to track this issue down to the line of code mentioned above, I was curious if you had the time to make a PR we could consider to fix this?
I am also experiencing this problem
Oh my, I had completely forgotten about this after implementing workarounds. I'll have to try and remember to get back at this after my long awaited summer vacation in a month or so.
What workarounds did you implement? @Huulivoide