Relay can be used to read and write local data, and act as a single source of truth for all data in your client application. The Relay Compiler fully supports client-side extensions of the schema, which allows you to define local fields and types.
Table of Contents:
To extend the server schema, create a new
.graphql file inside your
Let's call it
This schema describes what local data can be queried on the client. It can even be used to extend an existing server schema.
For example, we can create a new type called
And then extend the server schema type
User, with a list of
Accessing local data is no different from querying your GraphQL server, although you are required to include atleast one server field in the query.
The field can be from the server schema, or it can be schema agnostic, like an introspection field (i.e.
Here, we use a QueryRenderer to get the current
User via the
viewer field, along with their id, name and the local list of notes.
All local data lives in the Relay Store.
Updating local state can be done with any
commitLocalUpdate function is especially ideal for this, because writes to local state are usually executed outside of a mutation.
To build upon the previous example, let's try creating, updating and deleting a
Note from the list of
All new client-side schema fields default to
undefined value. Often however, you will want to set the initial state before querying local data.
You can use an updater function via
commitLocalUpdate to prime local state.