typealias SelectorStoreUpdater = (
	inout RecordSourceSelectorProxy,
	SelectorData?
) -> Void

Updater functions provide a hook for you to update Relay's local Store when a mutation completes. You can use this to make arbitrary complex changes to the store without refetching data from the server. This can make your app feel more responsive, especially when paired with optimistic updates.

Do you need an updater function?

If your mutation only changes some properties of an existing record, you probably don't need an updater. Relay will match records in the mutation's response to the records that already exist in the store and update their fields. If your mutation response includes the fields you expect to change on the record, they'll update in the store and your UI automatically.

For more complex changes that Relay can't infer automatically, you can use an updater function. Some situations where you would want to use an updater function:

How do updater functions work?

You provide an updater function with the updater or optimisticUpdater parameter when committing a mutation. Most of the time, can use the same updater function for both the optimistic and non-optimistic updaters. Updater functions are only run on a successful response; error responses will not run the updater.

When you commit a mutation, a few different actions will happen in order. The exact actions depend on which parameters you provide:

  1. If you provide an optimisticResponse, then the store will be updated as though it was the real response.
  2. If you provide an optimisticUpdater, then that function will be called with a proxy to the Relay store.
  3. Relay will wait for the response to the mutation from the server.
  4. Any changes applied optimistically will be rolled back, so you're starting from a clean slate.
  5. If the response is successful:
    1. The store will be updated with the record data from the response.
    2. If you provide an updater, then that function will be called with a proxy to the Relay store.

An updater function takes two parameters: