Skip to content

Commit

Permalink
support multiple reference fields for a single content type
Browse files Browse the repository at this point in the history
  • Loading branch information
axe312ger committed May 20, 2021
1 parent 5f18b1d commit a0d7001
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
5 changes: 5 additions & 0 deletions e2e-tests/contentful/src/pages/content-reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ export const pageQuery = graphql`
}
... on ContentfulContentReference {
title
... on ContentfulEntry {
sys {
id
}
}
one {
... on ContentfulText {
title
Expand Down
17 changes: 14 additions & 3 deletions packages/gatsby-source-contentful/src/generate-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(``)
Expand Down

0 comments on commit a0d7001

Please sign in to comment.