Skip to content

Commit dffb74e

Browse files
frinyvonnickpieh
authored andcommitted
fix(gatsby): respect node type owner in deleteNode (#13492)
1 parent 424c027 commit dffb74e

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

packages/gatsby/src/db/__tests__/nodes.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,32 @@ describe(`nodes db tests`, () => {
349349
expect(report.warn).toHaveBeenCalledWith(deprecationNotice)
350350
})
351351

352+
it(`throws an error when trying to delete a node of a type owned from another plugin`, () => {
353+
expect(() => {
354+
store.dispatch(
355+
actions.createNode(
356+
{
357+
id: `hi`,
358+
children: [],
359+
parent: `test`,
360+
internal: {
361+
contentDigest: `hasdfljds`,
362+
type: `Other`,
363+
},
364+
},
365+
{
366+
name: `other`,
367+
}
368+
)
369+
)
370+
store.dispatch(
371+
actions.deleteNode(`hi`, getNode(`hi`), {
372+
name: `tests`,
373+
})
374+
)
375+
}).toThrow(/deleted/)
376+
})
377+
352378
it(`does not crash when delete node is called on undefined`, () => {
353379
actions.deleteNode(undefined, {
354380
name: `tests`,

packages/gatsby/src/redux/actions.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,26 @@ actions.deleteNode = (options: any, plugin: Plugin, args: any) => {
403403
// Always get node from the store, as the node we get as an arg
404404
// might already have been deleted.
405405
const node = getNode(id)
406+
if (plugin) {
407+
const pluginName = plugin.name
408+
409+
if (node && typeOwners[node.internal.type] !== pluginName)
410+
throw new Error(stripIndent`
411+
The plugin "${pluginName}" deleted a node of a type owned by another plugin.
412+
413+
The node type "${node.internal.type}" is owned by "${
414+
typeOwners[node.internal.type]
415+
}".
416+
417+
The node object passed to "deleteNode":
418+
419+
${JSON.stringify(node, null, 4)}
420+
421+
The plugin deleting the node:
422+
423+
${JSON.stringify(plugin, null, 4)}
424+
`)
425+
}
406426

407427
const createDeleteAction = node => {
408428
return {

0 commit comments

Comments
 (0)