Skip to content

Commit

Permalink
fix(gatsby-source-wordpress): auto-rename types named "Filter" (#29718)…
Browse files Browse the repository at this point in the history
… (#29884)

Co-authored-by: gatsbybot <mathews.kyle+gatsbybot@gmail.com>
(cherry picked from commit fb225be)

Co-authored-by: Tyler Barnes <tyler@gatsbyjs.com>
  • Loading branch information
GatsbyJS Bot and TylerBarnes committed Mar 1, 2021
1 parent c2ea9b9 commit 8986b12
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
Expand Up @@ -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(
Expand All @@ -22,7 +22,7 @@ const unionType = typeBuilderApi => {
.map(possibleType => buildTypeName(possibleType.name))

if (!types || !types.length) {
return
return false
}

let unionType = {
Expand All @@ -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,
Expand Down Expand Up @@ -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 => {
Expand All @@ -120,7 +119,6 @@ const objectType = typeBuilderApi => {
gatsbyNodeTypes,
fieldAliases,
fieldBlacklist,
typeDefs,
schema,
isAGatsbyNode,
} = typeBuilderApi
Expand All @@ -138,7 +136,7 @@ const objectType = typeBuilderApi => {
// TypeError: Cannot convert undefined or null to object at Function.keys (<anonymous>)
// Also cause wordpress blog site build failure in createSchemaCustomization step
if (!transformedFields || !Object.keys(transformedFields).length) {
return
return false
}

let objectType = {
Expand Down Expand Up @@ -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 }
Expand Up @@ -20,6 +20,10 @@ export const buildTypeName = name => {
return name
}

if (name === `Filter`) {
name = `FilterType`
}

return prefix + name
}

Expand Down
Expand Up @@ -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`:
/**
Expand All @@ -62,14 +64,18 @@ const customizeSchema = async ({ actions, schema }) => {
*/
break
}

if (builtType) {
typeDefs.push(builtType)
}
}
})

// Create non Gatsby node types by creating a single node
// 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`,
Expand All @@ -81,6 +87,8 @@ const customizeSchema = async ({ actions, schema }) => {
isAGatsbyNode: true,
})

typeDefs.push(wpType)

actions.createTypes(typeDefs)
}

Expand Down

0 comments on commit 8986b12

Please sign in to comment.