Relay
is the entry point to the Relay library. If you're using one of the prebuilt packages it's available as a global; if you're using CommonJS modules you can require()
it.
Note
The
react-relay
npm module includesreact
as a peer dependency. Your app should specify React as a dependency explicitly.
The most-used function is createContainer()
which wraps components with data declarations.
Properties
Methods
static createContainer(Component, ContainerConfig)Creates a Relay Container.
static injectNetworkLayer(networkLayer)Customize how queries and mutations are sent to the server.
static injectTaskScheduler(scheduler)Configure when Relay processing occurs.
static isContainer(Component)Determine if a given object is a Relay.Container.
See the Network Layer Guide.
See the Mutations Guide.
See the Relay.QL API reference.
See the PropTypes API reference.
See the RootContainer Guide.
See the Routes Guide.
See the Store API reference.
var Container = Relay.createContainer(Component, { initialVariables?: Object, prepareVariables?: (variables: Object, route: string) => Object, fragments: {[key: string]: Function} });
Creates a new Relay Container - see the Container Guide for more details and examples.
Relay.injectNetworkLayer(networkLayer: { sendMutation: (mutation: RelayMutationRequest) => void; sendQueries: (queries: Array<RelayQueryRequest>) => void; supports: (...options: Array<string>): boolean; });
Overrides the DefaultNetworkLayer.
As an example, we can log each mutation that is sent to the server as follows:
var DefaultNetworkLayer = Relay.DefaultNetworkLayer; class MutationLoggingNetworkLayer extends DefaultNetworkLayer { sendMutation(mutation) { // log the response or error (note that `mutation` is a promise) mutation.then( response => console.log(response), error => console.error(error), ); // Send the mutation using the default network implementation return super.sendMutation(mutation); } }; Relay.injectNetworkLayer(new MutationLoggingNetworkLayer());
Relay.injectTaskScheduler(scheduler: Scheduler): void; type Scheduler = (task: Function) => void;
Relay wraps its core processing functions inside lightweight tasks, which by default are executed immediately (i.e. synchronously). In order to customize when these tasks are run - for example to avoid interrupting an animation during a touch gesture - applications can provide a custom scheduling function.
The default implementation is as follows:
Relay.injectTaskScheduler(task => task());
Notice that it immediately executes the next task. Relay manages the order of tasks to ensure a proper order of operations - the scheduler can't skip or reorder tasks, only decide when to execute the next one.
In React Native, we can schedule Relay processing so as to avoid interrupting touch gestures as follows:
var {InteractionManager} = require('react-native'); Relay.injectTaskScheduler(InteractionManager.runAfterInteractions);
You can read more about InteractionManager
on the React Native API docs.
Relay.isContainer(Component: Object): boolean;
var Component = require('...'); if (Relay.isContainer(Component)) { Component.getFragment('...'); }