diff --git a/gatsby-config.js b/gatsby-config.js index f74dc1381..06de43c99 100644 --- a/gatsby-config.js +++ b/gatsby-config.js @@ -85,9 +85,7 @@ module.exports = { }, }; }, - filterNode: ({ node }) => - node.internal.type === 'Mdx' && - node.frontmatter.template === 'GuideTemplate', + filterNode: ({ node }) => node.frontmatter.template === 'GuideTemplate', }, }, 'gatsby-plugin-sass', diff --git a/plugins/gatsby-source-swiftype/gatsby-node.js b/plugins/gatsby-source-swiftype/gatsby-node.js index e74c473a5..7fd5be502 100644 --- a/plugins/gatsby-source-swiftype/gatsby-node.js +++ b/plugins/gatsby-source-swiftype/gatsby-node.js @@ -1,7 +1,20 @@ const fs = require('fs'); const search = require('./src/search'); +const createRelatedResourceNode = require('./src/createRelatedResourceNode'); -exports.onCreateNode = async ({ node, getNodesByType }, pluginOptions) => { +exports.onPreBootstrap = (_, pluginOptions) => { + const { file } = pluginOptions; + + if (!fs.existsSync(file)) { + fs.writeFileSync(file, '{}'); + } +}; + +exports.onCreateNode = async ( + { actions, node, getNodesByType, createNodeId, createContentDigest }, + pluginOptions +) => { + const { createNode } = actions; const { enabled, filterNode = () => false, @@ -12,7 +25,7 @@ exports.onCreateNode = async ({ node, getNodesByType }, pluginOptions) => { file, } = pluginOptions; - if (!enabled || !filterNode({ node })) { + if (!enabled || node.internal.type !== 'Mdx' || !filterNode({ node })) { return; } @@ -25,14 +38,54 @@ exports.onCreateNode = async ({ node, getNodesByType }, pluginOptions) => { const data = JSON.parse(fs.readFileSync(file)); const params = getParams({ node }); const pathname = getPath({ node }); + const url = siteUrl + pathname; - const result = await search(siteUrl + pathname, params, { + const resources = await search(url, params, { engineKey, pageLimit, }); + resources.forEach((resource) => { + createRelatedResourceNode({ + parentPathname: pathname, + resource, + createContentDigest, + createNode, + createNodeId, + }); + }); + fs.writeFileSync( file, - JSON.stringify({ ...data, [pathname]: result }, null, 2, { flag: 'w' }) + JSON.stringify({ ...data, [pathname]: resources }, null, 2, { flag: 'w' }) ); }; + +exports.createSchemaCustomization = ({ actions }) => { + const { createTypes } = actions; + + const typeDefs = ` + type RelatedResource implements Node { + id: ID! + title: String! + url: String! + } + `; + + createTypes(typeDefs); +}; + +exports.createResolvers = ({ createResolvers }, pluginOptions) => { + const { file } = pluginOptions; + + createResolvers({ + Mdx: { + relatedResources: { + type: ['RelatedResource!'], + resolve(source, args, context, info) { + return []; + }, + }, + }, + }); +}; diff --git a/plugins/gatsby-source-swiftype/src/createRelatedResourceNode.js b/plugins/gatsby-source-swiftype/src/createRelatedResourceNode.js new file mode 100644 index 000000000..9485d4777 --- /dev/null +++ b/plugins/gatsby-source-swiftype/src/createRelatedResourceNode.js @@ -0,0 +1,21 @@ +module.exports = ({ + createNode, + createNodeId, + createContentDigest, + resource, + parentPathname, +}) => { + createNode({ + ...resource, + id: createNodeId(`RelatedResource-${resource.url}`), + parent: null, + children: [], + plugin: 'gatsby-source-swiftype', + parentPathname, + internal: { + type: 'RelatedResource', + content: JSON.stringify(resource), + contentDigest: createContentDigest(resource), + }, + }); +};