-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
recursively-transform-fields.js
732 lines (630 loc) · 18.6 KB
/
recursively-transform-fields.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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
import { getStore } from "~/store"
import {
getTypeSettingsByType,
findNamedTypeName,
findTypeKind,
} from "~/steps/create-schema-customization/helpers"
import {
fieldIsExcludedOnParentType,
fieldIsExcludedOnAll,
} from "~/steps/ingest-remote-schema/is-excluded"
import { returnAliasedFieldName } from "~/steps/create-schema-customization/transform-fields"
import { typeIsExcluded } from "../is-excluded"
export const transformInlineFragments = ({
possibleTypes,
gatsbyNodesInfo,
typeMap,
maxDepth,
parentType,
mainType,
parentField,
fragments,
circularQueryLimit,
buildGatsbyNodeFields = false,
depth = 0,
buildingFragment = false,
ancestorTypeNames: parentAncestorTypeNames = [],
}) => {
const state = getStore().getState()
if (!typeMap) {
typeMap = state.remoteSchema.typeMap
}
const { pluginOptions } = state.gatsbyApi
if (!maxDepth) {
maxDepth = pluginOptions.schema.queryDepth
}
if (!circularQueryLimit) {
circularQueryLimit = pluginOptions.circularQueryLimit
}
if (!gatsbyNodesInfo) {
gatsbyNodesInfo = state.remoteSchema.gatsbyNodesInfo
}
const ancestorTypeNames = [...parentAncestorTypeNames]
const transformedInlineFragments = possibleTypes
.map(possibleType => {
possibleType = { ...possibleType }
const type = typeMap.get(possibleType.name)
if (!type) {
return false
}
const typeSettings = getTypeSettingsByType(type)
if (typeSettings.exclude) {
return false
}
if (
typeIsExcluded({
pluginOptions,
typeName: findNamedTypeName(type),
})
) {
return false
}
possibleType.type = { ...type }
// save this type so we can use it in schema customization
getStore().dispatch.remoteSchema.addFetchedType(type)
const isAGatsbyNode = gatsbyNodesInfo.typeNames.includes(
possibleType.name
)
if (isAGatsbyNode && !buildGatsbyNodeFields) {
// we use the id to link to the top level Gatsby node
possibleType.fields = [`id`]
return possibleType
}
const typeInfo = typeMap.get(possibleType.name)
let filteredFields = [...typeInfo.fields]
if (parentType?.kind === `INTERFACE`) {
// remove any fields from our fragment if the parent type already has them as shared fields
filteredFields = filteredFields.filter(
filteredField =>
!parentType.fields.find(
parentField => parentField.name === filteredField.name
)
)
}
if (typeInfo) {
const fields = recursivelyTransformFields({
fields: filteredFields,
parentType: type,
depth,
ancestorTypeNames,
fragments,
buildingFragment,
circularQueryLimit,
mainType,
parentField,
})
if (!fields || !fields.length) {
return false
}
possibleType.fields = [...fields]
return possibleType
}
return false
})
.filter(Boolean)
return possibleTypes && depth <= maxDepth ? transformedInlineFragments : null
}
// since we're counting circular types that may be on fields many levels up, incarnation felt like it works here ;) the types are born again in later generations
const countIncarnations = ({ typeName, ancestorTypeNames }) =>
ancestorTypeNames.length
? ancestorTypeNames.filter(
ancestorTypeName => ancestorTypeName === typeName
)?.length
: 0
export function transformField({
field,
gatsbyNodesInfo,
typeMap,
maxDepth,
depth,
fieldBlacklist,
fieldAliases,
ancestorTypeNames: parentAncestorTypeNames,
circularQueryLimit,
fragments,
buildingFragment,
mainType,
} = {}) {
const ancestorTypeNames = [...parentAncestorTypeNames]
// we're potentially infinitely recursing when fields are connected to other types that have fields that are connections to other types
// so we need a maximum limit for that
if (depth > maxDepth) {
return false
}
depth++
// if the field has no type we can't use it.
if (!field || !field.type) {
return false
}
const typeSettings = getTypeSettingsByType(field.type)
if (typeSettings.exclude) {
return false
}
// count the number of times this type has appeared as an ancestor of itself
// somewhere up the tree
const typeName = findNamedTypeName(field.type)
const typeKind = findTypeKind(field.type)
const typeIncarnationCount = countIncarnations({
typeName,
ancestorTypeNames,
})
if (typeIncarnationCount > 0) {
// this type is nested within itself atleast once
// create a fragment here that can be reused
createFragment({
fields: typeMap.get(typeName).fields,
type: field.type,
fragments,
field,
ancestorTypeNames: parentAncestorTypeNames,
depth,
fieldBlacklist,
fieldAliases,
typeMap,
gatsbyNodesInfo,
circularQueryLimit,
queryDepth: maxDepth,
buildingFragment,
mainType,
})
}
if (typeIncarnationCount >= circularQueryLimit) {
return false
}
// this is used to alias fields that conflict with Gatsby node fields
// for ex Gatsby and WPGQL both have a `parent` field
const fieldName = returnAliasedFieldName({ fieldAliases, field })
if (
fieldBlacklist.includes(field.name) ||
fieldBlacklist.includes(fieldName)
) {
return false
}
// remove fields that have required args. They'll cause query errors if omitted
// and we can't determine how to use those args programatically.
if (
field.args &&
field.args.length &&
field.args.find(arg => arg?.type?.kind === `NON_NULL`)
) {
return false
}
const fieldType = typeMap.get(findNamedTypeName(field.type)) || {}
const ofType = typeMap.get(findNamedTypeName(fieldType.ofType)) || {}
if (
fieldType.kind === `SCALAR` ||
fieldType.kind === `ENUM` ||
(fieldType.kind === `NON_NULL` && ofType.kind === `SCALAR`) ||
(fieldType.kind === `LIST` && fieldType.ofType.kind === `SCALAR`) ||
// a list of enums has no type name, so findNamedTypeName above finds the enum type
// instead of the field type. Need to explicitly check here
// instead of using helpers
(field.type.kind === `LIST` && field.type?.ofType?.kind === `ENUM`)
) {
return {
fieldName,
fieldType,
}
}
const isListOfGatsbyNodes =
ofType && gatsbyNodesInfo.typeNames.includes(typeName)
const isListOfMediaItems = ofType && typeName === `MediaItem`
const hasIdField = fieldType?.fields?.find(({ name }) => name === `id`)
if (
fieldType.kind === `LIST` &&
isListOfGatsbyNodes &&
!isListOfMediaItems &&
hasIdField
) {
return {
fieldName: fieldName,
fields: [`__typename`, `id`],
fieldType,
}
} else if (fieldType.kind === `LIST` && isListOfMediaItems && hasIdField) {
return {
fieldName: fieldName,
fields: [`__typename`, `id`],
fieldType,
}
} else if (fieldType.kind === `LIST`) {
const listOfType = typeMap.get(findNamedTypeName(fieldType))
const transformedFields = recursivelyTransformFields({
fields: listOfType.fields,
parentType: listOfType || fieldType,
depth,
ancestorTypeNames,
fragments,
circularQueryLimit,
buildingFragment,
mainType,
})
const transformedInlineFragments = transformInlineFragments({
possibleTypes: listOfType.possibleTypes,
parentType: listOfType || fieldType,
parentField: field,
mainType,
gatsbyNodesInfo,
typeMap,
depth,
maxDepth,
ancestorTypeNames,
fragments,
circularQueryLimit,
buildingFragment,
})
if (!transformedFields?.length && !transformedInlineFragments?.length) {
return false
}
// if we have either inlineFragments or fields
return {
fieldName: fieldName,
fields: transformedFields,
inlineFragments: transformedInlineFragments,
fieldType,
}
}
const isAGatsbyNode =
// if this is a gatsby node type
gatsbyNodesInfo.typeNames.includes(typeName) ||
// or all possible types on this type are Gatsby node types
typeMap
.get(typeName)
?.possibleTypes?.every(possibleType =>
gatsbyNodesInfo.typeNames.includes(possibleType.name)
)
if (isAGatsbyNode && hasIdField) {
return {
fieldName: fieldName,
fields: [`__typename`, `id`],
fieldType,
}
}
const typeInfo = typeMap.get(findNamedTypeName(fieldType))
const { fields } = typeInfo || {}
let transformedInlineFragments
if (typeInfo.possibleTypes) {
transformedInlineFragments = transformInlineFragments({
possibleTypes: typeInfo.possibleTypes,
parentType: typeInfo,
parentField: field,
mainType,
gatsbyNodesInfo,
typeMap,
depth,
maxDepth,
ancestorTypeNames,
fragments,
circularQueryLimit,
buildingFragment,
})
}
if (fields || transformedInlineFragments) {
const transformedFields = recursivelyTransformFields({
parentType: typeInfo,
parentFieldName: field.name,
mainType,
fields,
depth,
ancestorTypeNames,
parentField: field,
fragments,
circularQueryLimit,
buildingFragment,
})
if (!transformedFields?.length && !transformedInlineFragments?.length) {
return false
}
return {
fieldName: fieldName,
fields: transformedFields,
inlineFragments: transformedInlineFragments,
fieldType,
}
}
if (fieldType.kind === `UNION`) {
const typeInfo = typeMap.get(fieldType.name)
const transformedFields = recursivelyTransformFields({
fields: typeInfo.fields,
parentType: fieldType,
mainType,
depth,
ancestorTypeNames,
fragments,
circularQueryLimit,
buildingFragment,
})
const inlineFragments = transformInlineFragments({
possibleTypes: typeInfo.possibleTypes,
gatsbyNodesInfo,
typeMap,
mainType,
depth,
maxDepth,
ancestorTypeNames,
parentField: field,
fragments,
circularQueryLimit,
buildingFragment,
})
return {
fieldName: fieldName,
fields: transformedFields,
inlineFragments,
fieldType,
}
}
return false
}
const createFragment = ({
fields,
field,
type,
fragments,
fieldBlacklist,
fieldAliases,
typeMap,
gatsbyNodesInfo,
queryDepth,
ancestorTypeNames,
mainType,
buildingFragment = false,
}) => {
const typeName = findNamedTypeName(type)
if (buildingFragment) {
// this fragment is inside a fragment that's already being built so we should exit
return null
}
const previouslyCreatedFragment = fragments?.[typeName]
if (previouslyCreatedFragment && buildingFragment === typeName) {
return previouslyCreatedFragment
}
const fragmentFields = fields.reduce((fragmentFields, field) => {
const fieldTypeName = findNamedTypeName(field.type)
const fieldType = typeMap.get(fieldTypeName)
if (
// if this field is a different type than the fragment but has a field of the same type as the fragment,
// we need to skip this field in the fragment to prevent nesting this type in itself a level down
fieldType.name !== typeName &&
fieldType?.fields?.find(
innerFieldField => findNamedTypeName(innerFieldField.type) === typeName
)
) {
return fragmentFields
}
const transformedField = transformField({
field,
gatsbyNodesInfo,
typeMap,
maxDepth: queryDepth,
depth: 0,
fieldBlacklist,
fieldAliases,
ancestorTypeNames,
mainType,
circularQueryLimit: 1,
fragments,
buildingFragment: typeName,
})
if (findNamedTypeName(field.type) !== typeName && !!transformedField) {
fragmentFields.push(transformedField)
}
return fragmentFields
}, [])
const queryType = typeMap.get(typeName)
const transformedInlineFragments = queryType?.possibleTypes?.length
? transformInlineFragments({
possibleTypes: queryType.possibleTypes,
parentType: queryType,
parentField: field,
mainType,
gatsbyNodesInfo,
typeMap,
depth: 0,
maxDepth: queryDepth,
circularQueryLimit: 1,
ancestorTypeNames,
fragments,
buildingFragment: typeName,
})
: null
if (fragments) {
fragments[typeName] = {
name: `${typeName}Fragment`,
type: typeName,
fields: fragmentFields,
inlineFragments: transformedInlineFragments,
}
}
return fragmentFields
}
const transformFields = ({
fields,
parentType,
mainType,
fragments,
parentField,
ancestorTypeNames,
depth,
fieldBlacklist,
fieldAliases,
typeMap,
gatsbyNodesInfo,
queryDepth,
circularQueryLimit,
pluginOptions,
buildingFragment,
}) =>
fields
?.filter(
field =>
!fieldIsExcludedOnParentType({
field,
parentType,
}) &&
!fieldIsExcludedOnAll({
pluginOptions,
field,
}) &&
!typeIsExcluded({
pluginOptions,
typeName: findNamedTypeName(field.type),
})
)
.map(field => {
const transformedField = transformField({
maxDepth: queryDepth,
gatsbyNodesInfo,
fieldBlacklist,
fieldAliases,
typeMap,
field,
depth,
ancestorTypeNames,
circularQueryLimit,
fragments,
buildingFragment,
mainType,
parentField,
})
if (transformedField) {
// save this type so we know to use it in schema customization
getStore().dispatch.remoteSchema.addFetchedType(field.type)
}
const typeName = findNamedTypeName(field.type)
const fragment = fragments?.[typeName]
// @todo add any adjacent fields and inline fragments directly to the stored fragment object so this logic can be changed to if (fragment) useTheFragment()
// once that's done it can be added above and below transformField() above ☝️
// and potentially short circuit expensive work that will be thrown away anyway
if (fragment && transformedField && buildingFragment !== typeName) {
// if (fragment && buildingFragment !== typeName && transformedField) {
// remove fields from this query that already exist in the fragment
if (transformedField?.fields?.length) {
transformedField.fields = transformedField.fields.filter(
field =>
!fragment.fields.find(
fragmentField => fragmentField.fieldName === field.fieldName
)
)
}
// if this field has no fields (because it has inline fragments only)
// we need to create an empty array since we treat reusable fragments as
// a field
if (!transformedField.fields) {
transformedField.fields = []
}
transformedField.fields.push({
internalType: `Fragment`,
fragment,
})
if (transformedField?.inlineFragments?.length) {
transformedField.inlineFragments =
transformedField.inlineFragments.filter(
fieldInlineFragment =>
// yes this is a horrible use of .find(). @todo refactor this for better perf
!fragment.inlineFragments.find(
fragmentInlineFragment =>
fragmentInlineFragment.name === fieldInlineFragment.name
)
)
}
}
if (field.fields && !transformedField) {
return null
}
const fieldTypeKind = findTypeKind(field.type)
const fieldOfTypeKind = findTypeKind(field.type.ofType)
const typeKindsRequiringSelectionSets = [`OBJECT`, `UNION`, `INTERFACE`]
const fieldNeedsSelectionSet =
typeKindsRequiringSelectionSets.includes(fieldTypeKind) ||
typeKindsRequiringSelectionSets.includes(fieldOfTypeKind)
if (
// if our field needs a selectionset
fieldNeedsSelectionSet &&
// but we have no fields
!transformedField.fields &&
// and no inline fragments
!transformedField.inlineFragments
) {
// we need to discard this field to prevent GraphQL errors
// we're likely at the very bottom of the query depth
// so that this fields children were omitted
return null
}
return transformedField
})
.filter(Boolean)
const recursivelyTransformFields = ({
fields,
parentType,
mainType,
fragments,
parentField,
ancestorTypeNames: parentAncestorTypeNames,
depth = 0,
buildingFragment = false,
}) => {
if (!fields || !fields.length) {
return null
}
if (!parentAncestorTypeNames) {
parentAncestorTypeNames = []
}
const ancestorTypeNames = [...parentAncestorTypeNames]
const {
gatsbyApi: { pluginOptions },
remoteSchema: { fieldBlacklist, fieldAliases, typeMap, gatsbyNodesInfo },
} = getStore().getState()
const {
schema: { queryDepth, circularQueryLimit },
} = pluginOptions
if (depth > queryDepth && ancestorTypeNames.length) {
return null
}
const typeName = findNamedTypeName(parentType)
const grandParentTypeName = ancestorTypeNames.length
? ancestorTypeNames[ancestorTypeNames.length - 1]
: null
if (grandParentTypeName && typeName !== grandParentTypeName) {
// if a field has fields of the same type as the field above it
// we shouldn't fetch them. 2 types that are circular between each other
// are dangerous as they will generate very large queries and fetch data we don't need
// these types should instead be proper connections so we can identify
// that only an id needs to be fetched.
// @todo maybe move this into transformFields() instead of here
fields = fields.filter(field => {
const fieldTypeName = findNamedTypeName(field.type)
return fieldTypeName !== grandParentTypeName
})
}
const typeIncarnationCount = countIncarnations({
typeName,
ancestorTypeNames,
})
if (typeIncarnationCount >= circularQueryLimit) {
return null
}
parentAncestorTypeNames.push(typeName)
const recursivelyTransformedFields = transformFields({
fields,
parentType,
mainType,
fragments,
parentField,
ancestorTypeNames: parentAncestorTypeNames,
depth,
fieldBlacklist,
fieldAliases,
typeMap,
gatsbyNodesInfo,
queryDepth,
circularQueryLimit,
pluginOptions,
buildingFragment,
})
if (!recursivelyTransformedFields.length) {
return null
}
return recursivelyTransformedFields
}
export default recursivelyTransformFields