-
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
Referential identity of arrays lost when setting the cache in client resolvers #4662
Comments
Added a new, better example to the original description. It seems like this hast to do with arrays only. Seems like a serious bug for people like me who depend on simple identity checks in our view code. The objects inside the array are still good, but the array itself changes every time. |
I believe we could fix this by using a similar kind of caching for In my original benchmarking in #3394, I concluded that caching In short, if we proceed with your recommendation here, we will need your help to validate the hypothesis that providing |
I haven't taken a deep look into the issues you've described, Right now it's just troublesome in the view to have to check every element of an array just to decipher if it has changed. The logic is beautifully simple when normal immutability rules are followed. But there may also be a deeper issue here as well. Since in my current project, simply checking the identity of the elements was not enough, since there was, more deeply nested, arrays in the state. Oddly enough, nothing seemed to have changed, but nothing nested in the array had maintained identity. I have been really enjoying/getting used to the idea of client state in Apollo, but little issues like these seem to crop up, making me wonder if not many people use Apollo for local state, yet. Anyways, I'll help however I can. Right now this one issue is problematic and really inconsistent from how just the normal loading of new data from the network works (which "just works"). |
Should help with #4662. The caching is currently keyed on the identity of the input array object, rather than the identities of the elements. It would be relatively easy to use cacheKeyRoot.lookupArray(array) instead of just array in makeCacheKey, which would enable preserving array identity in even more cases, but that approach would also make the cache key computation more expensive, since makeCacheKey would have to traverse an input array of unbounded size.
Part of #4662. It's really nice that we can just remove the expectStrictEqualExceptArrays function from this test to verify that executeSubSelectedArray is caching its results correctly!
Ok, this appears to be fixed if you update to the latest beta version of Following up on my previous concerns, it turns out we can compute the cache key using the object identity of the array that was read from the cache (in other words, the array that gets passed into |
Wow! Nice! I'll check it out hopefully today. Thanks so much for getting on this so quickly! |
Seems to be working well. Thanks! |
Closing since we just published the final version(s) of |
Intended outcome:
When I change cache entries, using either
read/writeQuery
orread/writeFragment
in a client resolver, it should only change the property intended, and leave the referential integrity of everything else in tact.Actual outcome:
Currently, it causes all properties to lose referential integrity.The arrays are changed. Although it seems when loading data from normal server requests behaves properlyHow to reproduce the issue:
Just a rough example I was playing with. I can try to continue cleaning it up if necessary.
Make sure and start the server first. Clicking the
refetch
button will result in every other fetch having the nestedhere
property matching (correct and intended). Clicking thesetActive
button will result in thehere
prop never being referentially sound (broken, unintended)Server: https://codesandbox.io/s/6z2310j19z
Client: https://codesandbox.io/s/y0yyvp8pzj
EDIT: Ok... so I was able to simplify it. Originals left for reference if desired
https://codesandbox.io/s/oqnk90k68q
Versions
The text was updated successfully, but these errors were encountered: