Relay Mutation Updater — Part 1: Basic Reading and Editing

Relay Modern is now here and its awesome. Mutations still have the ablity todo simple operation through configurations, described in the documentation here: https://facebook.github.io/relay/docs/mutations.html

However there is now a new ablity to define your own custom updater functions, which is awesome. The updater is a function you provide with a single argument the proxyStore.

For the following examples we going to use this mutation as our context with it returning the result, that follows it. (Result.json)

The Example mutation and Result.

We are now going to go through different things that you can do in the updater function.


Reading values off the Payload

First off we want to get the payload’s ProxyRecord, a ProxyRecord represents a Record (can be a record already in our store, a record in our payload or a new record we create in our updater function, among other things)

to get the payload’s ProxyRecord we are going to use proxyStore.getRootField(<RootFieldName>) in our case RootFieldName is “doSomething”. Once we get the payload we can get values off it.

Reading Values off the Relay Store

we are going to assume that the existing store looks like:

{
"viewer": {
"objectOne": {
"id": "1337",
"name": "Leet"
},
"objectTwo": {
"id": "123456",
"name": "Bob",
"description": "He is A builder"
}
}
}

As well as being able to read the values of the payload you can read values of the store. There are two main ways you can do this. If you have the node’s id you can use proxyStore.get(<id>) and if you just want to get the root you can use proxyStore.getRoot()


Now that we know how to read values from the store and payload, how do we go about editing the store.

Thats it for Part One: now you should know the basics of reading, editing and deleting of records in the store and payload. I will be writing a futures post on how to deal with more complex situation. like Conections – adding and removing from them, and creating new records out of thin air. As well as anything else suggested to me.

If you had any problems understanding any of this please let me know. If you want me to explain how todo anything else in the updaters let me know and I’ll write about it.