Skip to content

Commit

Permalink
Add _PARENT automatically to all sub-objects in nodes fixes #2567 (#2654
Browse files Browse the repository at this point in the history
)

* Add _PARENT automatically to all sub-objects in nodes fixes #2567

* Fix tests
  • Loading branch information
KyleAMathews committed Oct 27, 2017
1 parent 0e2d8ee commit 656299d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
17 changes: 0 additions & 17 deletions packages/gatsby-transformer-remark/src/on-node-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,6 @@ module.exports = async function onCreateNode({
},
}

// Add _PARENT recursively to sub-objects in the frontmatter so we can
// use this to find the root markdown node when running GraphQL
// queries. Yes this is lame. But it's because in GraphQL child nodes
// can't access their parent nodes so we use this _PARENT convention
// to get around this.
const addParentToSubObjects = data => {
_.each(data, (v, k) => {
if (_.isArray(v) && _.isObject(v[0])) {
_.each(v, o => addParentToSubObjects(o))
} else if (_.isObject(v)) {
addParentToSubObjects(v)
}
})
data._PARENT = node.id
}
addParentToSubObjects(data.data)

markdownNode.frontmatter = {
title: ``, // always include a title
...data.data,
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby/src/redux/__tests__/nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ describe(`Create and update nodes`, () => {
let state = nodeReducer(undefined, action)
state = nodeReducer(state, updateAction)
expect(state[`hi`].pickle).toEqual(false)
expect(state[`hi`].deep).toEqual({ array: [1, 2] })
expect(state[`hi`].deep2).toEqual({ boom: `foo` })
expect(state[`hi`].deep.array[0]).toEqual(1)
expect(state[`hi`].deep2.boom).toEqual(`foo`)
})

it(`allows deleting nodes`, () => {
Expand Down
27 changes: 27 additions & 0 deletions packages/gatsby/src/redux/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,33 @@ actions.createNode = (node, plugin, traceId) => {
)
}

// Add _PARENT recursively to sub-objects in nodes so we can use this to find
// the root node when running GraphQL queries. Yes this is lame. But it's
// because in GraphQL child nodes can't access their parent nodes so we use
// this _PARENT convention to get around this.
const addParentToSubObjects = (data, parentId) => {
_.each(data, (v, k) => {
if (_.isArray(v) && _.isObject(v[0])) {
_.each(v, o => addParentToSubObjects(o, parentId))
} else if (_.isObject(v)) {
addParentToSubObjects(v, parentId)
}
})
data._PARENT = parentId
}

_.each(node, (v, k) => {
// Ignore the node internal object.
if (k === `internal`) {
return
}
if (_.isArray(v) && _.isObject(v[0])) {
_.each(v, o => addParentToSubObjects(o, node.parent))
} else if (_.isObject(v)) {
addParentToSubObjects(v, node.parent)
}
})

// Ensure the plugin isn't creating a node type owned by another
// plugin. Type "ownership" is first come first served.
if (!typeOwners[node.internal.type] && plugin) {
Expand Down

0 comments on commit 656299d

Please sign in to comment.