Skip to content

Commit

Permalink
feat: add graphql schema item and dummy resolver for related resources
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Aug 13, 2020
1 parent 53053ef commit d36afbc
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 7 deletions.
4 changes: 1 addition & 3 deletions gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
61 changes: 57 additions & 4 deletions plugins/gatsby-source-swiftype/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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;
}

Expand All @@ -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 [];
},
},
},
});
};
21 changes: 21 additions & 0 deletions plugins/gatsby-source-swiftype/src/createRelatedResourceNode.js
Original file line number Diff line number Diff line change
@@ -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),
},
});
};

0 comments on commit d36afbc

Please sign in to comment.