diff --git a/integration-tests/gatsby-source-wordpress/__tests__/__snapshots__/index.js.snap b/integration-tests/gatsby-source-wordpress/__tests__/__snapshots__/index.js.snap index fd6d2989ba730..595396781502d 100644 --- a/integration-tests/gatsby-source-wordpress/__tests__/__snapshots__/index.js.snap +++ b/integration-tests/gatsby-source-wordpress/__tests__/__snapshots__/index.js.snap @@ -551,6 +551,62 @@ Array [ ], "name": "WpComment", }, + Object { + "fields": Array [ + "databaseId", + "email", + "id", + "name", + "url", + "nodeType", + "parent", + "children", + "internal", + ], + "name": "WpCommentAuthor", + }, + Object { + "fields": Array [ + "totalCount", + "edges", + "nodes", + "pageInfo", + "distinct", + "group", + ], + "name": "WpCommentAuthorConnection", + }, + Object { + "fields": Array [ + "next", + "node", + "previous", + ], + "name": "WpCommentAuthorEdge", + }, + Object { + "fields": null, + "name": "WpCommentAuthorFieldsEnum", + }, + Object { + "fields": null, + "name": "WpCommentAuthorFilterInput", + }, + Object { + "fields": Array [ + "totalCount", + "edges", + "nodes", + "pageInfo", + "field", + "fieldValue", + ], + "name": "WpCommentAuthorGroupConnection", + }, + Object { + "fields": null, + "name": "WpCommentAuthorSortInput", + }, Object { "fields": Array [ "totalCount", diff --git a/integration-tests/gatsby-source-wordpress/gatsby-config.js b/integration-tests/gatsby-source-wordpress/gatsby-config.js index f94b45ce74135..b2a323b857d4c 100644 --- a/integration-tests/gatsby-source-wordpress/gatsby-config.js +++ b/integration-tests/gatsby-source-wordpress/gatsby-config.js @@ -48,6 +48,9 @@ const wpPluginOptions = !process.env.DEFAULT_PLUGIN_OPTIONS `registeredDate`, ], }, + Commenter: { + excludeFieldNames: [`databaseId`], + }, Post: { limit: process.env.NODE_ENV === `development` diff --git a/integration-tests/gatsby-source-wordpress/src/pages/index.js b/integration-tests/gatsby-source-wordpress/src/pages/index.js new file mode 100644 index 0000000000000..1cf1659123a3a --- /dev/null +++ b/integration-tests/gatsby-source-wordpress/src/pages/index.js @@ -0,0 +1,5 @@ +import React from "react" + +export default function index() { + return
Index page
+} diff --git a/integration-tests/gatsby-source-wordpress/test-fns/data-resolution.js b/integration-tests/gatsby-source-wordpress/test-fns/data-resolution.js index 5e8e508e35b93..d9ec8f17f6d98 100644 --- a/integration-tests/gatsby-source-wordpress/test-fns/data-resolution.js +++ b/integration-tests/gatsby-source-wordpress/test-fns/data-resolution.js @@ -29,12 +29,10 @@ describe(`data resolution`, () => { expect(data[`allWpPage`].totalCount).toBe(1) expect(data[`allWpPost`].totalCount).toBe(1) expect(data[`allWpComment`].totalCount).toBe(1) - // expect(data[`allWpProject`].totalCount).toBe(1) expect(data[`allWpTaxonomy`].totalCount).toBe(3) expect(data[`allWpCategory`].totalCount).toBe(9) expect(data[`allWpMenu`].totalCount).toBe(1) expect(data[`allWpMenuItem`].totalCount).toBe(4) - // expect(data[`allWpTeamMember`].totalCount).toBe(1) expect(data[`allWpPostFormat`].totalCount).toBe(0) expect(data[`allWpContentType`].totalCount).toBe(6) }) @@ -53,6 +51,61 @@ describe(`data resolution`, () => { }, }) + it(`resolves node interfaces without errors`, async () => { + const query = /* GraphQL */ ` + query { + allWpTermNode { + nodes { + id + } + } + + allWpContentNode { + nodes { + id + } + } + } + ` + + // this will throw if there are gql errors + const gatsbyResult = await fetchGraphql({ + url, + query, + }) + + expect(gatsbyResult.data.allWpTermNode.nodes.length).toBe(14) + expect(gatsbyResult.data.allWpContentNode.nodes.length).toBe(12) + }) + + it(`resolves interface fields which are a mix of Gatsby nodes and regular object data with no node`, async () => { + const query = /* GraphQL */ ` + query { + wpPost(id: { eq: "cG9zdDox" }) { + id + comments { + nodes { + author { + # this is an interface of WpUser (Gatsby node type) and WpCommentAuthor (no node for this so needs to be fetched on the comment) + node { + name + } + } + } + } + } + } + ` + + // this will throw an error if there are gql errors + const gatsbyResult = await fetchGraphql({ + url, + query, + }) + + expect(gatsbyResult.data.wpPost.comments.nodes.length).toBe(1) + }) + it(`resolves hierarchichal categories`, async () => { const gatsbyResult = await fetchGraphql({ url, 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 d3940964e6d26..2fcb59b4b94ec 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 @@ -64,6 +64,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 bd7e5d0735c70..e07f0e6665052 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, }, }