Relay Modern — Optimistic Update

Lucas Besen
Aug 17, 2018 · 2 min read
Photo by Nathan Dumlao on Unsplash

Relay Modern provides an update way that solves the delay to update client data from server response.

It consists in update the client data with an anticipated value that reflects server response.

I'll describe in this post one way to update client data using optimisticResponse.

What happens if something goes wrong?

In case server side returns an error, the client data will get a rollback.

What happens if server response was different from updated data?

If the server response was different from updated data, Relay will apply server data ensuring consistency.

Without Optimistic

With Optimistic

optimisticResponse is an object that reflect your mutation output and needs to be passed to commitMutation.

Let's see a complete example:

In the code above, I'm updating the message status before it returns from server side.

I've made a repository to benchmark updating data with and without optimistic update.

The code and instructions can be found here.

I hope you enjoy it, thanks for your time.

Any troubles or doubts, send me a message. You can find me at GitHub and Twitter.


The Bleeding Edge Tech Team

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store