Skip to content

Commit

Permalink
Add an example to the sourceNodes docs (#8585)
Browse files Browse the repository at this point in the history
* Add an example to the sourceNodes docs

* Formatting

* Pass a unique value in to createNodeId

* Remove stray const
  • Loading branch information
m-allanson authored Sep 27, 2018
1 parent 17b9dc2 commit bab08df
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions packages/gatsby/src/utils/api-node-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bab08df

Please sign in to comment.