This blog site has been archived. Go toreact.dev/blog to see the recent posts.
At React.js Conf in January we gave a preview of Relay, a new framework for building data-driven applications in React. In this post, we’ll describe the process of creating a Relay application. This post assumes some familiarity with the concepts of Relay and GraphQL, so if you haven’t already we recommend readingour introductory blog post or watchingthe conference talk.
We’re working hard to prepare GraphQL and Relay for public release. In the meantime, we’ll continue to provide information about what you can expect.
The diagram below shows the main parts of the Relay architecture on the client and the server:
The main pieces are as follows:
This post will focus onRelay components that describe encapsulated units of UI and their data dependencies. These components form the majority of a Relay application.
To see how components work and can be composed, let’s implement a basic version of the Facebook News Feed in Relay. Our application will have two components: a<NewsFeed>
that renders a list of<Story>
items. We’ll introduce the plain React version of each component first and then convert it to a Relay component. The goal is something like the following:
<Story>
BeginsThe first step is a React<Story>
component that accepts astory
prop with the story’s text and author information. Note that all examples uses ES6 syntax and elide presentation details to focus on the pattern of data access.
// Story.react.jsexportdefaultclassStoryextendsReact.Component{render(){var story=this.props.story;return(<View><Imageuri={story.author.profilePicture.uri}/><Text>{story.author.name}</Text><Text>{story.text}</Text></View>);}}
<Story>
?Relay automates the process of fetching data for components by wrapping existing React components in Relay containers (themselves React components):
// Story.react.jsclassStoryextendsReact.Component{...}exportdefault Relay.createContainer(Story,{fragments:{story:/* TODO */}});
Before adding the GraphQL fragment, let’s look at the component hierarchy this creates:
Most props will be passed through from the container to the original component. However, Relay will return the query results for a prop whenever a fragment is defined. In this case we’ll add a GraphQL fragment forstory
:
// Story.react.jsclassStoryextendsReact.Component{...}exportdefault Relay.createContainer(Story,{fragments:{story:()=> Relay.QL` fragment on Story { author { name profilePicture { uri } } text }`,},});
Queries use ES6 template literals tagged with theRelay.QL
function. Similar to how JSX transpiles to plain JavaScript objects and function calls, these template literals transpile to plain objects that describe fragments. Note that the fragment’s structure closely matches the object structure that we expected in<Story>
’s render function.
<Story>
s on DemandWe can render a Relay component by providing Relay with the component (<Story>
) and the ID of the data (a story ID). Given this information, Relay will first fetch the results of the query and thenrender()
the component. The value ofprops.story
will be a plain JavaScript object such as the following:
{author:{name:"Greg",profilePicture:{uri:"https://…"}},text:"The first Relay blog post is up…"}
Relay guarantees that all data required to render a component will be available before it is rendered. This means that<Story>
does not need to handle a loading state; thestory
isguaranteed to be available beforerender()
is called. We have found that this invariant simplifies our application codeand improves the user experience. Of course, Relay also has options to delay the fetching of some parts of our queries.
The diagram below shows how Relay containers make data available to our React components:
<NewsFeed>
WorthyNow that the<Story>
is over we can continue with the<NewsFeed>
component. Again, we’ll start with a React version:
// NewsFeed.react.jsclassNewsFeedextendsReact.Component{render(){var stories=this.props.viewer.stories;// `viewer` is the active userreturn(<View>{stories.map(story=><Storystory={story}/>)}<ButtononClick={()=>this.loadMore()}>Load More</Button></View>);}loadMore(){// TODO: fetch more stories}}module.exports= NewsFeed;
<NewsFeed>
has two new requirements: it composes<Story>
and requests more data at runtime.
Just as React views can be nested, Relay components can compose query fragments from child components. Composition in GraphQL uses ES6 template literal substitution:${Component.getFragment('prop')}
. Pagination can be accomplished with a variable, specified with$variable
(as instories(first: $count)
):
// NewsFeed.react.jsclassNewsFeedextendsReact.Component{...}exportdefault Relay.createContainer(NewsFeed,{initialVariables:{count:3/* default to 3 stories */},fragments:{viewer:()=> Relay.QL` fragment on Viewer { stories(first: $count) { /* fetch viewer's stories */ edges { /* traverse the graph */ node {${Story.getFragment('story')} /* compose child fragment */ } } } }`,},});
Whenever<NewsFeed>
is rendered, Relay will recursively expand all the composed fragments and fetch the queries in a single trip to the server. In this case, thetext
andauthor
data will be fetched for each of the 3 story nodes.
Query variables are available to components asprops.relay.variables
and can be modified withprops.relay.setVariables(nextVariables)
. We can use these to implement pagination:
// NewsFeed.react.jsclassNewsFeedextendsReact.Component{render(){...}loadMore(){// read current paramsvar count=this.props.relay.variables.count;// update paramsthis.props.relay.setVariables({count: count+5,});}}
Now whenloadMore()
is called, Relay will send a GraphQL request for the additional five stories. When these stories are fetched, the component will re-render with the new stories available inprops.viewer.stories
and the updated count reflected inprops.relay.variables.count
.
These two components form a solid core for our application. With the use of Relay containers and GraphQL queries, we’ve enabled the following benefits:
<Story>
can be used on its own or within<NewsFeed>
, without any changes to either component.But Relay has many more tricks up its sleeve. For example, it’s built from the start to handle reads and writes, allowing for features like optimistic client updates with transactional rollback. Relay can also defer fetching select parts of queries, and it uses a local data store to avoid fetching the same data twice. These are all powerful features that we hope to explore in future posts.