-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
628 lines (577 loc) · 15.7 KB
/
gatsby-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
const { getGatsbyImageResolver } = require("gatsby-plugin-image/graphql-utils")
exports.createSchemaCustomization = async ({ actions }) => {
actions.createFieldExtension({
name: "wpImagePassthroughResolver",
extend(options) {
const { args } = getGatsbyImageResolver()
return {
args,
async resolve(source, args, context, info) {
const imageType = info.schema.getType("ImageSharp")
const file = context.nodeModel.getNodeById({
id: source.localFile?.id,
})
if (!file) return null
const image = context.nodeModel.getNodeById({
id: file.children[0],
})
const resolver = imageType.getFields().gatsbyImageData.resolve
if (!resolver) return null
return await resolver(image, args, context, info)
},
}
},
})
actions.createFieldExtension({
name: "wpRecursiveImage",
extend(options) {
return {
async resolve(source, args, context, info) {
return source
},
}
},
})
// interfaces
actions.createTypes(/* GraphQL */ `
interface HomepageImage implements Node {
id: ID!
alt: String
gatsbyImageData: GatsbyImageData @wpImagePassthroughResolver
image: HomepageImage
localFile: File
url: String
}
interface HomepageBlock implements Node {
id: ID!
blocktype: String
}
`)
// blocks
actions.createTypes(/* GraphQL */ `
type HomepageLink implements Node {
id: ID!
href: String
text: String
}
type HomepageHero implements Node & HomepageBlock {
id: ID!
blocktype: String
heading: String!
kicker: String
subhead: String
image: HomepageImage @link
text: String
links: [HomepageLink] @link
}
type HomepageCta implements Node & HomepageBlock {
id: ID!
blocktype: String
kicker: String
heading: String
text: String
links: [HomepageLink] @link
image: HomepageImage @link
}
type HomepageFeature implements Node & HomepageBlock {
id: ID!
blocktype: String
heading: String
kicker: String
text: String
image: HomepageImage @link
links: [HomepageLink] @link
}
type HomepageTestimonial implements Node {
id: ID!
quote: String
source: String
avatar: HomepageImage @link
}
type HomepageBenefit implements Node {
id: ID!
heading: String
text: String
image: HomepageImage @link
}
type HomepageLogo implements Node {
id: ID!
image: HomepageImage @link
alt: String @proxy(from: "image.title")
}
type HomepageProduct implements Node {
id: ID!
heading: String
text: String
image: HomepageImage @link
links: [HomepageLink] @link
}
type HomepageFeatureList implements Node & HomepageBlock {
id: ID!
blocktype: String
kicker: String
heading: String
text: String
content: [HomepageFeature] @link
}
type HomepageLogoList implements Node & HomepageBlock {
id: ID!
blocktype: String
text: String
logos: [HomepageImage] @link
}
type HomepageTestimonialList implements Node & HomepageBlock {
id: ID!
blocktype: String
kicker: String
heading: String
content: [HomepageTestimonial] @link
}
type HomepageBenefitList implements Node & HomepageBlock {
id: ID!
blocktype: String
heading: String
text: String
content: [HomepageBenefit] @link
}
type HomepageStat implements Node {
id: ID!
value: String
label: String
heading: String
}
type HomepageStatList implements Node & HomepageBlock {
id: ID!
blocktype: String
kicker: String
heading: String
text: String
image: HomepageImage @link
icon: HomepageImage @link
content: [HomepageStat] @link
links: [HomepageLink] @link
}
type HomepageProductList implements Node & HomepageBlock {
id: ID!
blocktype: String
kicker: String
heading: String
text: String
content: [HomepageProduct] @link
}
type AboutHero implements Node & HomepageBlock {
id: ID!
blocktype: String
heading: String
text: String
image: HomepageImage @link
}
type AboutStat implements Node & HomepageBlock {
id: ID!
blocktype: String
value: String
label: String
}
type AboutStatList implements Node & HomepageBlock {
id: ID!
blocktype: String
content: [AboutStat] @link
}
type AboutProfile implements Node {
id: ID!
image: HomepageImage @link
name: String
jobTitle: String
}
type AboutLeadership implements Node & HomepageBlock {
id: ID!
blocktype: String
kicker: String
heading: String
subhead: String
content: [AboutProfile] @link
}
type AboutLogoList implements Node & HomepageBlock {
id: ID!
blocktype: String
heading: String
links: [HomepageLink] @link
logos: [HomepageImage] @link
}
`)
// pages
actions.createTypes(/* GraphQL */ `
type Homepage implements Node {
id: ID!
title: String
description: String
image: HomepageImage @link
content: [HomepageBlock] @link
}
type AboutPage implements Node {
id: ID!
title: String
description: String
image: HomepageImage @link
content: [HomepageBlock] @link
}
type Page implements Node {
id: ID!
slug: String!
title: String
description: String
image: HomepageImage @link
html: String
}
`)
// WordPress types
actions.createTypes(/* GraphQL */ `
type WpMediaItem implements Node & RemoteFile & HomepageImage {
id: ID!
alt: String @proxy(from: "altText")
altText: String
gatsbyImageData: GatsbyImageData @wpImagePassthroughResolver
image: HomepageImage @wpRecursiveImage
localFile: File
url: String @proxy(from: "mediaItemUrl")
mediaItemUrl: String
}
`)
}
exports.onCreateNode = ({
node,
actions,
getNode,
createNodeId,
createContentDigest,
reporter,
}) => {
if (!node.internal.type.includes("Wp")) return
const createLinkNode =
(parent) =>
({ url, title, ...rest }, i) => {
const id = createNodeId(`${parent.id} >>> HomepageLink ${url} ${i}`)
actions.createNode({
id,
internal: {
type: "HomepageLink",
contentDigest: createContentDigest({ url, title }),
},
href: url,
text: title,
})
return id
}
const createItemNode = (parent, type) => (data, i) => {
const id = createNodeId(`${parent.id} >>> ${type} ${i}`)
if (data.image) {
data.image = data.image?.id
}
if (data.avatar) {
data.avatar = data.avatar?.id
}
if (Array.isArray(data.link)) {
data.links = data.link.filter(Boolean).map(createLinkNode(parent))
}
actions.createNode({
...data,
id,
internal: {
type,
contentDigest: createContentDigest(data),
},
})
return id
}
if (node.internal.type === "WpPage") {
switch (node.slug) {
case "homepage":
// prettier-ignore
const {
description,
hero,
logoList,
featureList,
productList,
benefitList,
statList,
testimonialList,
cta,
} = node.homepage
const content = {
features: [featureList.feature1, featureList.feature2]
.filter(Boolean)
.map((feature) => ({
...feature,
blocktype: "Feature",
}))
.map(createItemNode(node, "HomepageFeature")),
products: [
productList.product1,
productList.product2,
productList.product3,
]
.filter(Boolean)
.map(createItemNode(node, "HomepageProduct")),
benefits: [
benefitList.benefit1,
benefitList.benefit2,
benefitList.benefit3,
]
.filter(Boolean)
.map(createItemNode(node, "HomepageBenefit")),
stats: [statList.stat1, statList.stat2, statList.stat3]
.filter(Boolean)
.map(createItemNode(node, "HomepageStat")),
testimonials: [
testimonialList.testimonial1,
testimonialList.testimonial2,
testimonialList.testimonial3,
testimonialList.testimonial4,
]
.filter(Boolean)
.map(createItemNode(node, "HomepageTestimonial")),
}
const blocks = {
hero: {
id: createNodeId(`${node.id} >>> HomepageHero`),
...hero,
image: hero.image?.id,
links: [hero.cta1, hero.cta2]
.filter(Boolean)
.map(createLinkNode(node.id)),
},
logoList: {
id: createNodeId(`${node.id} >>> HomepageLogoList`),
...logoList,
logos: logoList.logos?.filter(Boolean).map((logo) => logo.id) || [],
},
featureList: {
id: createNodeId(`${node.id} >>> HomepageFeatureList`),
...featureList,
content: content.features,
},
productList: {
id: createNodeId(`${node.id} >>> HomepageProductList`),
...productList,
content: content.products,
},
benefitList: {
id: createNodeId(`${node.id} >>> HomepageBenefitList`),
...benefitList,
content: content.benefits,
},
statList: {
id: createNodeId(`${node.id} >>> HomepageStatList`),
...statList,
image: statList.image?.id,
icon: statList.icon?.id,
links: [statList.link].filter(Boolean).map(createLinkNode(node.id)),
content: content.stats,
},
testimonialList: {
id: createNodeId(`${node.id} >>> HomepageTestimonialList`),
...testimonialList,
content: content.testimonials,
},
cta: {
id: createNodeId(`${node.id} >>> HompageCta`),
...cta,
image: cta.image?.id,
links: [cta.link1, cta.link2]
.filter(Boolean)
.map(createLinkNode(node.id)),
},
}
actions.createNode({
...blocks.hero,
blocktype: "HomepageHero",
internal: {
type: "HomepageHero",
contentDigest: node.internal.contentDigest,
},
})
actions.createNode({
...blocks.logoList,
blocktype: "HomepageLogoList",
internal: {
type: "HomepageLogoList",
contentDigest: node.internal.contentDigest,
},
})
actions.createNode({
...blocks.featureList,
blocktype: "HomepageFeatureList",
internal: {
type: "HomepageFeatureList",
contentDigest: node.internal.contentDigest,
},
})
actions.createNode({
...blocks.productList,
blocktype: "HomepageProductList",
internal: {
type: "HomepageProductList",
contentDigest: node.internal.contentDigest,
},
})
actions.createNode({
...blocks.benefitList,
blocktype: "HomepageBenefitList",
internal: {
type: "HomepageBenefitList",
contentDigest: node.internal.contentDigest,
},
})
actions.createNode({
...blocks.statList,
blocktype: "HomepageStatList",
internal: {
type: "HomepageStatList",
contentDigest: node.internal.contentDigest,
},
})
actions.createNode({
...blocks.testimonialList,
blocktype: "HomepageTestimonialList",
internal: {
type: "HomepageTestimonialList",
contentDigest: node.internal.contentDigest,
},
})
actions.createNode({
...blocks.cta,
blocktype: "HomepageCta",
internal: {
type: "HomepageCta",
contentDigest: node.internal.contentDigest,
},
})
actions.createNode({
...node.homepage,
id: createNodeId(`${node.id} >>> Homepage`),
internal: {
type: "Homepage",
contentDigest: node.internal.contentDigest,
},
parent: node.id,
title: node.title,
description,
image: node.featuredImage?.node?.id,
content: [
blocks.hero.id,
blocks.logoList.id,
blocks.productList.id,
blocks.featureList.id,
blocks.benefitList.id,
blocks.statList.id,
blocks.testimonialList.id,
blocks.cta.id,
],
})
break
default:
actions.createNode({
...node.page,
id: createNodeId(`${node.id} >>> Page ${node.slug}`),
internal: {
type: "Page",
contentDigest: node.internal.contentDigest,
},
parent: node.id,
slug: node.slug,
title: node.title,
description: node.page?.description,
image: node.featuredImage?.node?.id,
html: node.content,
})
break
}
}
}
exports.createPages = ({ actions }) => {
const { createSlice } = actions
createSlice({
id: "header",
component: require.resolve("./src/components/header.js"),
})
createSlice({
id: "footer",
component: require.resolve("./src/components/footer.js"),
})
}
// in gatsby-node.js
exports.createSchemaCustomization = async ({ actions }) => {
// ...
actions.createTypes(`
type HomepageBanner implements Node & HomepageBlock {
id: ID!
blocktype: String
heading: String
text: String
}
`)
// ...
}
// ...
exports.onCreateNode = ({ actions, node, createNodeId, createContentDigest }) => {
}
// ...
switch (node.internal.type) {
case "WpPage":
if (node.slug !== "homepage") return
const {
homepageHero,
homepageCta,
statList,
testimonialList,
productList,
logoList,
featureList,
benefitList,
// add the new custom field group here
homepageBanner,
} = node
const heroID = createNodeId(`${node.id} >>> HomepageHero`)
// create an node id for the field group
const bannerID = createNodeId(`${node.id} >>> HomepageBanner`)
// ...
// create a new node for this field group
actions.createNode({
id: bannerID,
internal: {
type: "HomepageBanner",
contentDigest: createContentDigest(JSON.stringify(homepageBanner)),
},
parent: node.id,
blocktype: "HomepageBanner",
heading: homepageBanner.bannerHeading,
text: homepageBanner.bannerText,
})
// ...
actions.createNode({
...node,
id: createNodeId(`${node.id} >>> Homepage`),
internal: {
type: "Homepage",
contentDigest: node.internal.contentDigest,
},
parent: node.id,
blocktype: "Homepage",
image: node.featuredImageId,
content: [
heroID,
logosID,
// add your banner content in the postion you would like it to appear on the page
bannerID,
productsID,
featuresID,
benefitsID,
statsID,
testimonialsID,
ctaID,
],
})
// ...
}
}