Isomorphic React Relay 
Enables server-side rendering of React Relay containers.
If you use react-router-relay
you might also become interested in
isomorphic-relay-router.
Acknowledgments
Thank you to everyone who helped in the development of this project with suggestions,
testing, reported issues, pull-requests. Thank you to the Facebook employees who reviewed
my contributions
to Relay, which helped to improve the server-side rendering support.
Installation
npm install --save isomorphic-relay
How to Use
Here is an example with detailed comments of how isomorphic-relay
can be used on the server:
import IsomorphicRelay from 'isomorphic-relay';
const rootContainerProps = {
Container: MyContainer,
queryConfig: new MyRoute(),
};
app.get('/', (req, res, next) => {
const networkLayer = new Relay.DefaultNetworkLayer(
'http://localhost:8080/graphql',
{ headers: { cookie: req.headers.cookie } },
);
IsomorphicRelay.prepareData(rootContainerProps, networkLayer).then(({ data, props }) => {
const reactOutput = ReactDOMServer.renderToString(
<IsomorphicRelay.Renderer {...props} />
);
res.render('index.ejs', {
preloadedData: JSON.stringify(data),
reactOutput
});
}).catch(next);
});
And here is an example of the code that can be used in the browser:
import IsomorphicRelay from 'isomorphic-relay';
const environment = new Relay.Environment();
environment.injectNetworkLayer(new Relay.DefaultNetworkLayer('/graphql'));
const data = JSON.parse(document.getElementById('preloadedData').textContent);
IsomorphicRelay.injectPreparedData(environment, data);
IsomorphicRelay.prepareInitialRender({ ...rootContainerProps, environment }).then(props => {
ReactDOM.render(<IsomorphicRelay.Renderer {...props} />, document.getElementById('root'));
});
Also see the Star Wars example.