Skip to content

Commit a0d7001

Browse files
committed
support multiple reference fields for a single content type
1 parent 5f18b1d commit a0d7001

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

e2e-tests/contentful/src/pages/content-reference.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ export const pageQuery = graphql`
115115
}
116116
... on ContentfulContentReference {
117117
title
118+
... on ContentfulEntry {
119+
sys {
120+
id
121+
}
122+
}
118123
one {
119124
... on ContentfulText {
120125
title

packages/gatsby-source-contentful/src/generate-schema.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,27 @@ const getLinkFieldType = (linkType, field, schema, createTypes) => {
8484
// { validations: [ { linkContentType: [Array] } ] }
8585
if (linkContentTypeValidation) {
8686
const { linkContentType } = linkContentTypeValidation
87+
const contentTypes = Array.isArray(linkContentType)
88+
? linkContentType
89+
: [linkContentType]
8790

8891
// Full type names for union members, shorter variant for the union type name
89-
const translatedTypeNames = linkContentType.map(typeName =>
92+
const translatedTypeNames = contentTypes.map(typeName =>
9093
makeTypeName(typeName)
9194
)
92-
const shortTypeNames = linkContentType.map(typeName =>
95+
const shortTypeNames = contentTypes.map(typeName =>
9396
makeTypeName(typeName, ``)
9497
)
9598

96-
// @todo Single content type
99+
// Single content type
100+
if (translatedTypeNames.length === 1) {
101+
return {
102+
type: translatedTypeNames.shift(),
103+
extensions: {
104+
link: { by: `id`, from: `${field.id}___NODE` },
105+
},
106+
}
107+
}
97108

98109
// Multiple content types
99110
const unionName = [`UnionContentful`, ...shortTypeNames].join(``)

0 commit comments

Comments
 (0)