Skip to content

Commit

Permalink
Fixed an error where it was impossible to build a site if the `gqlTyp…
Browse files Browse the repository at this point in the history
…ePrefix` setting was enabled on the Craft site.
  • Loading branch information
andris-sevcenko committed Nov 30, 2021
1 parent 3773c28 commit 69a00d7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

---

Expand Down
35 changes: 24 additions & 11 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +28,8 @@ let craftTypesByInterface = {};
let craftFieldsByInterface = {};
let craftPrimarySiteId = '';
let craftEnabledSites = '';
let gatsbyHelperVersion = '';
let craftGqlTypePrefix = '';
/**
* Fetch the schema
*/
Expand All @@ -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;
Expand All @@ -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"
Expand All @@ -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 = {};
Expand Down Expand Up @@ -146,7 +157,6 @@ async function getGatsbyNodeTypes() {
`
};
};
gatsbyNodeTypes = [];
if (loadedPluginOptions.enabledSites) {
if (typeof loadedPluginOptions.enabledSites == "object") {
craftEnabledSites = `["${loadedPluginOptions.enabledSites.join('", "')}"]`;
Expand Down Expand Up @@ -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) {
Expand All @@ -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';
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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,
Expand All @@ -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);
}
41 changes: 29 additions & 12 deletions gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand All @@ -73,6 +70,10 @@ let craftFieldsByInterface: { [key: string]: [GraphQLField<any, any>] } = {};

let craftPrimarySiteId = '';
let craftEnabledSites = '';

let gatsbyHelperVersion = '';
let craftGqlTypePrefix = '';

/**
* Fetch the schema
*/
Expand All @@ -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
}
Expand All @@ -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"
Expand All @@ -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;
Expand Down Expand Up @@ -206,8 +221,6 @@ async function getGatsbyNodeTypes() {
};
};

gatsbyNodeTypes = [];

if (loadedPluginOptions.enabledSites) {
if (typeof loadedPluginOptions.enabledSites == "object") {
craftEnabledSites = `["${loadedPluginOptions.enabledSites.join('", "')}"]`;
Expand Down Expand Up @@ -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)
Expand All @@ -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';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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);
}

0 comments on commit 69a00d7

Please sign in to comment.