In Relay, what role does the node interface and global ID specification play?

I started with relay-starter-kit , and also worked my way through the Relay and GraphQL documentation. But there are many areas that are inexplicable and mysterious.

Seriously, I read a lot of documents around the world about all these things, but I could not find satisfactory explanations on the following issues:

What is this for? I put the log, but it is not even called at all:

 var {nodeInterface, nodeField} = nodeDefinitions( (globalId) => { var {type, id} = fromGlobalId(globalId); if (type === 'User') { return getUser(id); } else if (type === 'Widget') { return getWidget(id); } else { return null; } }, (obj) => { if (obj instanceof User) { return userType; } else if (obj instanceof Widget) { return widgetType; } else { return null; } } ); 

And what is the actual effect of this:

 interfaces: [nodeInterface], 

Perhaps this is due to what the node field does here:

 var queryType = new GraphQLObjectType({ name: 'Query', fields: () => ({ node: nodeField, // Add your own root fields here viewer: { type: userType, resolve: () => getViewer(), }, }), }); 

And what is the magic around the id field? What is globalIdField for?

I have an id in my database and I thought I could use it in my GraphQL objects:

Instead:

 id: globalIdField('User'), 

I want to use my database id:

 id: { type: GraphQLID, description: 'The identifier' }, 

But if I do, I get an error in the browser saying RelayQueryWriter: Could not find a type name for record '1' .

I can get rid of this error by adding __typename to my Relay Query component containers, but this seems to be wrong.

It would be great if you could give deeper insides and a better explanation here and improve the official documentation.

thank

+43
reactjs graphql relayjs graphql-js
Oct 28 '15 at 19:27
source share
1 answer

The Node root field, combined with globally unique identifiers, comes into play when Relay needs to restore an object. The callback happens when you call this.props.relay.forceFetch() or when you add fields to the query for an object whose global identifier is known because it is already partially retrieved.

In such cases, Relay will short-circuit the regular request and execute the request for the object (s) directly using its global identifier and the Node root call.

An example :

Suppose $showComments was false when this request was first resolved.

 query { viewer { stories(first: 10) { edges { node { id, comments(first: 10) @include(if: $showComments) { author, commentText } text, } } } } } 

This caused a selection for id and text for a number of stories whose identifiers are now known.

Imagine that at some point the $showComments variable became true . The relay will restore only the data it needs using the root Node field.

 query { node(id: "ABC123") { fragment on Story { comments(first: 10) { author, commentText } } } node(id: "DEF456") { fragment on Story { comments(first: 10) { author, commentText } } } node(id: "GHI789") { fragment on Story { comments(first: 10) { author, commentText } } } ... } 

It depends on several parts:

  • Each object must have a globally unique identifier or be identified by a type / identifier pair (the globalIdField does this and creates a base64 encoded string).
  • The server must know how to resolve an object from a unique global identifier, and vice versa. This is what nodeDefinitions for.
  • Any object that hopes to be reconnected using this system must implement nodeInterface .

See also: https://facebook.imtqy.com/relay/docs/graphql-object-identification.html#content

+51
Oct 29 '15 at 10:15
source share



All Articles