Skip to content

Commit 15ab773

Browse files
committed
support multiple reference fields for a single content type
1 parent cae6633 commit 15ab773

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
@@ -169,6 +169,11 @@ export const pageQuery = graphql`
169169
}
170170
... on ContentfulContentTypeContentReference {
171171
title
172+
... on ContentfulEntry {
173+
sys {
174+
id
175+
}
176+
}
172177
one {
173178
... on ContentfulContentTypeText {
174179
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)