Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.0] Make nodes fully immutable by adding API for allowing plugins to add fields #1035

Merged
merged 17 commits into from
May 26, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
remove updateNode action creator, rename 'pluginName' to 'pluginOwner…
…', and add check that only original node creator is updating a node
  • Loading branch information
KyleAMathews committed May 24, 2017
commit 449173e1b824c8e1bc3966a561a356060652addd
2 changes: 1 addition & 1 deletion docs/docs/node-interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal: {
contentDigest: String,
mediaType: String,
type: String,
pluginName: String,
pluginOwner: String,
content: String,
}
...other node type specific fields
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-transformer-json/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async function onNodeCreate({ node, boundActionCreators, loadNodeContent }) {
// but since this transformer creates new nodes with the same media-type
// as its parent node, we have to add this check that we didn't create
// the node).
if (node.internal.pluginName === `gatsby-transformer-json`) {
if (node.internal.pluginOwner === `gatsby-transformer-json`) {
return
}

Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/joi-schemas/joi.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const nodeSchema = Joi.object()
contentDigest: Joi.string().required(),
mediaType: Joi.string().required(),
type: Joi.string().required(),
pluginName: Joi.string().required(),
pluginOwner: Joi.string().required(),
content: Joi.string(),
}),
})
Expand Down
22 changes: 13 additions & 9 deletions packages/gatsby/src/redux/__tests__/__snapshots__/nodes.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ exports[`Create and update nodes allows creating nodes 1`] = `
Object {
"payload": Object {
"children": Array [],
"contentDigest": "hasdfljds",
"id": "hi",
"mediaType": "test",
"internal": Object {
"contentDigest": "hasdfljds",
"mediaType": "test",
"pluginOwner": "tests",
"type": "Test",
},
"parent": "test",
"pickle": true,
"pluginName": "tests",
"type": "Test",
},
"plugin": "",
"plugin": undefined,
"type": "CREATE_NODE",
}
`;
Expand All @@ -21,13 +23,15 @@ exports[`Create and update nodes allows creating nodes 2`] = `
Object {
"hi": Object {
"children": Array [],
"contentDigest": "hasdfljds",
"id": "hi",
"mediaType": "test",
"internal": Object {
"contentDigest": "hasdfljds",
"mediaType": "test",
"pluginOwner": "tests",
"type": "Test",
},
"parent": "test",
"pickle": true,
"pluginName": "tests",
"type": "Test",
},
}
`;
56 changes: 24 additions & 32 deletions packages/gatsby/src/redux/__tests__/nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,17 @@ const nodeReducer = require(`../reducers/nodes`)
const nodeTouchedReducer = require(`../reducers/nodes-touched`)

describe(`Create and update nodes`, () => {
// TODO add these back when we stop directly consoleing errors.
// Right now makes tests noisy.
//
// it(`validates created nodes`, () => {
// const action = actions.createNode({
// type: `Test`,
// });
// expect(action.type).toEqual(`VALIDATION_ERROR`);
// });
// it(`validates updated nodes`, () => {
// const action = actions.updateNode({
// type: `Test`,
// });
// expect(action.type).toEqual(`VALIDATION_ERROR`);
// });

it(`allows creating nodes`, () => {
const action = actions.createNode({
id: `hi`,
contentDigest: `hasdfljds`,
children: [],
parent: `test`,
mediaType: `test`,
pluginName: `tests`,
type: `Test`,
internal: {
contentDigest: `hasdfljds`,
mediaType: `test`,
pluginOwner: `tests`,
type: `Test`,
},
pickle: true,
})
expect(action).toMatchSnapshot()
Expand All @@ -37,25 +23,29 @@ describe(`Create and update nodes`, () => {
it(`allows updating nodes`, () => {
const action = actions.createNode({
id: `hi`,
contentDigest: `hasdfljds`,
children: [],
parent: `test`,
mediaType: `test`,
pluginName: `tests`,
type: `Test`,
internal: {
contentDigest: `hasdfljds`,
mediaType: `test`,
pluginOwner: `tests`,
type: `Test`,
},
pickle: true,
deep: {
array: [0, 1, { boom: true }],
},
})
const updateAction = actions.createNode({
id: `hi`,
contentDigest: `hasdfljds`,
children: [],
parent: `test`,
mediaType: `test`,
pluginName: `tests`,
type: `Test`,
internal: {
contentDigest: `hasdfljds`,
mediaType: `test`,
pluginOwner: `tests`,
type: `Test`,
},
pickle: false,
deep: {
array: [1, 2],
Expand All @@ -74,12 +64,14 @@ describe(`Create and update nodes`, () => {
it(`nodes that are added are also "touched"`, () => {
const action = actions.createNode({
id: `hi`,
contentDigest: `hasdfljds`,
children: [],
parent: `test`,
mediaType: `test`,
pluginName: `tests`,
type: `Test`,
internal: {
contentDigest: `hasdfljds`,
mediaType: `test`,
pluginOwner: `tests`,
type: `Test`,
},
pickle: true,
})
let state = nodeTouchedReducer(undefined, action)
Expand Down
60 changes: 32 additions & 28 deletions packages/gatsby/src/redux/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,6 @@ actions.upsertPage = (page, plugin = ``) => {
}
}

actions.updateNode = (node, plugin = ``) => {
if (!_.isObject(node)) {
return console.log(
chalk.bold.red(
`The node passed to the "updateNode" action creator must be an object`
)
)
}
const result = Joi.validate(node, joiSchemas.nodeSchema)
if (result.error) {
console.log(chalk.bold.red(`The updated node didn't pass validation`))
console.log(chalk.bold.red(result.error))
console.log(node)
return { type: `VALIDATION_ERROR`, error: true }
}

return {
type: `UPDATE_NODE`,
plugin,
payload: node,
}
}

actions.deleteNode = (nodeId, plugin = ``) => {
return {
type: `DELETE_NODE`,
Expand All @@ -99,7 +76,7 @@ actions.touchNode = (nodeId, plugin = ``) => {
}
}

actions.createNode = (node, plugin = ``) => {
actions.createNode = (node, plugin) => {
if (!_.isObject(node)) {
return console.log(
chalk.bold.red(
Expand All @@ -115,11 +92,38 @@ actions.createNode = (node, plugin = ``) => {
return { type: `VALIDATION_ERROR`, error: true }
}

// Ensure the new node has an internals object.
if (!node.internal) {
node.internal = {}
}

// Add the plugin name to the internal object.
if (plugin) {
node.internal.pluginOwner = plugin.name
}

const oldNode = getNode(node.id)

// If the node has been created in the past, check that
// the current plugin is the same as the previous.
if (oldNode && oldNode.internal.pluginOwner !== plugin.name) {
throw new Error(
`Nodes can only be updated by their owner. Node ${node.id} is
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another one

owned by ${oldNode.internal.pluginOwner} and another plugin ${plugin.name}
tried to update it.

Node:

${JSON.stringify(node, null, 4)}

Plugin that tried to update the node:
${JSON.stringify(plugin, null, 4)}
`
)
}

// Check if the node has already been processed.
if (
getNode(node.id) &&
!hasNodeChanged(node.id, node.internal.contentDigest)
) {
if (oldNode && !hasNodeChanged(node.id, node.internal.contentDigest)) {
return {
type: `TOUCH_NODE`,
plugin,
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/redux/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ exports.loadNodeContent = node => {
// Load plugin's loader function
const plugin = store
.getState()
.flattenedPlugins.find(plug => plug.name === node.internal.pluginName)
.flattenedPlugins.find(plug => plug.name === node.internal.pluginOwner)
const { loadNodeContent } = require(plugin.resolve)
if (!loadNodeContent) {
throw new Error(
Expand Down
7 changes: 0 additions & 7 deletions packages/gatsby/src/redux/reducers/nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ module.exports = (state = {}, action) => {
}
return newState

case `UPDATE_NODE`:
newState = {
...state,
[action.payload.id]: action.payload,
}
return newState

case `DELETE_NODE`:
newState = _.omit(state, action.payload)
return newState
Expand Down
5 changes: 0 additions & 5 deletions packages/gatsby/src/utils/api-runner-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ const doubleBind = (boundActionCreators, plugin) => {
const boundActionCreator = boundActionCreators[key]
if (typeof boundActionCreator === `function`) {
doubleBoundActionCreators[key] = (...args) => {
// Automatically add to newly created nodes
// the plugin's name
if (key === `createNode`) {
args[0].internal.pluginName = plugin.name
}
return boundActionCreator(...args, plugin)
}
}
Expand Down