Optimistic UI With React Apollo (GraphQL)
How to implement Optimistic UI for insert/update/delete GraphQL mutations with React Apollo?

If you wonder what Optimistic UI is, I suggest you check out my previous article “Better UX with Optimistic UI”.
Most apps nowadays use GraphQL to talk to the server. Apollo is one of the famous tools to implement GraphQL both in the server and the client. Apollo Client is the Javascript library to interact with a GraphQL API. It supports major Javascript libraries and frameworks such as Angular, Vue, and React. In this article, I assume that you’re familiar with GraphQL and Apollo Client.
Generally, optimistic updates come with mutations. Typically, a mutation is an action that changes some data on the server (insert/update/delete) or performs server-side effects.
So… How Does Apollo Do an Optimistic Update?
Apollo lets you provide a fake response and updates the cache with it, then starts the actual mutation:
- If the mutation is successful it will replace the response from the server with the fake response you provided.
- If the mutation is failed, it will roll back the changes you made via the fake response.
Let’s see some examples.
Like Button
I showed you how to implement a like button in the previous article. Let’s see how we can do the same with Apollo Client.

We have a component that renders the tweets:






