From a0d70017c9846675600d83fcbcce0f576abb230c Mon Sep 17 00:00:00 2001 From: axe312ger Date: Thu, 20 May 2021 17:43:14 +0200 Subject: [PATCH] support multiple reference fields for a single content type --- .../contentful/src/pages/content-reference.js | 5 +++++ .../src/generate-schema.js | 17 ++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/e2e-tests/contentful/src/pages/content-reference.js b/e2e-tests/contentful/src/pages/content-reference.js index cda2d6bea69a7..af22aaf056014 100644 --- a/e2e-tests/contentful/src/pages/content-reference.js +++ b/e2e-tests/contentful/src/pages/content-reference.js @@ -115,6 +115,11 @@ export const pageQuery = graphql` } ... on ContentfulContentReference { title + ... on ContentfulEntry { + sys { + id + } + } one { ... on ContentfulText { title diff --git a/packages/gatsby-source-contentful/src/generate-schema.js b/packages/gatsby-source-contentful/src/generate-schema.js index 556efc3f5c446..541ad2429f67e 100644 --- a/packages/gatsby-source-contentful/src/generate-schema.js +++ b/packages/gatsby-source-contentful/src/generate-schema.js @@ -84,16 +84,27 @@ const getLinkFieldType = (linkType, field, schema, createTypes) => { // { validations: [ { linkContentType: [Array] } ] } if (linkContentTypeValidation) { const { linkContentType } = linkContentTypeValidation + const contentTypes = Array.isArray(linkContentType) + ? linkContentType + : [linkContentType] // Full type names for union members, shorter variant for the union type name - const translatedTypeNames = linkContentType.map(typeName => + const translatedTypeNames = contentTypes.map(typeName => makeTypeName(typeName) ) - const shortTypeNames = linkContentType.map(typeName => + const shortTypeNames = contentTypes.map(typeName => makeTypeName(typeName, ``) ) - // @todo Single content type + // Single content type + if (translatedTypeNames.length === 1) { + return { + type: translatedTypeNames.shift(), + extensions: { + link: { by: `id`, from: `${field.id}___NODE` }, + }, + } + } // Multiple content types const unionName = [`UnionContentful`, ...shortTypeNames].join(``)