Skip to content

Commit

Permalink
feat: properly resolve related resources for a node by linking the pa…
Browse files Browse the repository at this point in the history
…rent to the child
  • Loading branch information
jerelmiller committed Aug 13, 2020
1 parent 3efef9d commit 0ad50b9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
8 changes: 5 additions & 3 deletions plugins/gatsby-source-swiftype/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })) {
Expand All @@ -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 });
});
};

Expand All @@ -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));
},
},
},
Expand Down
11 changes: 8 additions & 3 deletions plugins/gatsby-source-swiftype/src/createRelatedResourceNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module.exports = ({
createContentDigest,
resource,
parent,
}) =>
createNode({
}) => {
const node = {
id: createNodeId(`RelatedResource-${resource.url}`),
title: resource.title,
url: resource.url,
Expand All @@ -17,4 +17,9 @@ module.exports = ({
content: JSON.stringify(resource),
contentDigest: createContentDigest(resource),
},
});
};

createNode(node);

return node;
};

0 comments on commit 0ad50b9

Please sign in to comment.