Skip to content

Commit

Permalink
feat(gatsby-source-drupal): Persist backreference data individually i…
Browse files Browse the repository at this point in the history
…n cache so cheaper to get/set (#35323)

* Persist backreference data individually in cache so cheaper to get/set

* small updates to README

* Update packages/gatsby-source-drupal/src/utils.js

Co-authored-by: Ward Peeters <ward@coding-tech.com>

* Update packages/gatsby-source-drupal/src/utils.js

Co-authored-by: Tyler Barnes <tylerdbarnes@gmail.com>

* Update packages/gatsby-source-drupal/src/utils.js

Co-authored-by: Ward Peeters <ward@coding-tech.com>

* more fixes

* add set

* Process nodes in parallel

Co-authored-by: Ward Peeters <ward@coding-tech.com>
Co-authored-by: Tyler Barnes <tylerdbarnes@gmail.com>
  • Loading branch information
3 people committed Apr 28, 2022
1 parent 63828fc commit 1257a8d
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 146 deletions.
4 changes: 2 additions & 2 deletions packages/gatsby-source-drupal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

Source plugin for pulling data (including images) into Gatsby from Drupal sites.

It pulls data from Drupal 8 sites with the
It pulls data from Drupal 8/9 sites with the
[Drupal JSONAPI module](https://www.drupal.org/project/jsonapi) installed.

An example site built with the headless Drupal distro
[ContentaCMS](https://twitter.com/contentacms) is at
https://using-drupal.gatsbyjs.org/

`apiBase` Option allows changing the API entry point depending on the version of
The `apiBase` option allows changing the API entry point depending on the version of
jsonapi used by your Drupal instance. The default value is `jsonapi`, which has
been used since jsonapi version `8.x-1.0-alpha4`.

Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-source-drupal/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function makeCache() {
return {
get: async id => store.get(id),
set: async (key, value) => store.set(key, value),
del: async key => store.delete(key),
store,
}
}
Expand Down
66 changes: 32 additions & 34 deletions packages/gatsby-source-drupal/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ const {
imageCDNState,
} = require(`./normalize`)
const {
initRefsLookups,
storeRefsLookups,
handleReferences,
handleWebhookUpdate,
createNodeIfItDoesNotExist,
Expand Down Expand Up @@ -189,8 +187,6 @@ exports.sourceNodes = async (
unstable_createNodeManifest,
} = actions

await initRefsLookups({ cache, getNode })

// Update the concurrency limit from the plugin options
requestQueue.concurrency = concurrentAPIRequests

Expand Down Expand Up @@ -234,6 +230,7 @@ ${JSON.stringify(webhookBody, null, 4)}`
const deletedNode = await handleDeletedNode({
actions,
getNode,
cache,
node: nodeToDelete,
createNodeId,
createContentDigest,
Expand All @@ -243,7 +240,6 @@ ${JSON.stringify(webhookBody, null, 4)}`
}

changesActivity.end()
await storeRefsLookups({ cache })
return
}

Expand Down Expand Up @@ -285,7 +281,6 @@ ${JSON.stringify(webhookBody, null, 4)}`
return
}
changesActivity.end()
await storeRefsLookups({ cache })
return
}

Expand Down Expand Up @@ -404,6 +399,7 @@ ${JSON.stringify(webhookBody, null, 4)}`
handleDeletedNode({
actions,
getNode,
cache,
node: nodeSyncData,
createNodeId,
createContentDigest,
Expand All @@ -417,23 +413,25 @@ ${JSON.stringify(webhookBody, null, 4)}`
nodesToUpdate = [nodeSyncData.data]
}

for (const nodeToUpdate of nodesToUpdate) {
await handleWebhookUpdate(
{
nodeToUpdate,
actions,
cache,
createNodeId,
createContentDigest,
getCache,
getNode,
reporter,
store,
languageConfig,
},
pluginOptions
await Promise.all(
nodesToUpdate.map(nodeToUpdate =>
handleWebhookUpdate(
{
nodeToUpdate,
actions,
cache,
createNodeId,
createContentDigest,
getCache,
getNode,
reporter,
store,
languageConfig,
},
pluginOptions
)
)
}
)
}
}

Expand All @@ -445,7 +443,6 @@ ${JSON.stringify(webhookBody, null, 4)}`

drupalFetchIncrementalActivity.end()
fastBuildsSpan.finish()
await storeRefsLookups({ cache })
return
}

Expand All @@ -456,7 +453,6 @@ ${JSON.stringify(webhookBody, null, 4)}`
initialSourcing = false

if (!requireFullRebuild) {
await storeRefsLookups({ cache })
return
}
}
Expand Down Expand Up @@ -696,15 +692,17 @@ ${JSON.stringify(webhookBody, null, 4)}`
createNodesSpan.setTag(`sourceNodes.createNodes.count`, nodes.size)

// second pass - handle relationships and back references
nodes.forEach(node => {
handleReferences(node, {
getNode: nodes.get.bind(nodes),
mutateNode: true,
createNodeId,
entityReferenceRevisions,
fileNodesExtendedData,
})
})
await Promise.all(
Array.from(nodes.values()).map(node =>
handleReferences(node, {
getNode: nodes.get.bind(nodes),
mutateNode: true,
createNodeId,
cache,
entityReferenceRevisions,
})
)
)

if (skipFileDownloads) {
reporter.info(`Skipping remote file download from Drupal`)
Expand Down Expand Up @@ -753,7 +751,7 @@ ${JSON.stringify(webhookBody, null, 4)}`
initialSourcing = false

createNodesSpan.finish()
await storeRefsLookups({ cache, getNodes })
return
}

// This is maintained for legacy reasons and will eventually be removed.
Expand Down
Loading

0 comments on commit 1257a8d

Please sign in to comment.