-
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
useMutation doesn't roll back optimistic cache.evict #7321
Comments
@lorensr This is an intentional consequence of #5773, though I understand that behavior can be surprising. However, when that functionality was implemented, |
Yeah, if there were an option like Not sure how to name it to make it obvious what to use it for. |
@lorensr What if we reused // Evict from all layers of the cache (the current behavior)
cache.evict({ id, fields: {...} });
// Evict from the top layer only (can be rolled back)
cache.evict({ id, fields: {...}, optimistic: true });
// Evict from the bottom layer only (ignores optimistic layers)
cache.evict({ id, fields: {...}, optimistic: false }); |
Looks good! Using an enum for the 3 options with BOTH as default option might be a little clearer, but less neat. I like boolean. Also wonder what the use cases are for |
Any updates on this? |
Just ran into this today using |
Something news about this optimistic variant? |
After thinking further about the discussion in #7321, I believe the inability to roll back optimistic evictions is simply a bug that we can fix, rather than something the developer should have to worry about (e.g. by passing `optimistic: true`, as I previously proposed in #7321 (comment)). Normally, `cache.optimisticData` points to the topmost currently active optimistic `Layer`, and `cache.data` points to the root/non-optimistic layer. While running an optimistic `update` function, we temporarily set `cache.data` to be `=== cache.optimisticData`, in an attempt to confine the optimistic update to one optimistic layer. Eviction was (until now) an exception to this confinement, because optimistic `Layer` objects still have a chain of `layer.parent.parent...` objects that eventually terminates at the `Root` layer. Before now, eviction was blindly following that entire chain, ignoring the current value of `cache.data`. This commit uses `cache.data` as the stopping point for eviction, which has the effect of confining optimistic evictions to the one layer that they should be updating, hopefully solving #7321 without requiring any intervention by the developer.
@lorensr @brainkim @Risbot @gmathieu @3nvi I did some more thinking, and I believe I've found a way to solve the original problem without passing any additional options: #8829 This is good news, because the same |
After thinking further about the discussion in #7321, I believe the inability to roll back optimistic evictions is simply a bug that we can fix, rather than something the developer should have to worry about (e.g. by passing `optimistic: true`, as I previously proposed in #7321 (comment)). Normally, `cache.optimisticData` points to the topmost currently active optimistic `Layer`, and `cache.data` points to the root/non-optimistic layer. While running an optimistic `update` function, we temporarily set `cache.data` to be `=== cache.optimisticData`, in an attempt to confine the optimistic update to one optimistic layer. Eviction was (until now) an exception to this confinement, because optimistic `Layer` objects still have a chain of `layer.parent.parent...` objects that eventually terminates at the `Root` layer. Before now, eviction was blindly following that entire chain, ignoring the current value of `cache.data`. This commit uses `cache.data` as the stopping point for eviction, which has the effect of confining optimistic evictions to the one layer that they should be updating, hopefully solving #7321 without requiring any intervention by the developer.
Hi @lorensr, I am using cache.evict({ id: cache.identify(xxxx) }) to implement the deletion functionality. but the optimistic UI is broken after I upgraded to latest version.
@benjamn could u help to enlighten me how to make the optimistic update(deletion) work? |
Successful rollback
When I call this mutation, the following happens:
update
is run withoptimisticResponse
, and an item is removed from the listupdate
are rolled back: the item is added back to the listFailing rollback
When the mutation
update
usescache.evict
instead ofcache.writeQuery
, step 3 does not occur.https://github.com/GraphQLGuide/guide/blob/repro-rollback-fail/src/components/Review.js#L132-L141
Versions
System:
OS: macOS 10.15.7
Binaries:
Node: 10.22.0 - ~/.nvm/versions/node/v10.22.0/bin/node
Yarn: 1.22.4 - ~/gh/guide/node_modules/.bin/yarn
npm: 6.14.6 - ~/.nvm/versions/node/v10.22.0/bin/npm
Browsers:
Chrome: 86.0.4240.193
Safari: 14.0
npmPackages:
@apollo/client: 3.1.3 => 3.2.5
apollo: ^2.30.2 => 2.30.2
apollo-cache-persist: 0.1.1 => 0.1.1
apollo-link-rest: 0.8.0-beta.0 => 0.8.0-beta.0
npmGlobalPackages:
apollo: 2.30.2
The text was updated successfully, but these errors were encountered: