Undoing optimistic updates on mutation error

I have a mutation that has an optimistic update and a cache update method. When the mutation succeeds, this is great, because the cache (and therefore the UI) is immediately updated without having to wait for the response from the server.

However, when the mutation fails, the cache and UI are still behaving as it succeeded. How do I tell the cache to undo the optimistic updates and go back to the previous state?

You should be able to update the state of the cache based on the mutation’s result. Take a look at the Mutations in Apollo Client - Client (React) - Apollo GraphQL Docs