Relay provides the ability to extend the GraphQL schema on the client (i.e. in the browser), via client schema extensions, in order to model data that only needs to be created, read and updated on the client. This can be useful to add small pieces of information to data that is fetched from the server, or to entirely model client-specific state to be stored and managed by Relay.
Client schema extensions allows you to modify existing types on the schema (e.g. by adding new fields to a type), or to create entirely new types that only exist in the client.
In order to extend an existing type, add a
.graphql file to your appropriate source (
- In this example, we're using the
extendkeyword to extend an existing type, and we're adding a new field,
is_new_commentto the existing
Commenttype, which we will be able to read in our components, and update when necessary using normal Relay APIs; you might imagine that we might use this field to render a different visual treatment for a comment if it's new, and we might set it when creating a new comment.
You can define types using the same regular GraphQL syntax, by defining it inside a
.graphql file in
- In this contrived example, we're defining 2 new client-only types, and
enumand a regular
type. Note that they can reference themselves as normal, and reference regular server defined types. Also note that we can extend server types and add fields that are of our client-only types.
- As mentioned previously, we will be able to read and update this data normally via Relay APIs.
Is this page useful?