Created
October 9, 2018 02:28
-
-
Save jlengstorf/e8e805d5a5579bc48ce58a937e32d53f to your computer and use it in GitHub Desktop.
Creating node relationships in Gatsby.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const createNodeHelpers = require('gatsby-node-helpers').default; | |
const { createNodeFactory } = createNodeHelpers({ | |
typePrefix: 'MyData' | |
}); | |
const FooNode = createNodeFactory('Foo'); | |
const BarNode = createNodeFactory('Bar'); | |
const foo = { id: 'foo', value: 'foo' }; | |
const bar = [{ id: 0, name: 'Bar1' }, { id: 1, name: 'Bar2' }]; | |
exports.sourceNodes = async ({ actions: { createNode } }) => { | |
const fooNode = FooNode(foo); | |
await Promise.all( | |
bar.map(async data => { | |
const barNode = BarNode(data); | |
barNode.foo___NODE = fooNode.id; | |
createNode(barNode); | |
}) | |
); | |
// Can’t create this node until all `barNode`s are created. | |
createNode(fooNode); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment