question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useMutation doesn't roll back optimistic cache.evict

See original GitHub issue

Successful rollback

When I call this mutation, the following happens:

  1. update is run with optimisticResponse, and an item is removed from the list
  2. error is received from the server
  3. 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:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
benjamncommented, Sep 23, 2021

@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 hypothetical optimistic: true option in the non-optimistic case, even if it seemed to solve the problem in the optimistic case.

2reactions
gmathieucommented, Jul 1, 2021

Just ran into this today using v3.3.20. An optimistic flag would be really helpful!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found