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

fix(gatsby-source-drupal): Ensure all new nodes are created before creating relationships (#33864) #34019

Merged
merged 1 commit into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,69 @@
}
}
}
},
{
"jsonapi": {
"version": "1.0",
"meta": {
"links": {
"self": {
"href": "http://jsonapi.org/format/1.0/"
}
}
}
},
"data": {
"type": "node--article",
"id": "article-10",
"attributes": {
"id": 100,
"uuid": "article-10",
"title": "Article #10",
"body": "Aliquam non varius libero, sit amet consequat ex. Aenean porta turpis quis vulputate blandit. Suspendisse in porta erat. Sed sit amet scelerisque turpis, at rutrum mauris. Sed tempor eleifend lobortis. Proin maximus, massa sed dignissim sollicitudin, quam risus mattis justo, sit amet aliquam odio ligula quis urna. Interdum et malesuada fames ac ante ipsum primis in faucibus. Ut mollis leo nisi, at interdum urna fermentum ut. Fusce id suscipit neque, eu fermentum lacus. Donec egestas laoreet felis ac luctus. Vestibulum molestie mattis ante, a vulputate nunc ullamcorper at. Ut hendrerit ipsum eget gravida ultricies."
},
"relationships": {
"field_tags": {
"data": [
{
"type": "taxonomy_term--tags",
"id": "tag-10"
}
]
}
}
}
},
{
"jsonapi": {
"version": "1.0",
"meta": {
"links": {
"self": {
"href": "http://jsonapi.org/format/1.0/"
}
}
}
},
"data": {
"type": "taxonomy_term--tags",
"id": "tag-10",
"attributes": {
"id": 110,
"uuid": "tag-10",
"langcode": "en",
"name": "Tag #10",
"description": null,
"weight": 0,
"changed": 1523031646,
"default_langcode": true,
"path": {
"alias": null,
"pid": null,
"langcode": "en"
}
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"action": "update",
"data": [
{
"type": "node--article",
"id": "article-10",
"attributes": {
"id": 100,
"uuid": "article-10",
"title": "Article #10",
"body": "Aliquam non varius libero, sit amet consequat ex. Aenean porta turpis quis vulputate blandit. Suspendisse in porta erat. Sed sit amet scelerisque turpis, at rutrum mauris. Sed tempor eleifend lobortis. Proin maximus, massa sed dignissim sollicitudin, quam risus mattis justo, sit amet aliquam odio ligula quis urna. Interdum et malesuada fames ac ante ipsum primis in faucibus. Ut mollis leo nisi, at interdum urna fermentum ut. Fusce id suscipit neque, eu fermentum lacus. Donec egestas laoreet felis ac luctus. Vestibulum molestie mattis ante, a vulputate nunc ullamcorper at. Ut hendrerit ipsum eget gravida ultricies."
},
"relationships": {
"field_tags": {
"data": [
{
"type": "taxonomy_term--tags",
"id": "tag-10"
}
]
}
}
},
{
"type": "taxonomy_term--tags",
"id": "tag-10",
"attributes": {
"id": 110,
"uuid": "tag-10",
"langcode": "en",
"name": "Tag #10",
"description": null,
"weight": 0,
"changed": 1523031646,
"default_langcode": true,
"path": {
"alias": null,
"pid": null,
"langcode": "en"
}
}
}
]
}

24 changes: 24 additions & 0 deletions packages/gatsby-source-drupal/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,26 @@ describe(`gatsby-source-drupal`, () => {
})
})
})
describe(`multiple entities in webhook body`, () => {
let resp
beforeAll(async () => {
const webhookBody = require(`./fixtures/webhook-body-multiple-nodes.json`)
await sourceNodes(
{
...args,
webhookBody,
},
{ baseUrl }
)
})

it(`Relationships`, async () => {
expect(
nodes[createNodeId(`und.article-10`)].relationships.field_tags___NODE
.length
).toBe(1)
})
})

describe(`Insert content`, () => {
it(`Node doesn't exist before webhook`, () => {
Expand Down Expand Up @@ -567,6 +587,10 @@ describe(`gatsby-source-drupal`, () => {
nodes[createNodeId(`und.article-2`)].relationships
.field_tertiary_image___NODE_image___NODE
).toBe(undefined)
expect(
nodes[createNodeId(`und.article-10`)].relationships.field_tags___NODE
.length
).toBe(1)
})

it(`Back references`, () => {
Expand Down
37 changes: 37 additions & 0 deletions packages/gatsby-source-drupal/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const {
storeRefsLookups,
handleReferences,
handleWebhookUpdate,
createNodeIfItDoesNotExist,
handleDeletedNode,
} = require(`./utils`)

Expand Down Expand Up @@ -219,6 +220,17 @@ ${JSON.stringify(webhookBody, null, 4)}`
nodesToUpdate = [data]
}

for (const nodeToUpdate of nodesToUpdate) {
await createNodeIfItDoesNotExist({
nodeToUpdate,
actions,
createNodeId,
createContentDigest,
getNode,
reporter,
})
}

for (const nodeToUpdate of nodesToUpdate) {
await handleWebhookUpdate(
{
Expand Down Expand Up @@ -330,6 +342,31 @@ ${JSON.stringify(webhookBody, null, 4)}`

// Process sync data from Drupal.
const nodesToSync = res.body.entities

// First create all nodes that we haven't seen before. That
// way we can create relationships correctly next as the nodes
// will exist in Gatsby.
for (const nodeSyncData of nodesToSync) {
if (nodeSyncData.action === `delete`) {
continue
}

let nodesToUpdate = nodeSyncData.data
if (!Array.isArray(nodeSyncData.data)) {
nodesToUpdate = [nodeSyncData.data]
}
for (const nodeToUpdate of nodesToUpdate) {
createNodeIfItDoesNotExist({
nodeToUpdate,
actions,
createNodeId,
createContentDigest,
getNode,
reporter,
})
}
}

for (const nodeSyncData of nodesToSync) {
if (nodeSyncData.action === `delete`) {
handleDeletedNode({
Expand Down
45 changes: 45 additions & 0 deletions packages/gatsby-source-drupal/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,50 @@ const handleDeletedNode = async ({
return deletedNode
}

function createNodeIfItDoesNotExist({
nodeToUpdate,
actions,
createNodeId,
createContentDigest,
getNode,
reporter,
}) {
if (!nodeToUpdate) {
reporter.warn(
`The updated node was empty. The fact you're seeing this warning means there's probably a bug in how we're creating and processing updates from Drupal.

${JSON.stringify(nodeToUpdate, null, 4)}
`
)

return
}

const { createNode } = actions
const newNodeId = createNodeId(
createNodeIdWithVersion(
nodeToUpdate.id,
nodeToUpdate.type,
getOptions().languageConfig ? nodeToUpdate.langcode : `und`,
nodeToUpdate.meta?.target_version,
getOptions().entityReferenceRevisions
)
)

const oldNode = getNode(newNodeId)
// Node doesn't yet exist so we'll create it now.
if (!oldNode) {
const newNode = nodeFromData(
nodeToUpdate,
createNodeId,
getOptions().entityReferenceRevisions
)

newNode.internal.contentDigest = createContentDigest(newNode)
createNode(newNode)
}
}

const handleWebhookUpdate = async (
{
nodeToUpdate,
Expand Down Expand Up @@ -371,3 +415,4 @@ ${JSON.stringify(nodeToUpdate, null, 4)}

exports.handleWebhookUpdate = handleWebhookUpdate
exports.handleDeletedNode = handleDeletedNode
exports.createNodeIfItDoesNotExist = createNodeIfItDoesNotExist