Skip to content

Commit

Permalink
Replace 'updateNode' action creator with 'addChildNodeToParentNode' a…
Browse files Browse the repository at this point in the history
…nd 'addFieldToNode'
  • Loading branch information
KyleAMathews committed May 24, 2017
1 parent cdf2539 commit f9faf17
Show file tree
Hide file tree
Showing 21 changed files with 426 additions and 149 deletions.
5 changes: 5 additions & 0 deletions docs/docs/node-interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ pluginFields: Object,
internal: {
contentDigest: String,
mediaType: String,
// A globally unique node type choosen by the plugin owner.
type: String,
// The plugin which created this node.
pluginOwner: String,
// Stores which plugins created which fields.
fieldPluginOwners: Object,
// Raw content for this node.
content: String,
}
...other node type specific fields
Expand Down
16 changes: 8 additions & 8 deletions packages/gatsby-transformer-json/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ describe(`Process JSON nodes correctly`, () => {
node.content = JSON.stringify(data)

const createNode = jest.fn()
const updateNode = jest.fn()
const boundActionCreators = { createNode, updateNode }
const addChildNodeToParentNode = jest.fn()
const boundActionCreators = { createNode, addChildNodeToParentNode }

await onNodeCreate({
node,
loadNodeContent,
boundActionCreators,
}).then(() => {
expect(createNode.mock.calls).toMatchSnapshot()
expect(updateNode.mock.calls).toMatchSnapshot()
expect(addChildNodeToParentNode.mock.calls).toMatchSnapshot()
expect(createNode).toHaveBeenCalledTimes(2)
expect(updateNode).toHaveBeenCalledTimes(1)
expect(addChildNodeToParentNode).toHaveBeenCalledTimes(1)
})
})

Expand All @@ -49,8 +49,8 @@ describe(`Process JSON nodes correctly`, () => {
node.content = JSON.stringify(data)

const createNode = jest.fn()
const updateNode = jest.fn()
const boundActionCreators = { createNode, updateNode }
const addChildNodeToParentNode = jest.fn()
const boundActionCreators = { createNode, addChildNodeToParentNode }

await onNodeCreate({
node,
Expand All @@ -71,8 +71,8 @@ describe(`Process JSON nodes correctly`, () => {
node.content = JSON.stringify(data)

const createNode = jest.fn()
const updateNode = jest.fn()
const boundActionCreators = { createNode, updateNode }
const addChildNodeToParentNode = jest.fn()
const boundActionCreators = { createNode, addChildNodeToParentNode }

await onNodeCreate({
node,
Expand Down
9 changes: 5 additions & 4 deletions packages/gatsby-transformer-json/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const _ = require(`lodash`)
const crypto = require(`crypto`)

async function onNodeCreate({ node, boundActionCreators, loadNodeContent }) {
const { createNode, updateNode } = boundActionCreators
const { createNode, addChildNodeToParentNode } = boundActionCreators

// Don't reprocess our own nodes! (note: this doesn't normally happen
// but since this transformer creates new nodes with the same media-type
Expand Down Expand Up @@ -49,9 +49,10 @@ async function onNodeCreate({ node, boundActionCreators, loadNodeContent }) {
}
})

node.children = node.children.concat(JSONArray.map(n => n.id))
updateNode(node)
_.each(JSONArray, j => createNode(j))
_.each(JSONArray, j => {
createNode(j)
addChildNodeToParentNode({ parent: node, child: j })
})
}

return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe(`transformer-react-doc-gen: onNodeCreate`, () => {
loadNodeContent = jest.fn(node => readFile(node.__fixture))
boundActionCreators = {
createNode: jest.fn(n => createdNodes.push(n)),
updateNode: jest.fn(n => updatedNodes.push(n)),
addChildNodeToParentNode: jest.fn(n => updatedNodes.push(n)),
}
})

Expand Down
32 changes: 20 additions & 12 deletions packages/gatsby-transformer-react-docgen/src/on-node-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const propsId = (parentId, name) => `${parentId}--ComponentProp-${name}`
const descId = parentId => `${parentId}--ComponentDescription`

function createDescriptionNode(node, entry, boundActionCreators) {
if (!entry.description) return
const { createNode, updateNode } = boundActionCreators
if (!entry.description) return node
const { createNode } = boundActionCreators

const descriptionNode = {
id: descId(node.id),
Expand All @@ -25,8 +25,9 @@ function createDescriptionNode(node, entry, boundActionCreators) {

node.description___NODE = descriptionNode.id
node.children = node.children.concat([descriptionNode.id])
updateNode(node)
createNode(descriptionNode)

return node
}

function createPropNodes(node, component, boundActionCreators) {
Expand All @@ -37,7 +38,7 @@ function createPropNodes(node, component, boundActionCreators) {
let propNodeId = propsId(node.id, prop.name)
let content = JSON.stringify(prop)

const propNode = {
let propNode = {
...prop,
id: propNodeId,
children: [],
Expand All @@ -51,20 +52,20 @@ function createPropNodes(node, component, boundActionCreators) {
},
}
children[i] = propNode.id
propNode = createDescriptionNode(propNode, prop, boundActionCreators)
createNode(propNode)
createDescriptionNode(propNode, prop, boundActionCreators)
})

node.props___NODE = children
node.children = node.children.concat(children)
updateNode(node)
return node
}

export default function onNodeCreate(
{ node, loadNodeContent, boundActionCreators },
pluginOptions
) {
const { createNode, updateNode } = boundActionCreators
const { createNode, addChildNodeToParentNode } = boundActionCreators

if (node.internal.mediaType !== `application/javascript`) return null

Expand All @@ -77,7 +78,7 @@ export default function onNodeCreate(
const contentDigest = digest(strContent)
const nodeId = `${node.id}--${component.displayName}--ComponentMetadata`

const metadataNode = {
let metadataNode = {
...component,
props: null, // handled by the prop node creation
id: nodeId,
Expand All @@ -91,11 +92,18 @@ export default function onNodeCreate(
},
}

node.children = node.children.concat([metadataNode.id])
updateNode(node)
addChildNodeToParentNode({ parent: node, child: metadataNode })
metadataNode = createPropNodes(
metadataNode,
component,
boundActionCreators
)
metadataNode = createDescriptionNode(
metadataNode,
component,
boundActionCreators
)
createNode(metadataNode)
createPropNodes(metadataNode, component, boundActionCreators)
createDescriptionNode(metadataNode, component, boundActionCreators)
})
})
.catch(err => console.log(err))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ Where oh where is my little pony?
node.content = content

const createNode = jest.fn()
const updateNode = jest.fn()
const boundActionCreators = { createNode, updateNode }
const addChildNodeToParentNode = jest.fn()
const boundActionCreators = { createNode, addChildNodeToParentNode }

await onNodeCreate({
node,
loadNodeContent,
boundActionCreators,
}).then(() => {
expect(createNode.mock.calls).toMatchSnapshot()
expect(updateNode.mock.calls).toMatchSnapshot()
expect(addChildNodeToParentNode.mock.calls).toMatchSnapshot()
expect(createNode).toHaveBeenCalledTimes(1)
expect(updateNode).toHaveBeenCalledTimes(1)
expect(addChildNodeToParentNode).toHaveBeenCalledTimes(1)
})
})
})
5 changes: 2 additions & 3 deletions packages/gatsby-transformer-remark/src/on-node-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = async function onNodeCreate({
loadNodeContent,
boundActionCreators,
}) {
const { createNode, updateNode } = boundActionCreators
const { createNode, addChildNodeToParentNode } = boundActionCreators

// Don't reprocess our own nodes! (note: this doesn't normally happen
// but since this transformer creates new nodes with the same media-type
Expand Down Expand Up @@ -50,7 +50,6 @@ module.exports = async function onNodeCreate({
markdownNode.fileAbsolutePath = node.absolutePath
}

node.children = node.children.concat([markdownNode.id])
updateNode(node)
createNode(markdownNode)
addChildNodeToParentNode({ parent: node, child: markdownNode })
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ describe(`Process image nodes correctly`, () => {
},
}
const createNode = jest.fn()
const updateNode = jest.fn()
const boundActionCreators = { createNode, updateNode }
const addChildNodeToParentNode = jest.fn()
const boundActionCreators = { createNode, addChildNodeToParentNode }

await onNodeCreate({
node,
boundActionCreators,
}).then(() => {
expect(createNode.mock.calls).toMatchSnapshot()
expect(updateNode.mock.calls).toMatchSnapshot()
expect(addChildNodeToParentNode.mock.calls).toMatchSnapshot()
expect(createNode).toHaveBeenCalledTimes(1)
expect(updateNode).toHaveBeenCalledTimes(1)
expect(addChildNodeToParentNode).toHaveBeenCalledTimes(1)
})
})
})
5 changes: 2 additions & 3 deletions packages/gatsby-transformer-sharp/src/on-node-create.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const _ = require(`lodash`)

module.exports = async function onNodeCreate({ node, boundActionCreators }) {
const { createNode, updateNode } = boundActionCreators
const { createNode, addChildNodeToParentNode } = boundActionCreators

const extensions = [`jpeg`, `jpg`, `png`, `webp`, `tif`, `tiff`, `svg`]
if (!_.includes(extensions, node.extension)) {
Expand All @@ -19,9 +19,8 @@ module.exports = async function onNodeCreate({ node, boundActionCreators }) {
},
}

node.children = node.children.concat([imageNode.id])
updateNode(node)
createNode(imageNode)
addChildNodeToParentNode({ parent: node, child: imageNode })

return
}
16 changes: 8 additions & 8 deletions packages/gatsby-transformer-yaml/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ describe(`Process YAML nodes correctly`, () => {
node.content = yaml.safeDump(data)

const createNode = jest.fn()
const updateNode = jest.fn()
const boundActionCreators = { createNode, updateNode }
const addChildNodeToParentNode = jest.fn()
const boundActionCreators = { createNode, addChildNodeToParentNode }

await onNodeCreate({
node,
loadNodeContent,
boundActionCreators,
}).then(() => {
expect(createNode.mock.calls).toMatchSnapshot()
expect(updateNode.mock.calls).toMatchSnapshot()
expect(addChildNodeToParentNode.mock.calls).toMatchSnapshot()
expect(createNode).toHaveBeenCalledTimes(2)
expect(updateNode).toHaveBeenCalledTimes(1)
expect(addChildNodeToParentNode).toHaveBeenCalledTimes(1)
})
})

Expand All @@ -47,8 +47,8 @@ describe(`Process YAML nodes correctly`, () => {
node.content = yaml.safeDump(data)

const createNode = jest.fn()
const updateNode = jest.fn()
const boundActionCreators = { createNode, updateNode }
const addChildNodeToParentNode = jest.fn()
const boundActionCreators = { createNode, addChildNodeToParentNode }

await onNodeCreate({
node,
Expand All @@ -69,8 +69,8 @@ describe(`Process YAML nodes correctly`, () => {
node.content = yaml.safeDump(data)

const createNode = jest.fn()
const updateNode = jest.fn()
const boundActionCreators = { createNode, updateNode }
const addChildNodeToParentNode = jest.fn()
const boundActionCreators = { createNode, addChildNodeToParentNode }

await onNodeCreate({
node,
Expand Down
9 changes: 5 additions & 4 deletions packages/gatsby-transformer-yaml/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const _ = require(`lodash`)
const crypto = require(`crypto`)

async function onNodeCreate({ node, boundActionCreators, loadNodeContent }) {
const { createNode, updateNode } = boundActionCreators
const { createNode, addChildNodeToParentNode } = boundActionCreators
if (node.internal.mediaType !== `text/yaml`) {
return
}
Expand Down Expand Up @@ -40,9 +40,10 @@ async function onNodeCreate({ node, boundActionCreators, loadNodeContent }) {
}
})

node.children = node.children.concat(yamlArray.map(y => y.id))
updateNode(node)
_.each(yamlArray, y => createNode(y))
_.each(yamlArray, y => {
createNode(y)
addChildNodeToParentNode({ parent: node, child: y })
})
}

return
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/src/joi-schemas/joi.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const nodeSchema = Joi.object()
mediaType: Joi.string().required(),
type: Joi.string().required(),
pluginOwner: Joi.string().required(),
fieldPluginOwners: Joi.array(),
content: Joi.string(),
}),
})
Expand Down
Loading

0 comments on commit f9faf17

Please sign in to comment.