Short answer
nodeInterface ONLY used by GraphQL when the client side (usually the browser) requests a piece of GraphQL data, which is already at its disposal from the initial selection, for repeated selection.
Re-sampling is performed under any of these circumstances:
- Another / the same reaction component (on the same web page) requests additional pieces of data at a later stage and this data is not available in the source data
- The code in the React component executes
this.props.relay.forceFetch() , which usually happens if the programmer believes that the server-side data can be changed due to other external - The code in the React component executes
this.props.relay.setVariable(...) when the user performs an action, such as changing the number of displayed items, image size, etc., which requires a change in the source data.
During re-fetching, GraphQL calls nodeInterface with the global Node identifier, which nodeInterface should receive the server-side object associated with this Node identifier. Using this server object, the GraphQL server can then return additional / variations of the data required by the client.
Thus, a Node identifier is never required if the original data of the object is never corrected.
See https://medium.com/@khor/relay-graphql-de-mystifying-node-id-38757121b9c for details
More details
nodeInterface does a Node ID to resolve on the server side, so it always looks something like this:
var {nodeInterface, nodeField} = nodeDefinitions( (globalId) => { var {type, id} = fromGlobalId(globalId); if (type === 'ClassA') { // Get ClassA type of instance with id // Eg, return ClassA.find_by_id(id) } else if (type === 'ClassB') { return ClassB.find_by_id(id) } ... } )
Since the GraphQL types that need to be reassigned require a Node ID, in the GraphQL schema you need to:
- Request GraphQL to automatically generate Node identifier when instantiating GraphQL type using
globalIdField keyword - Tell GraphQL how this GraphQL type resolves the Node identifier provided during re-fetching to the appropriate server object, which is your
interface : [nodeInterface]
In this way:
var ClassAType = new GraphQLObjectType({ name: 'ClassA', description: 'A', fields: () => ({ id: globalIdField('ClassA'), ... other fields ... }), interfaces: [nodeInterface], });
source share