-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #594 from newrelic/jerel/sync-search-data
Source related resource content from Swiftype
- Loading branch information
Showing
10 changed files
with
6,533 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
const fs = require('fs'); | ||
const createRelatedResourceNode = require('./src/createRelatedResourceNode'); | ||
const getRelatedResources = require('./src/getRelatedResources'); | ||
|
||
const writeableData = {}; | ||
|
||
exports.onPreBootstrap = (_, pluginOptions) => { | ||
const { file } = pluginOptions; | ||
|
||
if (!fs.existsSync(file)) { | ||
fs.writeFileSync(file, '{}'); | ||
} | ||
}; | ||
|
||
exports.onCreateNode = async ( | ||
{ actions, node, getNodesByType, createNodeId, createContentDigest }, | ||
pluginOptions | ||
) => { | ||
const { createNode, createParentChildLink } = actions; | ||
const { filterNode = () => false, getPath } = pluginOptions; | ||
|
||
if (node.internal.type !== 'Mdx' || !filterNode({ node })) { | ||
return; | ||
} | ||
|
||
const [ | ||
{ | ||
siteMetadata: { siteUrl }, | ||
}, | ||
] = getNodesByType('Site'); | ||
|
||
const pathname = getPath({ node }); | ||
const resources = await getRelatedResources({ node, siteUrl }, pluginOptions); | ||
|
||
writeableData[pathname] = resources; | ||
|
||
resources.forEach((resource) => { | ||
const child = createRelatedResourceNode({ | ||
parent: node.id, | ||
resource, | ||
createContentDigest, | ||
createNode, | ||
createNodeId, | ||
}); | ||
|
||
createParentChildLink({ parent: node, child: child }); | ||
}); | ||
}; | ||
|
||
exports.createSchemaCustomization = ({ actions }) => { | ||
const { createTypes } = actions; | ||
|
||
const typeDefs = ` | ||
type RelatedResource implements Node { | ||
id: ID! | ||
title: String! | ||
url: String! | ||
} | ||
`; | ||
|
||
createTypes(typeDefs); | ||
}; | ||
|
||
exports.createResolvers = ({ createResolvers }) => { | ||
createResolvers({ | ||
Mdx: { | ||
relatedResources: { | ||
args: { | ||
limit: { | ||
type: 'Int', | ||
defaultValue: 5, | ||
}, | ||
}, | ||
type: ['RelatedResource!'], | ||
resolve: (source, args, context) => { | ||
const { limit } = args; | ||
|
||
return context.nodeModel | ||
.getNodesByIds({ ids: source.children }) | ||
.slice(0, Math.max(limit, 0)); | ||
}, | ||
}, | ||
}, | ||
}); | ||
}; | ||
|
||
exports.onPostBootstrap = (_, pluginOptions) => { | ||
const { refetch, file } = pluginOptions; | ||
|
||
if (refetch) { | ||
fs.writeFileSync(file, JSON.stringify(writeableData, null, 2)); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
25 changes: 25 additions & 0 deletions
25
plugins/gatsby-source-swiftype/src/createRelatedResourceNode.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module.exports = ({ | ||
createNode, | ||
createNodeId, | ||
createContentDigest, | ||
resource, | ||
parent, | ||
}) => { | ||
const node = { | ||
id: createNodeId(`RelatedResource-${resource.url}`), | ||
title: resource.title, | ||
url: resource.url, | ||
parent, | ||
children: [], | ||
plugin: 'gatsby-source-swiftype', | ||
internal: { | ||
type: 'RelatedResource', | ||
content: JSON.stringify(resource), | ||
contentDigest: createContentDigest(resource), | ||
}, | ||
}; | ||
|
||
createNode(node); | ||
|
||
return node; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const fs = require('fs'); | ||
const search = require('./search'); | ||
|
||
module.exports = async ({ node, siteUrl }, pluginOptions) => { | ||
const { | ||
refetch, | ||
engineKey, | ||
limit, | ||
file, | ||
getParams = () => ({}), | ||
getPath, | ||
} = pluginOptions; | ||
|
||
const pathname = getPath({ node }); | ||
|
||
if (refetch) { | ||
return search(siteUrl + pathname, getParams({ node }), { | ||
engineKey, | ||
limit, | ||
}); | ||
} | ||
|
||
const data = JSON.parse(fs.readFileSync(file)); | ||
|
||
return data[pathname] || []; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const fetch = require('node-fetch'); | ||
const { appendTrailingSlash, stripTrailingSlash } = require('./utils/url'); | ||
|
||
const normalizeUrl = (url) => { | ||
const prefix = url.startsWith('!') ? '!' : ''; | ||
const plainUrl = url.replace(/^!/, ''); | ||
|
||
return [ | ||
prefix + appendTrailingSlash(plainUrl), | ||
prefix + stripTrailingSlash(plainUrl), | ||
]; | ||
}; | ||
|
||
const uniq = (arr) => [...new Set(arr)]; | ||
|
||
module.exports = async (url, params = {}, { engineKey, limit }) => { | ||
const { page: pageFilters = {} } = params.filters || {}; | ||
|
||
const res = await fetch( | ||
'https://search-api.swiftype.com/api/v1/public/engines/search.json', | ||
{ | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
...params, | ||
engine_key: engineKey, | ||
per_page: limit, | ||
filters: { | ||
...params.filters, | ||
page: { | ||
...pageFilters, | ||
url: uniq([ | ||
...normalizeUrl(`!${url}`), | ||
...(pageFilters.url || []).flatMap(normalizeUrl), | ||
]), | ||
}, | ||
}, | ||
}), | ||
} | ||
); | ||
|
||
const { records } = await res.json(); | ||
|
||
return records.page; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const hasQueryParams = (urlString) => { | ||
const url = new URL(urlString); | ||
|
||
return Boolean(url.search); | ||
}; | ||
|
||
exports.appendTrailingSlash = (url) => { | ||
if (hasQueryParams(url)) { | ||
return url; | ||
} | ||
|
||
return url.endsWith('/') ? url : `${url}/`; | ||
}; | ||
|
||
exports.stripTrailingSlash = (url) => { | ||
if (hasQueryParams(url)) { | ||
return url; | ||
} | ||
|
||
return url.endsWith('/') ? url.replace(/\/$/, '') : url; | ||
}; |
Oops, something went wrong.