@constructive-io/graphql-codegen 2.17.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/esm/gql.js ADDED
@@ -0,0 +1,721 @@
1
+ // TODO: use inflection for all the things
2
+ // const { singularize } = require('inflection');
3
+ import * as t from 'gql-ast';
4
+ // @ts-ignore
5
+ import inflection from 'inflection';
6
+ import plz from 'pluralize';
7
+ const NON_MUTABLE_PROPS = [
8
+ 'id',
9
+ 'createdAt',
10
+ 'createdBy',
11
+ 'updatedAt',
12
+ 'updatedBy',
13
+ ];
14
+ const objectToArray = (obj) => Object.keys(obj).map((k) => ({ name: k, ...obj[k] }));
15
+ export const createGqlMutation = ({ operationName, mutationName, selectArgs, variableDefinitions, modelName, selections, useModel = true, }) => {
16
+ const opSel = !modelName
17
+ ? [
18
+ t.field({
19
+ name: operationName,
20
+ args: selectArgs,
21
+ selectionSet: t.selectionSet({ selections }),
22
+ }),
23
+ ]
24
+ : [
25
+ t.field({
26
+ name: operationName,
27
+ args: selectArgs,
28
+ selectionSet: t.selectionSet({
29
+ selections: useModel
30
+ ? [
31
+ t.field({
32
+ name: modelName,
33
+ selectionSet: t.selectionSet({ selections }),
34
+ }),
35
+ ]
36
+ : selections,
37
+ }),
38
+ }),
39
+ ];
40
+ return t.document({
41
+ definitions: [
42
+ t.operationDefinition({
43
+ operation: 'mutation',
44
+ name: mutationName,
45
+ variableDefinitions,
46
+ selectionSet: t.selectionSet({ selections: opSel }),
47
+ }),
48
+ ],
49
+ });
50
+ };
51
+ export const getMany = ({ operationName, query, fields, }) => {
52
+ const queryName = inflection.camelize(['get', inflection.underscore(operationName), 'query', 'all'].join('_'), true);
53
+ const selections = getSelections(query, fields);
54
+ const opSel = [
55
+ t.field({
56
+ name: operationName,
57
+ selectionSet: t.selectionSet({
58
+ selections: [
59
+ t.field({ name: 'totalCount' }),
60
+ t.field({
61
+ name: 'nodes',
62
+ selectionSet: t.selectionSet({ selections }),
63
+ }),
64
+ ],
65
+ }),
66
+ }),
67
+ ];
68
+ const ast = t.document({
69
+ definitions: [
70
+ t.operationDefinition({
71
+ operation: 'query',
72
+ name: queryName,
73
+ selectionSet: t.selectionSet({ selections: opSel }),
74
+ }),
75
+ ],
76
+ });
77
+ return { name: queryName, ast };
78
+ };
79
+ export const getManyPaginatedEdges = ({ operationName, query, fields, }) => {
80
+ const queryName = inflection.camelize(['get', inflection.underscore(operationName), 'paginated'].join('_'), true);
81
+ const Plural = operationName.charAt(0).toUpperCase() + operationName.slice(1);
82
+ const Singular = query.model;
83
+ const Condition = `${Singular}Condition`;
84
+ const Filter = `${Singular}Filter`;
85
+ const OrderBy = `${Plural}OrderBy`;
86
+ const selections = getSelections(query, fields);
87
+ const variableDefinitions = [
88
+ 'first',
89
+ 'last',
90
+ 'offset',
91
+ 'after',
92
+ 'before',
93
+ ].map((name) => t.variableDefinition({
94
+ variable: t.variable({ name }),
95
+ type: t.namedType({ type: name === 'after' || name === 'before' ? 'Cursor' : 'Int' }),
96
+ }));
97
+ variableDefinitions.push(t.variableDefinition({
98
+ variable: t.variable({ name: 'condition' }),
99
+ type: t.namedType({ type: Condition }),
100
+ }), t.variableDefinition({
101
+ variable: t.variable({ name: 'filter' }),
102
+ type: t.namedType({ type: Filter }),
103
+ }), t.variableDefinition({
104
+ variable: t.variable({ name: 'orderBy' }),
105
+ type: t.listType({
106
+ type: t.nonNullType({
107
+ type: t.namedType({ type: OrderBy }),
108
+ }),
109
+ }),
110
+ }));
111
+ const args = [
112
+ 'first',
113
+ 'last',
114
+ 'offset',
115
+ 'after',
116
+ 'before',
117
+ 'condition',
118
+ 'filter',
119
+ 'orderBy',
120
+ ].map((name) => t.argument({
121
+ name,
122
+ value: t.variable({ name }),
123
+ }));
124
+ const ast = t.document({
125
+ definitions: [
126
+ t.operationDefinition({
127
+ operation: 'query',
128
+ name: queryName,
129
+ variableDefinitions,
130
+ selectionSet: t.selectionSet({
131
+ selections: [
132
+ t.field({
133
+ name: operationName,
134
+ args,
135
+ selectionSet: t.selectionSet({
136
+ selections: [
137
+ t.field({ name: 'totalCount' }),
138
+ t.field({
139
+ name: 'pageInfo',
140
+ selectionSet: t.selectionSet({
141
+ selections: [
142
+ t.field({ name: 'hasNextPage' }),
143
+ t.field({ name: 'hasPreviousPage' }),
144
+ t.field({ name: 'endCursor' }),
145
+ t.field({ name: 'startCursor' }),
146
+ ],
147
+ }),
148
+ }),
149
+ t.field({
150
+ name: 'edges',
151
+ selectionSet: t.selectionSet({
152
+ selections: [
153
+ t.field({ name: 'cursor' }),
154
+ t.field({
155
+ name: 'node',
156
+ selectionSet: t.selectionSet({ selections }),
157
+ }),
158
+ ],
159
+ }),
160
+ }),
161
+ ],
162
+ }),
163
+ }),
164
+ ],
165
+ }),
166
+ }),
167
+ ],
168
+ });
169
+ return { name: queryName, ast };
170
+ };
171
+ export const getManyPaginatedNodes = ({ operationName, query, fields, }) => {
172
+ const queryName = inflection.camelize(['get', inflection.underscore(operationName), 'query'].join('_'), true);
173
+ const Singular = query.model;
174
+ const Plural = operationName.charAt(0).toUpperCase() + operationName.slice(1);
175
+ const Condition = `${Singular}Condition`;
176
+ const Filter = `${Singular}Filter`;
177
+ const OrderBy = `${Plural}OrderBy`;
178
+ const selections = getSelections(query, fields);
179
+ const variableDefinitions = [
180
+ 'first',
181
+ 'last',
182
+ 'after',
183
+ 'before',
184
+ 'offset',
185
+ ].map((name) => t.variableDefinition({
186
+ variable: t.variable({ name }),
187
+ type: t.namedType({ type: name === 'after' || name === 'before' ? 'Cursor' : 'Int' }),
188
+ }));
189
+ variableDefinitions.push(t.variableDefinition({
190
+ variable: t.variable({ name: 'condition' }),
191
+ type: t.namedType({ type: Condition }),
192
+ }), t.variableDefinition({
193
+ variable: t.variable({ name: 'filter' }),
194
+ type: t.namedType({ type: Filter }),
195
+ }), t.variableDefinition({
196
+ variable: t.variable({ name: 'orderBy' }),
197
+ type: t.listType({
198
+ type: t.nonNullType({
199
+ type: t.namedType({ type: OrderBy }),
200
+ }),
201
+ }),
202
+ }));
203
+ const args = [
204
+ 'first',
205
+ 'last',
206
+ 'offset',
207
+ 'after',
208
+ 'before',
209
+ 'condition',
210
+ 'filter',
211
+ 'orderBy',
212
+ ].map((name) => t.argument({
213
+ name,
214
+ value: t.variable({ name }),
215
+ }));
216
+ const ast = t.document({
217
+ definitions: [
218
+ t.operationDefinition({
219
+ operation: 'query',
220
+ name: queryName,
221
+ variableDefinitions,
222
+ selectionSet: t.selectionSet({
223
+ selections: [
224
+ t.field({
225
+ name: operationName,
226
+ args,
227
+ selectionSet: t.selectionSet({
228
+ selections: [
229
+ t.field({ name: 'totalCount' }),
230
+ t.field({
231
+ name: 'pageInfo',
232
+ selectionSet: t.selectionSet({
233
+ selections: [
234
+ t.field({ name: 'hasNextPage' }),
235
+ t.field({ name: 'hasPreviousPage' }),
236
+ t.field({ name: 'endCursor' }),
237
+ t.field({ name: 'startCursor' }),
238
+ ],
239
+ }),
240
+ }),
241
+ t.field({
242
+ name: 'nodes',
243
+ selectionSet: t.selectionSet({ selections }),
244
+ }),
245
+ ],
246
+ }),
247
+ }),
248
+ ],
249
+ }),
250
+ }),
251
+ ],
252
+ });
253
+ return { name: queryName, ast };
254
+ };
255
+ export const getOrderByEnums = ({ operationName, query, }) => {
256
+ const queryName = inflection.camelize(['get', inflection.underscore(operationName), 'Order', 'By', 'Enums'].join('_'), true);
257
+ const Model = operationName.charAt(0).toUpperCase() + operationName.slice(1);
258
+ const OrderBy = `${Model}OrderBy`;
259
+ const ast = t.document({
260
+ definitions: [
261
+ t.operationDefinition({
262
+ operation: 'query',
263
+ name: queryName,
264
+ selectionSet: t.selectionSet({
265
+ selections: [
266
+ t.field({
267
+ name: '__type',
268
+ args: [
269
+ t.argument({
270
+ name: 'name',
271
+ value: t.stringValue({ value: OrderBy }),
272
+ }),
273
+ ],
274
+ selectionSet: t.selectionSet({
275
+ selections: [
276
+ t.field({
277
+ name: 'enumValues',
278
+ selectionSet: t.selectionSet({
279
+ selections: [t.field({ name: 'name' })],
280
+ }),
281
+ }),
282
+ ],
283
+ }),
284
+ }),
285
+ ],
286
+ }),
287
+ }),
288
+ ],
289
+ });
290
+ return { name: queryName, ast };
291
+ };
292
+ export const getFragment = ({ operationName, query, }) => {
293
+ const queryName = inflection.camelize([inflection.underscore(query.model), 'Fragment'].join('_'), true);
294
+ const selections = getSelections(query);
295
+ const ast = t.document({
296
+ definitions: [
297
+ t.fragmentDefinition({
298
+ name: queryName,
299
+ typeCondition: t.namedType({
300
+ type: query.model,
301
+ }),
302
+ selectionSet: t.selectionSet({
303
+ selections,
304
+ }),
305
+ }),
306
+ ],
307
+ });
308
+ return { name: queryName, ast };
309
+ };
310
+ export const getOne = ({ operationName, query, fields, }) => {
311
+ const queryName = inflection.camelize(['get', inflection.underscore(operationName), 'query'].join('_'), true);
312
+ const variableDefinitions = objectToArray(query.properties)
313
+ .filter((field) => field.isNotNull)
314
+ .map(({ name, type, isNotNull, isArray, isArrayNotNull }) => {
315
+ let gqlType = t.namedType({ type });
316
+ if (isNotNull) {
317
+ gqlType = t.nonNullType({ type: gqlType });
318
+ }
319
+ if (isArray) {
320
+ gqlType = t.listType({ type: gqlType });
321
+ if (isArrayNotNull) {
322
+ gqlType = t.nonNullType({ type: gqlType });
323
+ }
324
+ }
325
+ return t.variableDefinition({
326
+ variable: t.variable({ name }),
327
+ type: gqlType,
328
+ });
329
+ });
330
+ const selectArgs = objectToArray(query.properties)
331
+ .filter((field) => field.isNotNull)
332
+ .map((field) => t.argument({
333
+ name: field.name,
334
+ value: t.variable({ name: field.name }),
335
+ }));
336
+ const selections = getSelections(query, fields);
337
+ const opSel = [
338
+ t.field({
339
+ name: operationName,
340
+ args: selectArgs,
341
+ selectionSet: t.selectionSet({ selections }),
342
+ }),
343
+ ];
344
+ const ast = t.document({
345
+ definitions: [
346
+ t.operationDefinition({
347
+ operation: 'query',
348
+ name: queryName,
349
+ variableDefinitions,
350
+ selectionSet: t.selectionSet({ selections: opSel }),
351
+ }),
352
+ ],
353
+ });
354
+ return { name: queryName, ast };
355
+ };
356
+ export const createOne = ({ operationName, mutation, }) => {
357
+ const mutationName = inflection.camelize([inflection.underscore(operationName), 'mutation'].join('_'), true);
358
+ if (!mutation.properties?.input?.properties) {
359
+ console.log('no input field for mutation for ' + mutationName);
360
+ return;
361
+ }
362
+ const modelName = inflection.camelize([plz.singular(mutation.model)].join('_'), true);
363
+ const allAttrs = objectToArray(mutation.properties.input.properties[modelName].properties);
364
+ const attrs = allAttrs.filter((field) => !NON_MUTABLE_PROPS.includes(field.name));
365
+ const variableDefinitions = attrs.map(({ name, type, isNotNull, isArray, isArrayNotNull }) => {
366
+ let gqlType = t.namedType({ type });
367
+ if (isNotNull) {
368
+ gqlType = t.nonNullType({ type: gqlType });
369
+ }
370
+ if (isArray) {
371
+ gqlType = t.listType({ type: gqlType });
372
+ if (isArrayNotNull) {
373
+ gqlType = t.nonNullType({ type: gqlType });
374
+ }
375
+ }
376
+ return t.variableDefinition({
377
+ variable: t.variable({ name }),
378
+ type: gqlType,
379
+ });
380
+ });
381
+ const selectArgs = [
382
+ t.argument({
383
+ name: 'input',
384
+ value: t.objectValue({
385
+ fields: [
386
+ t.objectField({
387
+ name: modelName,
388
+ value: t.objectValue({
389
+ fields: attrs.map((field) => t.objectField({
390
+ name: field.name,
391
+ value: t.variable({ name: field.name }),
392
+ })),
393
+ }),
394
+ }),
395
+ ],
396
+ }),
397
+ }),
398
+ ];
399
+ const selections = allAttrs.map((field) => t.field({ name: 'id' }));
400
+ const ast = createGqlMutation({
401
+ operationName,
402
+ mutationName,
403
+ selectArgs,
404
+ selections: [t.field({ name: 'clientMutationId' })],
405
+ variableDefinitions,
406
+ modelName,
407
+ useModel: false,
408
+ });
409
+ return { name: mutationName, ast };
410
+ };
411
+ export const patchOne = ({ operationName, mutation, }) => {
412
+ const mutationName = inflection.camelize([inflection.underscore(operationName), 'mutation'].join('_'), true);
413
+ if (!mutation.properties?.input?.properties) {
414
+ console.log('no input field for mutation for ' + mutationName);
415
+ return;
416
+ }
417
+ const modelName = inflection.camelize([plz.singular(mutation.model)].join('_'), true);
418
+ // @ts-ignore
419
+ const allAttrs = objectToArray(mutation.properties.input.properties['patch']?.properties || {});
420
+ const patchAttrs = allAttrs.filter(
421
+ // @ts-ignore
422
+ (prop) => !NON_MUTABLE_PROPS.includes(prop.name));
423
+ const patchByAttrs = objectToArray(mutation.properties.input.properties).filter((n) => n.name !== 'patch');
424
+ const patchers = patchByAttrs.map((p) => p.name);
425
+ const patchAttrVarDefs = patchAttrs
426
+ // @ts-ignore
427
+ .filter((field) => !patchers.includes(field.name))
428
+ .map(
429
+ // @ts-ignore
430
+ ({ name, type, isArray }) => {
431
+ let gqlType = t.namedType({ type });
432
+ if (isArray) {
433
+ gqlType = t.listType({ type: gqlType });
434
+ }
435
+ // @ts-ignore
436
+ if (patchers.includes(name)) {
437
+ gqlType = t.nonNullType({ type: gqlType });
438
+ }
439
+ return t.variableDefinition({
440
+ // @ts-ignore
441
+ variable: t.variable({ name }),
442
+ type: gqlType,
443
+ });
444
+ });
445
+ const patchByVarDefs = patchByAttrs.map(({ name, type, isNotNull, isArray, isArrayNotNull }) => {
446
+ let gqlType = t.namedType({ type });
447
+ if (isNotNull) {
448
+ gqlType = t.nonNullType({ type: gqlType });
449
+ }
450
+ if (isArray) {
451
+ gqlType = t.listType({ type: gqlType });
452
+ if (isArrayNotNull) {
453
+ gqlType = t.nonNullType({ type: gqlType });
454
+ }
455
+ }
456
+ return t.variableDefinition({ variable: t.variable({ name }), type: gqlType });
457
+ });
458
+ const selectArgs = [
459
+ t.argument({
460
+ name: 'input',
461
+ value: t.objectValue({
462
+ fields: [
463
+ ...patchByAttrs.map((field) => t.objectField({
464
+ name: field.name,
465
+ value: t.variable({ name: field.name }),
466
+ })),
467
+ t.objectField({
468
+ name: 'patch',
469
+ value: t.objectValue({
470
+ fields: patchAttrs
471
+ // @ts-ignore
472
+ .filter((field) => !patchers.includes(field.name))
473
+ .map((field) => t.objectField({
474
+ // @ts-ignore
475
+ name: field.name,
476
+ // @ts-ignore
477
+ value: t.variable({ name: field.name }),
478
+ })),
479
+ }),
480
+ }),
481
+ ],
482
+ }),
483
+ }),
484
+ ];
485
+ const selections = [t.field({ name: 'clientMutationId' })];
486
+ const ast = createGqlMutation({
487
+ operationName,
488
+ mutationName,
489
+ selectArgs,
490
+ selections,
491
+ variableDefinitions: [...patchByVarDefs, ...patchAttrVarDefs],
492
+ modelName,
493
+ useModel: false,
494
+ });
495
+ return { name: mutationName, ast };
496
+ };
497
+ export const deleteOne = ({ operationName, mutation, }) => {
498
+ const mutationName = inflection.camelize([inflection.underscore(operationName), 'mutation'].join('_'), true);
499
+ if (!mutation.properties?.input?.properties) {
500
+ console.log('no input field for mutation for ' + mutationName);
501
+ return;
502
+ }
503
+ const modelName = inflection.camelize([plz.singular(mutation.model)].join('_'), true);
504
+ // @ts-ignore
505
+ const deleteAttrs = objectToArray(mutation.properties.input.properties);
506
+ const variableDefinitions = deleteAttrs.map(({ name, type, isNotNull, isArray }) => {
507
+ let gqlType = t.namedType({ type });
508
+ if (isNotNull) {
509
+ gqlType = t.nonNullType({ type: gqlType });
510
+ }
511
+ if (isArray) {
512
+ gqlType = t.listType({ type: gqlType });
513
+ // Always non-null list for deletion fields
514
+ gqlType = t.nonNullType({ type: gqlType });
515
+ }
516
+ return t.variableDefinition({
517
+ variable: t.variable({ name }),
518
+ type: gqlType,
519
+ });
520
+ });
521
+ const selectArgs = [
522
+ t.argument({
523
+ name: 'input',
524
+ value: t.objectValue({
525
+ fields: deleteAttrs.map((f) => t.objectField({
526
+ name: f.name,
527
+ value: t.variable({ name: f.name }),
528
+ })),
529
+ }),
530
+ }),
531
+ ];
532
+ const selections = [t.field({ name: 'clientMutationId' })];
533
+ const ast = createGqlMutation({
534
+ operationName,
535
+ mutationName,
536
+ selectArgs,
537
+ selections,
538
+ variableDefinitions,
539
+ modelName,
540
+ useModel: false,
541
+ });
542
+ return { name: mutationName, ast };
543
+ };
544
+ export const createMutation = ({ operationName, mutation, }) => {
545
+ const mutationName = inflection.camelize([inflection.underscore(operationName), 'mutation'].join('_'), true);
546
+ if (!mutation.properties?.input?.properties) {
547
+ console.log('no input field for mutation for ' + mutationName);
548
+ return;
549
+ }
550
+ // @ts-ignore
551
+ const otherAttrs = objectToArray(mutation.properties.input.properties);
552
+ const variableDefinitions = otherAttrs.map(({ name, type, isArray, isArrayNotNull }) => {
553
+ let gqlType = t.namedType({ type });
554
+ // Force as non-nullable for mutation reliability (as per your comment)
555
+ gqlType = t.nonNullType({ type: gqlType });
556
+ if (isArray) {
557
+ gqlType = t.listType({ type: gqlType });
558
+ if (isArrayNotNull) {
559
+ gqlType = t.nonNullType({ type: gqlType });
560
+ }
561
+ }
562
+ return t.variableDefinition({
563
+ variable: t.variable({ name }),
564
+ type: gqlType,
565
+ });
566
+ });
567
+ const selectArgs = otherAttrs.length > 0
568
+ ? [
569
+ t.argument({
570
+ name: 'input',
571
+ value: t.objectValue({
572
+ fields: otherAttrs.map((f) => t.objectField({
573
+ name: f.name,
574
+ value: t.variable({ name: f.name }),
575
+ })),
576
+ }),
577
+ }),
578
+ ]
579
+ : [];
580
+ const outputFields = mutation.outputs
581
+ ?.filter((field) => field.type.kind === 'SCALAR')
582
+ .map((f) => f.name) || [];
583
+ if (outputFields.length === 0) {
584
+ outputFields.push('clientMutationId');
585
+ }
586
+ const selections = outputFields.map((o) => t.field({ name: o }));
587
+ const ast = createGqlMutation({
588
+ operationName,
589
+ mutationName,
590
+ selectArgs,
591
+ selections,
592
+ variableDefinitions,
593
+ });
594
+ return { name: mutationName, ast };
595
+ };
596
+ export const generate = (gql) => {
597
+ return Object.keys(gql).reduce((m, operationName) => {
598
+ const defn = gql[operationName];
599
+ let name;
600
+ let ast;
601
+ if (defn.qtype === 'mutation') {
602
+ if (defn.mutationType === 'create') {
603
+ ({ name, ast } = createOne({ operationName, mutation: defn }) ?? {});
604
+ }
605
+ else if (defn.mutationType === 'patch') {
606
+ ({ name, ast } = patchOne({ operationName, mutation: defn }) ?? {});
607
+ }
608
+ else if (defn.mutationType === 'delete') {
609
+ ({ name, ast } = deleteOne({ operationName, mutation: defn }) ?? {});
610
+ }
611
+ else {
612
+ ({ name, ast } = createMutation({ operationName, mutation: defn }) ?? {});
613
+ }
614
+ }
615
+ else if (defn.qtype === 'getMany') {
616
+ // getMany + related
617
+ [
618
+ getMany,
619
+ getManyPaginatedEdges,
620
+ getManyPaginatedNodes,
621
+ getOrderByEnums,
622
+ getFragment
623
+ ].forEach(fn => {
624
+ // @ts-ignore
625
+ const result = fn({ operationName, query: defn });
626
+ if (result?.name && result?.ast) {
627
+ m[result.name] = result;
628
+ }
629
+ });
630
+ }
631
+ else if (defn.qtype === 'getOne') {
632
+ // @ts-ignore
633
+ ({ name, ast } = getOne({ operationName, query: defn }) ?? {});
634
+ }
635
+ else {
636
+ console.warn('Unknown qtype for key: ' + operationName);
637
+ }
638
+ if (name && ast) {
639
+ m[name] = { name, ast };
640
+ }
641
+ return m;
642
+ }, {});
643
+ };
644
+ export const generateGranular = (gql, model, fields) => {
645
+ return Object.keys(gql).reduce((m, operationName) => {
646
+ const defn = gql[operationName];
647
+ const matchModel = defn.model;
648
+ let name;
649
+ let ast;
650
+ if (defn.qtype === 'getMany') {
651
+ const many = getMany({ operationName, query: defn, fields });
652
+ if (many?.name && many?.ast && model === matchModel) {
653
+ m[many.name] = many;
654
+ }
655
+ const paginatedEdges = getManyPaginatedEdges({
656
+ operationName,
657
+ query: defn,
658
+ fields,
659
+ });
660
+ if (paginatedEdges?.name && paginatedEdges?.ast && model === matchModel) {
661
+ m[paginatedEdges.name] = paginatedEdges;
662
+ }
663
+ const paginatedNodes = getManyPaginatedNodes({
664
+ operationName,
665
+ query: defn,
666
+ fields,
667
+ });
668
+ if (paginatedNodes?.name && paginatedNodes?.ast && model === matchModel) {
669
+ m[paginatedNodes.name] = paginatedNodes;
670
+ }
671
+ }
672
+ else if (defn.qtype === 'getOne') {
673
+ const one = getOne({ operationName, query: defn, fields });
674
+ if (one?.name && one?.ast && model === matchModel) {
675
+ m[one.name] = one;
676
+ }
677
+ }
678
+ return m;
679
+ }, {});
680
+ };
681
+ export function getSelections(query, fields = []) {
682
+ const useAll = fields.length === 0;
683
+ const mapItem = (item) => {
684
+ if (typeof item === 'string') {
685
+ if (!useAll && !fields.includes(item))
686
+ return null;
687
+ return t.field({ name: item });
688
+ }
689
+ if (typeof item === 'object' &&
690
+ item !== null &&
691
+ 'name' in item &&
692
+ 'selection' in item &&
693
+ Array.isArray(item.selection)) {
694
+ if (!useAll && !fields.includes(item.name))
695
+ return null;
696
+ const isMany = item.qtype === 'getMany';
697
+ if (isMany) {
698
+ return t.field({
699
+ name: item.name,
700
+ args: [
701
+ t.argument({ name: 'first', value: t.intValue({ value: '3' }) }),
702
+ ],
703
+ selectionSet: t.selectionSet({
704
+ selections: [
705
+ t.field({
706
+ name: 'nodes',
707
+ selectionSet: t.selectionSet({ selections: item.selection.map((s) => mapItem(s)).filter(Boolean) }),
708
+ }),
709
+ ],
710
+ }),
711
+ });
712
+ }
713
+ return t.field({
714
+ name: item.name,
715
+ selectionSet: t.selectionSet({ selections: item.selection.map((s) => mapItem(s)).filter(Boolean) }),
716
+ });
717
+ }
718
+ return null;
719
+ };
720
+ return query.selection.map((field) => mapItem(field)).filter((i) => Boolean(i));
721
+ }