diff --git a/packages/gatsby-source-wordpress/src/steps/create-schema-customization/build-types.js b/packages/gatsby-source-wordpress/src/steps/create-schema-customization/build-types.js index bdce362fde9df..d5d30bfc87c67 100644 --- a/packages/gatsby-source-wordpress/src/steps/create-schema-customization/build-types.js +++ b/packages/gatsby-source-wordpress/src/steps/create-schema-customization/build-types.js @@ -9,7 +9,7 @@ import { } from "./helpers" const unionType = typeBuilderApi => { - const { typeDefs, schema, type, pluginOptions } = typeBuilderApi + const { schema, type, pluginOptions } = typeBuilderApi const types = type.possibleTypes .filter( @@ -22,7 +22,7 @@ const unionType = typeBuilderApi => { .map(possibleType => buildTypeName(possibleType.name)) if (!types || !types.length) { - return + return false } let unionType = { @@ -47,13 +47,12 @@ const unionType = typeBuilderApi => { // @todo add this as a plugin option unionType = filterTypeDefinition(unionType, typeBuilderApi, `UNION`) - typeDefs.push(schema.buildUnionType(unionType)) + return schema.buildUnionType(unionType) } const interfaceType = typeBuilderApi => { const { type, - typeDefs, schema, gatsbyNodeTypes, fieldAliases, @@ -111,7 +110,7 @@ const interfaceType = typeBuilderApi => { // @todo add this as a plugin option typeDef = filterTypeDefinition(typeDef, typeBuilderApi, `INTERFACE`) - typeDefs.push(schema.buildInterfaceType(typeDef)) + return schema.buildInterfaceType(typeDef) } const objectType = typeBuilderApi => { @@ -120,7 +119,6 @@ const objectType = typeBuilderApi => { gatsbyNodeTypes, fieldAliases, fieldBlacklist, - typeDefs, schema, isAGatsbyNode, } = typeBuilderApi @@ -138,7 +136,7 @@ const objectType = typeBuilderApi => { // TypeError: Cannot convert undefined or null to object at Function.keys () // Also cause wordpress blog site build failure in createSchemaCustomization step if (!transformedFields || !Object.keys(transformedFields).length) { - return + return false } let objectType = { @@ -180,21 +178,18 @@ const objectType = typeBuilderApi => { // @todo add this as a plugin option objectType = filterTypeDefinition(objectType, typeBuilderApi, `OBJECT`) - typeDefs.push(schema.buildObjectType(objectType)) + return schema.buildObjectType(objectType) } -const enumType = ({ typeDefs, schema, type }) => { - typeDefs.push( - schema.buildEnumType({ - name: buildTypeName(type.name), - values: type.enumValues.reduce((accumulator, { name }) => { - accumulator[name] = { name } - - return accumulator - }, {}), - description: type.description, - }) - ) -} +const enumType = ({ schema, type }) => + schema.buildEnumType({ + name: buildTypeName(type.name), + values: type.enumValues.reduce((accumulator, { name }) => { + accumulator[name] = { name } + + return accumulator + }, {}), + description: type.description, + }) export default { unionType, interfaceType, objectType, enumType } diff --git a/packages/gatsby-source-wordpress/src/steps/create-schema-customization/helpers.js b/packages/gatsby-source-wordpress/src/steps/create-schema-customization/helpers.js index 5e89e8979bae2..d08ab8ce82697 100644 --- a/packages/gatsby-source-wordpress/src/steps/create-schema-customization/helpers.js +++ b/packages/gatsby-source-wordpress/src/steps/create-schema-customization/helpers.js @@ -20,6 +20,10 @@ export const buildTypeName = name => { return name } + if (name === `Filter`) { + name = `FilterType` + } + return prefix + name } diff --git a/packages/gatsby-source-wordpress/src/steps/create-schema-customization/index.js b/packages/gatsby-source-wordpress/src/steps/create-schema-customization/index.js index f5d593177fcba..e72877387a96f 100644 --- a/packages/gatsby-source-wordpress/src/steps/create-schema-customization/index.js +++ b/packages/gatsby-source-wordpress/src/steps/create-schema-customization/index.js @@ -42,18 +42,20 @@ const customizeSchema = async ({ actions, schema }) => { fieldOfTypeWasFetched(type) && !typeIsExcluded({ pluginOptions, typeName: type.name }) ) { + let builtType + switch (type.kind) { case `UNION`: - buildType.unionType({ ...typeBuilderApi, type }) + builtType = buildType.unionType({ ...typeBuilderApi, type }) break case `INTERFACE`: - buildType.interfaceType({ ...typeBuilderApi, type }) + builtType = buildType.interfaceType({ ...typeBuilderApi, type }) break case `OBJECT`: - buildType.objectType({ ...typeBuilderApi, type }) + builtType = buildType.objectType({ ...typeBuilderApi, type }) break case `ENUM`: - buildType.enumType({ ...typeBuilderApi, type }) + builtType = buildType.enumType({ ...typeBuilderApi, type }) break case `SCALAR`: /** @@ -62,6 +64,10 @@ const customizeSchema = async ({ actions, schema }) => { */ break } + + if (builtType) { + typeDefs.push(builtType) + } } }) @@ -69,7 +75,7 @@ const customizeSchema = async ({ actions, schema }) => { // where the typename is the type prefix // The node fields are the non-node root fields of the remote schema // like so: query { prefix { ...fields } } - buildType.objectType({ + const wpType = buildType.objectType({ ...typeBuilderApi, type: { kind: `OBJECT`, @@ -81,6 +87,8 @@ const customizeSchema = async ({ actions, schema }) => { isAGatsbyNode: true, }) + typeDefs.push(wpType) + actions.createTypes(typeDefs) }