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