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 d5d30bfc87c67..2c61e1b95b988 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 @@ -29,10 +29,6 @@ const unionType = typeBuilderApi => { name: buildTypeName(type.name), types, resolveType: node => { - if (node.type) { - return buildTypeName(node.type) - } - if (node.__typename) { return buildTypeName(node.__typename) } @@ -104,7 +100,7 @@ const interfaceType = typeBuilderApi => { } else { // otherwise this is a regular interface type so we need to resolve the type name typeDef.resolveType = node => - node && node.__typename ? buildTypeName(node.__typename) : null + node?.__typename ? buildTypeName(node.__typename) : null } // @todo add this as a plugin option 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 d08ab8ce82697..0afa669a0dd34 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 @@ -24,6 +24,10 @@ export const buildTypeName = name => { name = `FilterType` } + if (name.startsWith(prefix)) { + return name + } + 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 e72877387a96f..fa6dd48b747f2 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 @@ -5,6 +5,8 @@ import { fieldOfTypeWasFetched } from "./helpers" import buildType from "./build-types" import { getGatsbyNodeTypeNames } from "../source-nodes/fetch-nodes/fetch-nodes" import { typeIsExcluded } from "~/steps/ingest-remote-schema/is-excluded" +import { formatLogMessage } from "../../utils/format-log-message" +import { CODES } from "../../utils/report" /** * createSchemaCustomization @@ -96,7 +98,13 @@ const createSchemaCustomization = async api => { try { await customizeSchema(api) } catch (e) { - api.reporter.panic(e) + api.reporter.panic({ + id: CODES.SourcePluginCodeError, + error: e, + context: { + sourceMessage: formatLogMessage(e.message), + }, + }) } } diff --git a/packages/gatsby-source-wordpress/src/steps/create-schema-customization/transform-fields/default-resolver.js b/packages/gatsby-source-wordpress/src/steps/create-schema-customization/transform-fields/default-resolver.js index de217f48e02f9..1c3c8e55ad8ec 100644 --- a/packages/gatsby-source-wordpress/src/steps/create-schema-customization/transform-fields/default-resolver.js +++ b/packages/gatsby-source-wordpress/src/steps/create-schema-customization/transform-fields/default-resolver.js @@ -1,6 +1,7 @@ import { findTypeName } from "~/steps/create-schema-customization/helpers" import { buildGatsbyNodeObjectResolver } from "~/steps/create-schema-customization/transform-fields/transform-object" +import { buildTypeName } from "../helpers" export const buildDefaultResolver = transformerApi => (source, _, context) => { const { fieldName, field, gatsbyNodeTypes } = transformerApi @@ -45,6 +46,12 @@ export const buildDefaultResolver = transformerApi => (source, _, context) => { finalFieldValue = aliasedField2 } + if (finalFieldValue?.__typename) { + // in Gatsby V3 this property is used to determine the type of an interface field + // instead of the resolveType fn. This means we need to prefix it so that gql doesn't throw errors about missing types. + finalFieldValue.__typename = buildTypeName(finalFieldValue.__typename) + } + const isANodeConnection = // if this field has just an id and typename finalFieldValue?.id && diff --git a/packages/gatsby-source-wordpress/src/steps/create-schema-customization/transform-fields/transform-object.js b/packages/gatsby-source-wordpress/src/steps/create-schema-customization/transform-fields/transform-object.js index 6c23ef581df32..2037b153963ea 100644 --- a/packages/gatsby-source-wordpress/src/steps/create-schema-customization/transform-fields/transform-object.js +++ b/packages/gatsby-source-wordpress/src/steps/create-schema-customization/transform-fields/transform-object.js @@ -55,6 +55,12 @@ export const buildGatsbyNodeObjectResolver = ({ field, fieldName }) => async ( const queryInfo = getQueryInfoByTypeName(field.type.name) + if (!queryInfo) { + // if we don't have query info for a type + // it probably means this type is excluded in plugin options + return null + } + const isLazyMediaItem = queryInfo.typeInfo.nodesTypeName === `MediaItem` && queryInfo.settings.lazyNodes diff --git a/packages/gatsby-source-wordpress/src/steps/create-schema-customization/transform-fields/transform-union.js b/packages/gatsby-source-wordpress/src/steps/create-schema-customization/transform-fields/transform-union.js index 052e57ced694a..b79f568344e66 100644 --- a/packages/gatsby-source-wordpress/src/steps/create-schema-customization/transform-fields/transform-union.js +++ b/packages/gatsby-source-wordpress/src/steps/create-schema-customization/transform-fields/transform-union.js @@ -16,12 +16,10 @@ export const transformUnion = ({ field, fieldName }) => { if (gatsbyNode) { return gatsbyNode - } else { - return null } } - return resolvedField + return resolvedField ?? null }, } } diff --git a/packages/gatsby-source-wordpress/src/steps/ingest-remote-schema/build-queries-from-introspection/recursively-transform-fields.js b/packages/gatsby-source-wordpress/src/steps/ingest-remote-schema/build-queries-from-introspection/recursively-transform-fields.js index 0ee6a7bca018f..8245f8d3e947a 100644 --- a/packages/gatsby-source-wordpress/src/steps/ingest-remote-schema/build-queries-from-introspection/recursively-transform-fields.js +++ b/packages/gatsby-source-wordpress/src/steps/ingest-remote-schema/build-queries-from-introspection/recursively-transform-fields.js @@ -303,10 +303,10 @@ export function transformField({ const isAGatsbyNode = // if this is a gatsby node type gatsbyNodesInfo.typeNames.includes(typeName) || - // or this type has a possible type which is a gatsby node type + // or all possible types on this type are Gatsby node types typeMap .get(typeName) - ?.possibleTypes?.find(possibleType => + ?.possibleTypes?.every(possibleType => gatsbyNodesInfo.typeNames.includes(possibleType.name) ) diff --git a/packages/gatsby-source-wordpress/src/steps/source-nodes/create-nodes/create-nodes.js b/packages/gatsby-source-wordpress/src/steps/source-nodes/create-nodes/create-nodes.js index 262056ebd449d..e99f4bd2ba751 100644 --- a/packages/gatsby-source-wordpress/src/steps/source-nodes/create-nodes/create-nodes.js +++ b/packages/gatsby-source-wordpress/src/steps/source-nodes/create-nodes/create-nodes.js @@ -48,13 +48,16 @@ export const createNodeWithSideEffects = ({ }) } + const builtTypename = buildTypeName(node.__typename) + let remoteNode = { ...node, + __typename: builtTypename, id: node.id, parent: null, internal: { contentDigest: createContentDigest(node), - type: type || buildTypeName(node.type), + type: type || builtTypename, }, }