From 69a00d7139879034b24c3f4bde4325fd0dd8ac17 Mon Sep 17 00:00:00 2001 From: andris-sevcenko Date: Tue, 30 Nov 2021 11:53:10 +0200 Subject: [PATCH] Fixed an error where it was impossible to build a site if the `gqlTypePrefix` setting was enabled on the Craft site. --- CHANGELOG.md | 4 ++++ README.md | 2 +- gatsby-node.js | 35 ++++++++++++++++++++++++----------- gatsby-node.ts | 41 +++++++++++++++++++++++++++++------------ 4 files changed, 58 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ecc3e2e..c06389b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Release Notes +## Unreleased +- Fixed an error where it was impossible to build a site if the `gqlTypePrefix` setting was enabled on the Craft site. ([#58](https://github.com/craftcms/gatsby-source-craft/issues/58)) +- Bumped Gatsby Helper plugin version requirements to at least 1.0.9. + ## 2.0.4 - 2021-10-19 - Improved multi-site support for elements that aren’t enabled for all sites. ([#50](https://github.com/craftcms/gatsby-source-craft/issues/50)) diff --git a/README.md b/README.md index 1d0a5b6..a173505 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ This Gatsby source plugin provides an integration with [Craft CMS](https://craftcms.com). It uses Craft’s [GraphQL API](https://docs.craftcms.com/v3/graphql.html) to make content within Craft available to Gatsby-powered front ends. -It requires Craft CMS 3.5.16 or later, and for the corresponding [Gatsby Helper](https://github.com/craftcms/gatsby-helper) plugin 1.0.6 or later to be installed on the Craft site. +It requires Craft CMS 3.5.16 or later, and for the corresponding [Gatsby Helper](https://github.com/craftcms/gatsby-helper) plugin 1.0.9 or later to be installed on the Craft site. --- diff --git a/gatsby-node.js b/gatsby-node.js index c80a5ab..61be8ee 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -19,9 +19,6 @@ const loadedPluginOptions = { enabledSites: null }; const internalFragmentDir = __dirname + "/.cache/internal-craft-fragments"; -const mandatoryFragments = { - ensureRemoteId: 'fragment RequiredEntryFields on EntryInterface { id }' -}; let schema; let gatsbyNodeTypes; let sourcingConfig; @@ -31,6 +28,8 @@ let craftTypesByInterface = {}; let craftFieldsByInterface = {}; let craftPrimarySiteId = ''; let craftEnabledSites = ''; +let gatsbyHelperVersion = ''; +let craftGqlTypePrefix = ''; /** * Fetch the schema */ @@ -43,7 +42,7 @@ async function getSchema() { /** * Return a list of all possible Gatsby node types */ -async function getGatsbyNodeTypes() { +async function getGatsbyNodeTypes(reporter) { var _a; if (gatsbyNodeTypes) { return gatsbyNodeTypes; @@ -53,13 +52,23 @@ async function getGatsbyNodeTypes() { if (!queries) { return ([]); } + gatsbyNodeTypes = []; // Check if Craft endpoint has Gatsby plugin installed and enabled. if (!queries.sourceNodeInformation) { + reporter.info("Gatsby Helper not found on target Craft site."); return ([]); } + if (!queries.gatsbyHelperVersion) { + reporter.info("Gatsby Helper plugin must be at least version 1.0.9 or greater."); + } const queryResponse = await execute({ operationName: 'sourceNodeData', - query: 'query sourceNodeData { sourceNodeInformation { node list filterArgument filterTypeExpression targetInterface } primarySiteId }', + query: `query sourceNodeData { + sourceNodeInformation { node list filterArgument filterTypeExpression targetInterface } + primarySiteId + gatsbyHelperVersion + gqlTypePrefix + }`, variables: {}, additionalHeaders: { "X-Craft-Gql-Cache": "no-cache" @@ -68,6 +77,8 @@ async function getGatsbyNodeTypes() { if (!(queryResponse.data && queryResponse.data.sourceNodeInformation)) { return ([]); } + craftGqlTypePrefix = queryResponse.data.gqlTypePrefix; + gatsbyHelperVersion = queryResponse.data.gatsbyHelperVersion; craftPrimarySiteId = queryResponse.data.primarySiteId; const sourceNodeInformation = queryResponse.data.sourceNodeInformation; const queryMap = {}; @@ -146,7 +157,6 @@ async function getGatsbyNodeTypes() { ` }; }; - gatsbyNodeTypes = []; if (loadedPluginOptions.enabledSites) { if (typeof loadedPluginOptions.enabledSites == "object") { craftEnabledSites = `["${loadedPluginOptions.enabledSites.join('", "')}"]`; @@ -203,10 +213,10 @@ async function getGatsbyNodeTypes() { /** * Write default fragments to the disk. */ -async function writeDefaultFragments() { +async function writeDefaultFragments(reporter) { const defaultFragments = generateDefaultFragments({ schema: await getSchema(), - gatsbyNodeTypes: await getGatsbyNodeTypes(), + gatsbyNodeTypes: await getGatsbyNodeTypes(reporter), }); await fs.ensureDir(internalFragmentDir); for (const [remoteTypeName, fragment] of defaultFragments) { @@ -219,6 +229,9 @@ async function writeDefaultFragments() { async function addExtraFragments(reporter) { const fragmentDir = loadedPluginOptions.fragmentsDir; const fragments = await fs.readdir(fragmentDir); + const mandatoryFragments = { + ensureRemoteId: `fragment RequiredEntryFields on ${craftGqlTypePrefix}EntryInterface { id }` + }; // Add mandatory fragments for (let [fragmentName, fragmentBody] of Object.entries(mandatoryFragments)) { fragmentName += '.graphql'; @@ -331,7 +344,7 @@ exports.createSchemaCustomization = async (gatsbyApi) => { } } // Convert Craft's DateTime to Gatsby's Date. - fieldType = fieldType.replace(/DateTime/, 'JSON'); + fieldType = fieldType.replace(new RegExp(craftGqlTypePrefix + 'DateTime'), 'JSON'); if (fieldType.match(/(Int|Float|String|Boolean|ID|JSON)(\]|!\]|$)/)) { return fieldType; } @@ -519,7 +532,7 @@ async function getSourcingConfig(gatsbyApi) { return sourcingConfig; } const schema = await getSchema(); - const gatsbyNodeTypes = await getGatsbyNodeTypes(); + const gatsbyNodeTypes = await getGatsbyNodeTypes(gatsbyApi.reporter); const documents = await compileNodeQueries({ schema, gatsbyNodeTypes, @@ -539,6 +552,6 @@ async function ensureFragmentsExist(reporter) { reporter.info("Clearing previous fragments."); await fs.remove(internalFragmentDir, { recursive: true }); reporter.info("Writing default fragments."); - await writeDefaultFragments(); + await writeDefaultFragments(reporter); await addExtraFragments(reporter); } diff --git a/gatsby-node.ts b/gatsby-node.ts index cc76dff..06d1cbf 100644 --- a/gatsby-node.ts +++ b/gatsby-node.ts @@ -59,9 +59,6 @@ const loadedPluginOptions: SourcePluginOptions = { }; const internalFragmentDir = __dirname + "/.cache/internal-craft-fragments"; -const mandatoryFragments = { - ensureRemoteId: 'fragment RequiredEntryFields on EntryInterface { id }' -} let schema: GraphQLSchema; let gatsbyNodeTypes: IGatsbyNodeConfig[]; @@ -73,6 +70,10 @@ let craftFieldsByInterface: { [key: string]: [GraphQLField] } = {}; let craftPrimarySiteId = ''; let craftEnabledSites = ''; + +let gatsbyHelperVersion = ''; +let craftGqlTypePrefix = ''; + /** * Fetch the schema */ @@ -87,7 +88,7 @@ async function getSchema() { /** * Return a list of all possible Gatsby node types */ -async function getGatsbyNodeTypes() { +async function getGatsbyNodeTypes(reporter: Reporter) { if (gatsbyNodeTypes) { return gatsbyNodeTypes } @@ -100,14 +101,26 @@ async function getGatsbyNodeTypes() { return ([]); } + gatsbyNodeTypes = []; + // Check if Craft endpoint has Gatsby plugin installed and enabled. if (!queries.sourceNodeInformation) { + reporter.info("Gatsby Helper not found on target Craft site."); return ([]); } + if (!queries.gatsbyHelperVersion) { + reporter.info("Gatsby Helper plugin must be at least version 1.0.9 or greater."); + } + const queryResponse = await execute({ operationName: 'sourceNodeData', - query: 'query sourceNodeData { sourceNodeInformation { node list filterArgument filterTypeExpression targetInterface } primarySiteId }', + query: `query sourceNodeData { + sourceNodeInformation { node list filterArgument filterTypeExpression targetInterface } + primarySiteId + gatsbyHelperVersion + gqlTypePrefix + }`, variables: {}, additionalHeaders: { "X-Craft-Gql-Cache": "no-cache" @@ -118,6 +131,8 @@ async function getGatsbyNodeTypes() { return ([]); } + craftGqlTypePrefix = queryResponse.data.gqlTypePrefix; + gatsbyHelperVersion = queryResponse.data.gatsbyHelperVersion; craftPrimarySiteId = queryResponse.data.primarySiteId; const sourceNodeInformation = queryResponse.data.sourceNodeInformation; @@ -206,8 +221,6 @@ async function getGatsbyNodeTypes() { }; }; - gatsbyNodeTypes = []; - if (loadedPluginOptions.enabledSites) { if (typeof loadedPluginOptions.enabledSites == "object") { craftEnabledSites = `["${loadedPluginOptions.enabledSites.join('", "')}"]`; @@ -278,10 +291,10 @@ async function getGatsbyNodeTypes() { /** * Write default fragments to the disk. */ -async function writeDefaultFragments() { +async function writeDefaultFragments(reporter: Reporter) { const defaultFragments = generateDefaultFragments({ schema: await getSchema(), - gatsbyNodeTypes: await getGatsbyNodeTypes(), + gatsbyNodeTypes: await getGatsbyNodeTypes(reporter), }) await fs.ensureDir(internalFragmentDir) @@ -298,6 +311,10 @@ async function addExtraFragments (reporter: Reporter) { const fragmentDir = loadedPluginOptions.fragmentsDir; const fragments = await fs.readdir(fragmentDir); + const mandatoryFragments = { + ensureRemoteId: `fragment RequiredEntryFields on ${craftGqlTypePrefix}EntryInterface { id }` + } + // Add mandatory fragments for (let [fragmentName, fragmentBody] of Object.entries(mandatoryFragments)) { fragmentName += '.graphql'; @@ -438,7 +455,7 @@ exports.createSchemaCustomization = async (gatsbyApi: NodePluginArgs) => { } // Convert Craft's DateTime to Gatsby's Date. - fieldType = fieldType.replace(/DateTime/, 'JSON'); + fieldType = fieldType.replace(new RegExp(craftGqlTypePrefix + 'DateTime'), 'JSON'); if (fieldType.match(/(Int|Float|String|Boolean|ID|JSON)(\]|!\]|$)/)) { return fieldType; @@ -655,7 +672,7 @@ async function getSourcingConfig(gatsbyApi: NodePluginArgs) { return sourcingConfig } const schema = await getSchema() - const gatsbyNodeTypes = await getGatsbyNodeTypes() + const gatsbyNodeTypes = await getGatsbyNodeTypes(gatsbyApi.reporter) const documents = await compileNodeQueries({ schema, @@ -680,6 +697,6 @@ async function ensureFragmentsExist(reporter: Reporter) { await fs.remove(internalFragmentDir, {recursive: true}); reporter.info("Writing default fragments."); - await writeDefaultFragments(); + await writeDefaultFragments(reporter); await addExtraFragments(reporter); }