Relay uses routes to define entry points into a Relay application.
Note
Relay routes don't really implement any URL routing specific logic or work with History API. In the future we will maybe rename RelayRoute to be something more like RelayQueryRoots or RelayQueryConfig.
Properties
static paramDefinitions Declare the expected parameters. static prepareParams Declare additional parameters or conversion for parameters. static queries Declare the set of query roots. static routeName Declare the name of this route class. Methods
static paramDefinitions: {[param: string]: {required: boolean}}
Routes can declare a set of parameter names that are required to be supplied to the constructor. This is also a convenient place to document the set of valid parameters.
class ProfileRoute extends Relay.Route {
static paramDefinitions = {
userID: {required: true},
};
// ...
}
static prepareParams: ?(prevParams: {[prevParam: string]: mixed}) => {[param: string]: mixed};
Routes can use prepareParams to provide default parameters, or pass through, convert or suppress passed-in parameters.
class ProfileRoute extends Relay.Route {
static queries = {
viewer: () => Relay.QL`query { viewer }`
};
static prepareParams = (prevParams) => {
return {
// Pass base set of supplied params through:
...prevParams,
// Transform a param to meet internal requirements:
id: toGlobalId('Profile', prevParams.id),
// Provide a starting `limit` variable:
limit: 10,
}
}
// ...
}
static queries: {
[queryName: string]: () => Relay.QL`query { ... }`
};
Routes must declare a set of query roots using Relay.QL. These queries will automatically compose a matching fragment named queryName on the Relay container used with this route on a Relay.RootContainer.
class ProfileRoute extends Relay.Route {
static queries = {
user: () => Relay.QL`query { user(id: $userID) }`,
};
// ...
}
In this example the Route should be initialized with a userID which gets passed on to the query. That userID variable will automatically be passed down to the top-level container and can be used there if needed. Further the top-level RelayContainer is expected to have a user fragment with the fields to be queried.
static routeName: string
Routes must define a string name.
Create a route instance using the new keyword, optionally passing it some params.
var profileRoute = new ProfileRoute({userID: '123'});
© 2013–present Facebook Inc.
Licensed under the BSD License.
https://facebook.github.io/relay/docs/api-reference-relay-route.html