Skip to content

Instantly share code, notes, and snippets.

@wesbos
Created January 14, 2021 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wesbos/77359f617cac497c167b31a8e6b08581 to your computer and use it in GitHub Desktop.
Save wesbos/77359f617cac497c167b31a8e6b08581 to your computer and use it in GitHub Desktop.
function update(cache, payload) {
cache.evict(cache.identify(payload.data.deleteCartItem));
}
const [removeFromCart, { loading }] = useMutation(REMOVE_FROM_CART_MUTATION, {
variables: { id },
update,
// When the optimistic response comes back, it seems to re-fetch every single query on the page, leaving `data` from queries undefined (and loading and error undefined as well), causing the layout to break.
// // The error itself comes from next.js, and it does seem that the item is evicted from the cache before that happens...
// optimisticResponse: {
// deleteCartItem: {
// __typename: 'CartItem',
// id,
// },
// },
});
@KruglikDev
Copy link

KruglikDev commented Jul 17, 2022

Hi, they fixed that bug in modern versions of @apollo/client (I am using 3.6.9), update it and it will work
or you can wrap evict inside setTimeout, like this:

setTimeout(() => { cache.evict(cache.identify(payload.data.deleteCartItem)); }, 0);

and it also will work with OptimisticResponse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment