useMutation doesn't roll back optimistic cache.evict
See original GitHub issueSuccessful rollback
When I call this mutation, the following happens:
update
is run withoptimisticResponse
, and an item is removed from the list- error is received from the server
- the changes made in
update
are rolled back: the item is added back to the list
const [removeReview] = useMutation(REMOVE_REVIEW_MUTATION, {
update: (cache) => {
const { reviews } = cache.readQuery({ query: REVIEWS_QUERY })
cache.writeQuery({
query: REVIEWS_QUERY,
data: { reviews: reviews.filter((review) => review.id !== id) },
})
},
})
...
removeReview({
variables: { id },
optimisticResponse: {
removeReview: true,
},
}).catch((e) => {
if (find(e.graphQLErrors, { message: 'unauthorized' })) {
alert('👮♀️✋ You can only delete your own reviews!')
}
})
Failing rollback
When the mutation update
uses cache.evict
instead of cache.writeQuery
, step 3 does not occur.
const [removeReview] = useMutation(REMOVE_REVIEW_MUTATION, {
update: (cache) => cache.evict({ id: cache.identify(review) })
})
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
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:9 (5 by maintainers)
Top Results From Across the Web
Optimistic mutation results - Apollo GraphQL Docs
When the code above calls mutate , the Apollo Client cache stores a Comment object with the field values specified in optimisticResponse ....
Read more >Evict deleted items from the Apollo cache
When deleting an item, we express our intent to completely remove all traces of it. If the mutation succeeds, that means it's gone...
Read more >Tips and Tricks for working with Apollo Cache
When the server does ultimately respond, the optimistic result will be replaced by the actual result. If the mutation fails, the optimistic ......
Read more >reactjs - Should manually updating the cache always be ...
import { useMutation, useQueryClient } from 'react-query' const ... then if the request fails you just roll back your update. this way your ......
Read more >Mutations – Angular
The optimistic response doesn't have to be exactly correct because it ... import { InMemoryCache } from '@apollo/client/core' @NgModule({ ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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
update
function typically runs for both the optimistic update and the final non-optimistic update, so it would be tricky to handle the hypotheticaloptimistic: true
option in the non-optimistic case, even if it seemed to solve the problem in the optimistic case.Just ran into this today using
v3.3.20
. Anoptimistic
flag would be really helpful!