Skip to content

Commit bf317a5

Browse files
committed
refactor: update e2e test schema
WIP - first try to use union for reference field with validations fix multi reference fields shorten union names support multiple reference fields for a single content type fix: type name generation remove comment introduce validation for single reference fields
1 parent b7687f8 commit bf317a5

File tree

5 files changed

+119
-30
lines changed

5 files changed

+119
-30
lines changed

e2e-tests/contentful/schema.gql

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ type SiteBuildMetadata implements Node @dontInfer {
124124

125125
interface ContentfulReference implements Node {
126126
id: ID!
127-
sys: ContentfulSys
127+
sys: ContentfulSys!
128128
}
129129

130130
type ContentfulSys {
@@ -145,13 +145,23 @@ type ContentfulContentType implements Node @dontInfer {
145145
description: String!
146146
}
147147

148-
interface ContentfulEntry implements Node {
148+
interface ContentfulEntry implements ContentfulReference & Node {
149149
id: ID!
150-
sys: ContentfulSys
150+
sys: ContentfulSys!
151+
metadata: ContentfulMetadata!
152+
}
153+
154+
type ContentfulMetadata @dontInfer {
155+
tags: [ContentfulTag]! @link(by: "id", from: "tags___NODE")
156+
}
157+
158+
type ContentfulTag implements Node @dontInfer {
159+
name: String!
160+
contentful_id: String!
151161
}
152162

153163
type ContentfulAsset implements ContentfulReference & Node @dontInfer {
154-
sys: ContentfulSys
164+
sys: ContentfulSys!
155165
title: String
156166
description: String
157167
contentType: String
@@ -203,7 +213,8 @@ type ContentfulText implements Node @dontInfer {
203213
}
204214

205215
type ContentfulContentTypeNumber implements ContentfulReference & ContentfulEntry & Node @dontInfer {
206-
sys: ContentfulSys
216+
sys: ContentfulSys!
217+
metadata: ContentfulMetadata!
207218
title: String
208219
integer: Int
209220
integerLocalized: Int
@@ -212,7 +223,8 @@ type ContentfulContentTypeNumber implements ContentfulReference & ContentfulEntr
212223
}
213224

214225
type ContentfulContentTypeText implements ContentfulReference & ContentfulEntry & Node @dontInfer {
215-
sys: ContentfulSys
226+
sys: ContentfulSys!
227+
metadata: ContentfulMetadata!
216228
title: String
217229
short: String
218230
shortLocalized: String
@@ -224,7 +236,8 @@ type ContentfulContentTypeText implements ContentfulReference & ContentfulEntry
224236
}
225237

226238
type ContentfulContentTypeMediaReference implements ContentfulReference & ContentfulEntry & Node @dontInfer {
227-
sys: ContentfulSys
239+
sys: ContentfulSys!
240+
metadata: ContentfulMetadata!
228241
title: String
229242
one: ContentfulAsset @link(by: "id", from: "one___NODE")
230243
oneLocalized: ContentfulAsset @link(by: "id", from: "oneLocalized___NODE")
@@ -233,14 +246,16 @@ type ContentfulContentTypeMediaReference implements ContentfulReference & Conten
233246
}
234247

235248
type ContentfulContentTypeBoolean implements ContentfulReference & ContentfulEntry & Node @dontInfer {
236-
sys: ContentfulSys
249+
sys: ContentfulSys!
250+
metadata: ContentfulMetadata!
237251
title: String
238252
boolean: Boolean
239253
booleanLocalized: Boolean
240254
}
241255

242256
type ContentfulContentTypeDate implements ContentfulReference & ContentfulEntry & Node @dontInfer {
243-
sys: ContentfulSys
257+
sys: ContentfulSys!
258+
metadata: ContentfulMetadata!
244259
title: String
245260
date: Date @dateformat
246261
dateTime: Date @dateformat
@@ -249,29 +264,33 @@ type ContentfulContentTypeDate implements ContentfulReference & ContentfulEntry
249264
}
250265

251266
type ContentfulContentTypeLocation implements ContentfulReference & ContentfulEntry & Node @dontInfer {
252-
sys: ContentfulSys
267+
sys: ContentfulSys!
268+
metadata: ContentfulMetadata!
253269
title: String
254270
location: ContentfulLocation
255271
locationLocalized: ContentfulLocation
256272
}
257273

258274
type ContentfulContentTypeJson implements ContentfulReference & ContentfulEntry & Node @dontInfer {
259-
sys: ContentfulSys
275+
sys: ContentfulSys!
276+
metadata: ContentfulMetadata!
260277
title: String
261278
json: JSON
262279
jsonLocalized: JSON
263280
}
264281

265282
type ContentfulContentTypeRichText implements ContentfulReference & ContentfulEntry & Node @dontInfer {
266-
sys: ContentfulSys
283+
sys: ContentfulSys!
284+
metadata: ContentfulMetadata!
267285
title: String
268286
richText: ContentfulRichText
269287
richTextLocalized: ContentfulRichText
270288
richTextValidated: ContentfulRichText
271289
}
272290

273291
type ContentfulContentTypeContentReference implements ContentfulReference & ContentfulEntry & Node @dontInfer {
274-
sys: ContentfulSys
292+
sys: ContentfulSys!
293+
metadata: ContentfulMetadata!
275294
title: String
276295
one: ContentfulEntry @link(by: "id", from: "one___NODE")
277296
oneLocalized: ContentfulEntry @link(by: "id", from: "oneLocalized___NODE")
@@ -280,7 +299,8 @@ type ContentfulContentTypeContentReference implements ContentfulReference & Cont
280299
}
281300

282301
type ContentfulContentTypeValidatedContentReference implements ContentfulReference & ContentfulEntry & Node @dontInfer {
283-
sys: ContentfulSys
302+
sys: ContentfulSys!
303+
metadata: ContentfulMetadata!
284304
title: String
285305
oneItemSingleType: ContentfulEntry @link(by: "id", from: "oneItemSingleType___NODE")
286306
oneItemManyTypes: ContentfulEntry @link(by: "id", from: "oneItemManyTypes___NODE")

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,10 @@ export const pageQuery = graphql`
109109
}
110110
one {
111111
__typename
112-
sys {
113-
id
112+
... on ContentfulEntry {
113+
sys {
114+
id
115+
}
114116
}
115117
... on ContentfulContentTypeText {
116118
title
@@ -152,8 +154,10 @@ export const pageQuery = graphql`
152154
}
153155
many {
154156
__typename
155-
sys {
156-
id
157+
... on ContentfulEntry {
158+
sys {
159+
id
160+
}
157161
}
158162
... on ContentfulContentTypeText {
159163
title
@@ -165,6 +169,11 @@ export const pageQuery = graphql`
165169
}
166170
... on ContentfulContentTypeContentReference {
167171
title
172+
... on ContentfulEntry {
173+
sys {
174+
id
175+
}
176+
}
168177
one {
169178
... on ContentfulContentTypeText {
170179
title

e2e-tests/contentful/src/pages/rich-text.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,10 @@ export const pageQuery = graphql`
156156
title
157157
one {
158158
__typename
159-
sys {
160-
id
159+
... on ContentfulEntry {
160+
sys {
161+
id
162+
}
161163
}
162164
... on ContentfulContentTypeText {
163165
title
@@ -179,8 +181,10 @@ export const pageQuery = graphql`
179181
}
180182
many {
181183
__typename
182-
sys {
183-
id
184+
... on ContentfulEntry {
185+
sys {
186+
id
187+
}
184188
}
185189
... on ContentfulContentTypeText {
186190
title

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

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,64 @@ const ContentfulDataTypes = new Map([
7070
],
7171
])
7272

73-
const getLinkFieldType = (linkType, field) => {
73+
const unionsNameSet = new Set()
74+
75+
const getLinkFieldType = (linkType, field, schema, createTypes) => {
76+
// Check for validations
77+
const validations =
78+
field.type === `Array` ? field.items?.validations : field?.validations
79+
80+
if (validations) {
81+
// We only handle content type validations
82+
const linkContentTypeValidation = validations.find(
83+
({ linkContentType }) => !!linkContentType
84+
)
85+
if (linkContentTypeValidation) {
86+
const { linkContentType } = linkContentTypeValidation
87+
const contentTypes = Array.isArray(linkContentType)
88+
? linkContentType
89+
: [linkContentType]
90+
91+
// Full type names for union members, shorter variant for the union type name
92+
const translatedTypeNames = contentTypes.map(typeName =>
93+
makeTypeName(typeName)
94+
)
95+
const shortTypeNames = contentTypes.map(typeName =>
96+
makeTypeName(typeName, ``)
97+
)
98+
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+
}
108+
109+
// Multiple content types
110+
const unionName = [`UnionContentful`, ...shortTypeNames].join(``)
111+
112+
if (!unionsNameSet.has(unionName)) {
113+
unionsNameSet.add(unionName)
114+
createTypes(
115+
schema.buildUnionType({
116+
name: unionName,
117+
types: translatedTypeNames,
118+
})
119+
)
120+
}
121+
122+
return {
123+
type: unionName,
124+
extensions: {
125+
link: { by: `id`, from: `${field.id}___NODE` },
126+
},
127+
}
128+
}
129+
}
130+
74131
return {
75132
type: `Contentful${linkType}`,
76133
extensions: {
@@ -79,19 +136,19 @@ const getLinkFieldType = (linkType, field) => {
79136
}
80137
}
81138

82-
const translateFieldType = field => {
139+
const translateFieldType = (field, schema, createTypes) => {
83140
let fieldType
84141
if (field.type === `Array`) {
85142
// Arrays of Contentful Links or primitive types
86143
const fieldData =
87144
field.items.type === `Link`
88-
? getLinkFieldType(field.items.linkType, field)
89-
: translateFieldType(field.items)
145+
? getLinkFieldType(field.items.linkType, field, schema, createTypes)
146+
: translateFieldType(field.items, schema, createTypes)
90147

91148
fieldType = { ...fieldData, type: `[${fieldData.type}]` }
92149
} else if (field.type === `Link`) {
93150
// Contentful Link (reference) field types
94-
fieldType = getLinkFieldType(field.linkType, field)
151+
fieldType = getLinkFieldType(field.linkType, field, schema, createTypes)
95152
} else {
96153
// Primitive field types
97154
fieldType = ContentfulDataTypes.get(field.type)(field)
@@ -371,7 +428,7 @@ export function generateSchema({
371428
if (field.disabled || field.omitted) {
372429
return
373430
}
374-
fields[field.id] = translateFieldType(field)
431+
fields[field.id] = translateFieldType(field, schema, createTypes)
375432
})
376433

377434
const type = pluginConfig.get(`useNameForId`)

packages/gatsby-source-contentful/src/normalize.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// @ts-check
22
import _ from "lodash"
33

4-
const typePrefix = `ContentfulContentType`
5-
export const makeTypeName = type =>
4+
export const makeTypeName = (type, typePrefix = `ContentfulContentType`) =>
65
_.upperFirst(_.camelCase(`${typePrefix} ${type}`))
76

87
export const getLocalizedField = ({ field, locale, localesFallback }) => {

0 commit comments

Comments
 (0)