From 0ad50b9465ddeada08b9d2a28acbd1b3f2378b7c Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Thu, 13 Aug 2020 02:00:40 -0700 Subject: [PATCH] feat: properly resolve related resources for a node by linking the parent to the child --- plugins/gatsby-source-swiftype/gatsby-node.js | 8 +++++--- .../src/createRelatedResourceNode.js | 11 ++++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/plugins/gatsby-source-swiftype/gatsby-node.js b/plugins/gatsby-source-swiftype/gatsby-node.js index fa99aad3b..fbab96197 100644 --- a/plugins/gatsby-source-swiftype/gatsby-node.js +++ b/plugins/gatsby-source-swiftype/gatsby-node.js @@ -16,7 +16,7 @@ exports.onCreateNode = async ( { actions, node, getNodesByType, createNodeId, createContentDigest }, pluginOptions ) => { - const { createNode } = actions; + const { createNode, createParentChildLink } = actions; const { filterNode = () => false, getPath } = pluginOptions; if (node.internal.type !== 'Mdx' || !filterNode({ node })) { @@ -35,13 +35,15 @@ exports.onCreateNode = async ( writeableData[pathname] = resources; resources.forEach((resource) => { - createRelatedResourceNode({ + const child = createRelatedResourceNode({ parent: node.id, resource, createContentDigest, createNode, createNodeId, }); + + createParentChildLink({ parent: node, child: child }); }); }; @@ -67,7 +69,7 @@ exports.createResolvers = ({ createResolvers }) => { resolve(source, _args, context) { return context.nodeModel .getAllNodes({ type: 'RelatedResource' }) - .filter((node) => node.parent === source.id); + .filter((node) => source.children.includes(node.id)); }, }, }, diff --git a/plugins/gatsby-source-swiftype/src/createRelatedResourceNode.js b/plugins/gatsby-source-swiftype/src/createRelatedResourceNode.js index b5623cb7a..249c4c238 100644 --- a/plugins/gatsby-source-swiftype/src/createRelatedResourceNode.js +++ b/plugins/gatsby-source-swiftype/src/createRelatedResourceNode.js @@ -4,8 +4,8 @@ module.exports = ({ createContentDigest, resource, parent, -}) => - createNode({ +}) => { + const node = { id: createNodeId(`RelatedResource-${resource.url}`), title: resource.title, url: resource.url, @@ -17,4 +17,9 @@ module.exports = ({ content: JSON.stringify(resource), contentDigest: createContentDigest(resource), }, - }); + }; + + createNode(node); + + return node; +};