Linking Data In Gatsby
The possible ways can be generally grouped by if you have some control of the nodes or if they are made for you.
When Nodes Are Made For You
- mapping nodes, probably the fastest and most approachable, but requires your data at each node to “match”
- using
createParentChildLinktypically in theonCreateNodelifecycle- The
onCreateNodelifecycle fires every time a new node is created. You can use this to link a node to another node.
- The
When You Have Control Of The Nodes
- Setting up the relationship with
___NODEappended to your node name. - A plain value and let Gatsby infer (guess) the type of node (generally for
Filenodes) - Creating a new type and using a resolver to “manually” query and link them
- This is likely the most involved. You will need to use
onCreateNodeto create “linking” nodes. - The “linking” nodes will point to your custom type. The type can use the
createResolversthat let you code the way the node is “inferred”. - You can see an example of this in my
gastby-theme-recipes.
- This is likely the most involved. You will need to use