Skip to content

Instantly share code, notes, and snippets.

@wesbos
Created Jan 14, 2021
Embed
What would you like to do?
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