Skip to content

Instantly share code, notes, and snippets.

@jlengstorf
Created October 9, 2018 02:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlengstorf/e8e805d5a5579bc48ce58a937e32d53f to your computer and use it in GitHub Desktop.
Save jlengstorf/e8e805d5a5579bc48ce58a937e32d53f to your computer and use it in GitHub Desktop.
Creating node relationships in Gatsby.
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