[1.0] Add new way to make connections between a node field and another node #902
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
One (of many) beautiful things about GraphQL is it makes it very easy to write queries which jump between multiple node types. But up to now, source plugins couldn't setup those relationships other than putting node IDs in the node "children" array. Which while nice isn't enough. Especially source plugins which are modeling complex data types.
This PR adds a new way for a source plugin author (or anyone extending a node type) to add connections between nodes.
In the simplest case where a field points to another node's id you do:
Now when Gatsby creates the GraphQL schema for these nodes, it'll see the field in
NodeB
ending with___NODE
and it'll try to create a connection between the nodes. It'll takesuper
and look up the node with that id (ids are globally unique). When it finds it, it makes the connection between the nodes so that you can then write graphql queries like:Which returns a JSON object looking like:
Fields ending with
___node
can have a value of a single node id or an array. If you use an array, all referenced nodes must be of the same type.But you don't always want to point to an id. Sometimes it's easier to point to a different field's value for whatever reason. In these cases, you can specify on the field the name of the foreign node's field you're referencing to e.g.
fieldReference___NODE___anotherField
. Warning, ensure this other field is globally unique as well as otherwise the wrong node could be returned.At some point in the future we might need to add a way source plugins to programmatically find the other node.