Query with Relay and GraphQL

I followed the tutorial for React + Relay + GraphQL @ https://facebook.imtqy.com/relay/docs/tutorial.html

And I was confused by what interface: [nodeInterface] does in the definition of a GraphQL object.

+6
source share
2 answers

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], }); 
+7
source

For a GraphQL server compatible with relays, an object with a global identifier is called Node , any Node can be refetch on request

 { node: (id: "some global id") { id, ... on SomeType { someField } } } 
Interface

Node used for this in the GraphGL scheme, which helps you determine the type with a global identifier.

Check Global Object Identification and Specification Information

+5
source

All Articles