From bab08dfc68ffca85a8b1e34b6bc09cc0accecee2 Mon Sep 17 00:00:00 2001 From: Mike Allanson Date: Thu, 27 Sep 2018 15:58:25 +0100 Subject: [PATCH] Add an example to the sourceNodes docs (#8585) * Add an example to the sourceNodes docs * Formatting * Pass a unique value in to createNodeId * Remove stray const --- packages/gatsby/src/utils/api-node-docs.js | 33 ++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/packages/gatsby/src/utils/api-node-docs.js b/packages/gatsby/src/utils/api-node-docs.js index 46c15fe53981d..cfd449008c66a 100644 --- a/packages/gatsby/src/utils/api-node-docs.js +++ b/packages/gatsby/src/utils/api-node-docs.js @@ -82,9 +82,38 @@ exports.createPagesStatefully = true * * See also the documentation for [`createNode`](/docs/actions/#createNode). * @example - * exports.sourceNodes = ({ actions }) => { + * const crypto = require(`crypto`) + * + * exports.sourceNodes = ({ actions, createNodeId }) => { * const { createNode } = actions - * // Create nodes here. + * + * // Data can come from anywhere, but for now create it manually + * const myData = { + * key: 123, + * foo: `The foo field of my node`, + * bar: `Baz` + * } + * + * const nodeContent = JSON.stringify(myData) + * const nodeContentDigest = crypto + * .createHash(`md5`) + * .update(nodeContent) + * .digest(`hex`) + * + * const nodeMeta = { + * id: createNodeId(`my-data-${myData.key}`), + * parent: null, + * children: [], + * internal: { + * type: `MyNodeType`, + * mediaType: `text/html`, + * content: nodeContent, + * contentDigest: nodeContentDigest + * } + * } + * + * const node = Object.assign({}, myData, nodeMeta) + * createNode(node) * } */ exports.sourceNodes = true