@constructive-io/graphql-codegen 2.17.29 → 2.17.31
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/README.md +97 -0
- package/codegen.js +35 -9
- package/esm/codegen.js +35 -9
- package/esm/gql.js +333 -159
- package/esm/options.js +8 -2
- package/gql.d.ts +37 -6
- package/gql.js +333 -159
- package/options.d.ts +10 -0
- package/options.js +8 -2
- package/package.json +4 -4
package/gql.js
CHANGED
|
@@ -52,6 +52,52 @@ const NON_MUTABLE_PROPS = [
|
|
|
52
52
|
'updatedBy',
|
|
53
53
|
];
|
|
54
54
|
const objectToArray = (obj) => Object.keys(obj).map((k) => ({ name: k, ...obj[k] }));
|
|
55
|
+
function refToTypeNode(ref, overrides) {
|
|
56
|
+
if (!ref)
|
|
57
|
+
return null;
|
|
58
|
+
if (ref.kind === 'NON_NULL') {
|
|
59
|
+
const inner = refToTypeNode(ref.ofType, overrides);
|
|
60
|
+
return t.nonNullType({ type: inner });
|
|
61
|
+
}
|
|
62
|
+
if (ref.kind === 'LIST') {
|
|
63
|
+
const inner = refToTypeNode(ref.ofType, overrides);
|
|
64
|
+
return t.listType({ type: inner });
|
|
65
|
+
}
|
|
66
|
+
const name = (overrides && overrides[ref.name]) || ref.name;
|
|
67
|
+
return t.namedType({ type: name });
|
|
68
|
+
}
|
|
69
|
+
function resolveTypeName(name, type, overrides) {
|
|
70
|
+
if (typeof type === 'string') {
|
|
71
|
+
const base = type;
|
|
72
|
+
const mapped = overrides && overrides[base];
|
|
73
|
+
return mapped || base;
|
|
74
|
+
}
|
|
75
|
+
if (type && typeof type === 'object') {
|
|
76
|
+
if (typeof type.name === 'string' && type.name.length > 0)
|
|
77
|
+
return type.name;
|
|
78
|
+
let t = type;
|
|
79
|
+
while (t && typeof t === 'object' && t.ofType)
|
|
80
|
+
t = t.ofType;
|
|
81
|
+
if (t && typeof t.name === 'string' && t.name.length > 0) {
|
|
82
|
+
const base = t.name;
|
|
83
|
+
const mapped = overrides && overrides[base];
|
|
84
|
+
return mapped || base;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return 'JSON';
|
|
88
|
+
}
|
|
89
|
+
function refToNamedTypeName(ref) {
|
|
90
|
+
let r = ref;
|
|
91
|
+
while (r && (r.kind === 'NON_NULL' || r.kind === 'LIST'))
|
|
92
|
+
r = r.ofType;
|
|
93
|
+
return r && r.name ? r.name : null;
|
|
94
|
+
}
|
|
95
|
+
function extractNamedTypeName(node) {
|
|
96
|
+
let n = node;
|
|
97
|
+
while (n && !n.name && n.type)
|
|
98
|
+
n = n.type;
|
|
99
|
+
return n && n.name ? n.name : null;
|
|
100
|
+
}
|
|
55
101
|
const createGqlMutation = ({ operationName, mutationName, selectArgs, variableDefinitions, modelName, selections, useModel = true, }) => {
|
|
56
102
|
const opSel = !modelName
|
|
57
103
|
? [
|
|
@@ -99,8 +145,27 @@ const getMany = ({ operationName, query, fields, }) => {
|
|
|
99
145
|
selections: [
|
|
100
146
|
t.field({ name: 'totalCount' }),
|
|
101
147
|
t.field({
|
|
102
|
-
name: '
|
|
103
|
-
selectionSet: t.selectionSet({
|
|
148
|
+
name: 'pageInfo',
|
|
149
|
+
selectionSet: t.selectionSet({
|
|
150
|
+
selections: [
|
|
151
|
+
t.field({ name: 'hasNextPage' }),
|
|
152
|
+
t.field({ name: 'hasPreviousPage' }),
|
|
153
|
+
t.field({ name: 'endCursor' }),
|
|
154
|
+
t.field({ name: 'startCursor' }),
|
|
155
|
+
],
|
|
156
|
+
}),
|
|
157
|
+
}),
|
|
158
|
+
t.field({
|
|
159
|
+
name: 'edges',
|
|
160
|
+
selectionSet: t.selectionSet({
|
|
161
|
+
selections: [
|
|
162
|
+
t.field({ name: 'cursor' }),
|
|
163
|
+
t.field({
|
|
164
|
+
name: 'node',
|
|
165
|
+
selectionSet: t.selectionSet({ selections }),
|
|
166
|
+
}),
|
|
167
|
+
],
|
|
168
|
+
}),
|
|
104
169
|
}),
|
|
105
170
|
],
|
|
106
171
|
}),
|
|
@@ -353,12 +418,13 @@ const getFragment = ({ operationName, query, }) => {
|
|
|
353
418
|
return { name: queryName, ast };
|
|
354
419
|
};
|
|
355
420
|
exports.getFragment = getFragment;
|
|
356
|
-
const getOne = ({ operationName, query, fields, }) => {
|
|
421
|
+
const getOne = ({ operationName, query, fields, }, typeNameOverrides) => {
|
|
357
422
|
const queryName = inflection_1.default.camelize(['get', inflection_1.default.underscore(operationName), 'query'].join('_'), true);
|
|
358
423
|
const variableDefinitions = objectToArray(query.properties)
|
|
359
424
|
.filter((field) => field.isNotNull)
|
|
360
425
|
.map(({ name, type, isNotNull, isArray, isArrayNotNull }) => {
|
|
361
|
-
|
|
426
|
+
const typeName = resolveTypeName(name, type, typeNameOverrides);
|
|
427
|
+
let gqlType = t.namedType({ type: typeName });
|
|
362
428
|
if (isNotNull) {
|
|
363
429
|
gqlType = t.nonNullType({ type: gqlType });
|
|
364
430
|
}
|
|
@@ -400,7 +466,7 @@ const getOne = ({ operationName, query, fields, }) => {
|
|
|
400
466
|
return { name: queryName, ast };
|
|
401
467
|
};
|
|
402
468
|
exports.getOne = getOne;
|
|
403
|
-
const createOne = ({ operationName, mutation, }) => {
|
|
469
|
+
const createOne = ({ operationName, mutation, selection, }, typeNameOverrides, typeIndex) => {
|
|
404
470
|
const mutationName = inflection_1.default.camelize([inflection_1.default.underscore(operationName), 'mutation'].join('_'), true);
|
|
405
471
|
if (!mutation.properties?.input?.properties) {
|
|
406
472
|
console.log('no input field for mutation for ' + mutationName);
|
|
@@ -408,55 +474,81 @@ const createOne = ({ operationName, mutation, }) => {
|
|
|
408
474
|
}
|
|
409
475
|
const modelName = inflection_1.default.camelize([pluralize_1.default.singular(mutation.model)].join('_'), true);
|
|
410
476
|
const allAttrs = objectToArray(mutation.properties.input.properties[modelName].properties);
|
|
411
|
-
const attrs = allAttrs.filter((field) => !NON_MUTABLE_PROPS.includes(field.name));
|
|
477
|
+
const attrs = allAttrs.filter((field) => field.name === 'id' ? Boolean(field.isNotNull) : !NON_MUTABLE_PROPS.includes(field.name));
|
|
478
|
+
const useRaw = selection?.mutationInputMode === 'raw';
|
|
479
|
+
const inputTypeName = resolveTypeName('input', mutation.properties?.input?.type || mutation.properties?.input, typeNameOverrides);
|
|
480
|
+
let unresolved = 0;
|
|
481
|
+
let modelInputName = null;
|
|
482
|
+
if (typeIndex && inputTypeName) {
|
|
483
|
+
const modelRef = typeIndex.getInputFieldType(inputTypeName, modelName);
|
|
484
|
+
modelInputName = refToNamedTypeName(modelRef);
|
|
485
|
+
}
|
|
412
486
|
const variableDefinitions = attrs.map(({ name, type, isNotNull, isArray, isArrayNotNull }) => {
|
|
413
|
-
let gqlType =
|
|
414
|
-
if (
|
|
415
|
-
|
|
487
|
+
let gqlType = null;
|
|
488
|
+
if (typeIndex && modelInputName) {
|
|
489
|
+
const fieldTypeRef = typeIndex.getInputFieldType(modelInputName, name);
|
|
490
|
+
const tn = refToTypeNode(fieldTypeRef, typeNameOverrides);
|
|
491
|
+
if (tn)
|
|
492
|
+
gqlType = tn;
|
|
416
493
|
}
|
|
417
|
-
if (
|
|
418
|
-
|
|
419
|
-
|
|
494
|
+
if (!gqlType) {
|
|
495
|
+
const typeName = resolveTypeName(name, type, typeNameOverrides);
|
|
496
|
+
gqlType = t.namedType({ type: typeName });
|
|
497
|
+
if (isNotNull) {
|
|
420
498
|
gqlType = t.nonNullType({ type: gqlType });
|
|
421
499
|
}
|
|
500
|
+
if (isArray) {
|
|
501
|
+
gqlType = t.listType({ type: gqlType });
|
|
502
|
+
if (isArrayNotNull) {
|
|
503
|
+
gqlType = t.nonNullType({ type: gqlType });
|
|
504
|
+
}
|
|
505
|
+
}
|
|
422
506
|
}
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
});
|
|
507
|
+
const nn = extractNamedTypeName(gqlType);
|
|
508
|
+
if (nn === 'JSON')
|
|
509
|
+
unresolved++;
|
|
510
|
+
return t.variableDefinition({ variable: t.variable({ name }), type: gqlType });
|
|
427
511
|
});
|
|
428
|
-
const
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
512
|
+
const mustUseRaw = useRaw || unresolved > 0;
|
|
513
|
+
const selectArgs = mustUseRaw
|
|
514
|
+
? [t.argument({ name: 'input', value: t.variable({ name: 'input' }) })]
|
|
515
|
+
: [
|
|
516
|
+
t.argument({
|
|
517
|
+
name: 'input',
|
|
518
|
+
value: t.objectValue({
|
|
519
|
+
fields: [
|
|
520
|
+
t.objectField({
|
|
521
|
+
name: modelName,
|
|
522
|
+
value: t.objectValue({
|
|
523
|
+
fields: attrs.map((field) => t.objectField({ name: field.name, value: t.variable({ name: field.name }) })),
|
|
524
|
+
}),
|
|
440
525
|
}),
|
|
441
|
-
|
|
442
|
-
|
|
526
|
+
],
|
|
527
|
+
}),
|
|
443
528
|
}),
|
|
444
|
-
|
|
445
|
-
];
|
|
529
|
+
];
|
|
446
530
|
const selections = allAttrs.map((field) => t.field({ name: 'id' }));
|
|
531
|
+
const modelFields = selection?.modelFields?.[modelName] || selection?.defaultMutationModelFields || ['id'];
|
|
532
|
+
const nested = (modelFields.length > 0)
|
|
533
|
+
? [t.field({
|
|
534
|
+
name: modelName,
|
|
535
|
+
selectionSet: t.selectionSet({ selections: modelFields.map((f) => t.field({ name: f })) }),
|
|
536
|
+
})]
|
|
537
|
+
: [];
|
|
447
538
|
const ast = (0, exports.createGqlMutation)({
|
|
448
539
|
operationName,
|
|
449
540
|
mutationName,
|
|
450
541
|
selectArgs,
|
|
451
|
-
selections: [t.field({ name: 'clientMutationId' })],
|
|
452
|
-
variableDefinitions
|
|
453
|
-
|
|
542
|
+
selections: [...nested, t.field({ name: 'clientMutationId' })],
|
|
543
|
+
variableDefinitions: mustUseRaw
|
|
544
|
+
? [t.variableDefinition({ variable: t.variable({ name: 'input' }), type: t.nonNullType({ type: t.namedType({ type: inputTypeName }) }) })]
|
|
545
|
+
: variableDefinitions,
|
|
454
546
|
useModel: false,
|
|
455
547
|
});
|
|
456
548
|
return { name: mutationName, ast };
|
|
457
549
|
};
|
|
458
550
|
exports.createOne = createOne;
|
|
459
|
-
const patchOne = ({ operationName, mutation, }) => {
|
|
551
|
+
const patchOne = ({ operationName, mutation, selection, }, typeNameOverrides, typeIndex) => {
|
|
460
552
|
const mutationName = inflection_1.default.camelize([inflection_1.default.underscore(operationName), 'mutation'].join('_'), true);
|
|
461
553
|
if (!mutation.properties?.input?.properties) {
|
|
462
554
|
console.log('no input field for mutation for ' + mutationName);
|
|
@@ -470,80 +562,111 @@ const patchOne = ({ operationName, mutation, }) => {
|
|
|
470
562
|
(prop) => !NON_MUTABLE_PROPS.includes(prop.name));
|
|
471
563
|
const patchByAttrs = objectToArray(mutation.properties.input.properties).filter((n) => n.name !== 'patch');
|
|
472
564
|
const patchers = patchByAttrs.map((p) => p.name);
|
|
473
|
-
const
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
565
|
+
const useCollapsedOpt = selection?.mutationInputMode === 'patchCollapsed';
|
|
566
|
+
const ModelPascal = inflection_1.default.camelize(pluralize_1.default.singular(mutation.model), false);
|
|
567
|
+
const patchTypeName = `${ModelPascal}Patch`;
|
|
568
|
+
const inputTypeName = resolveTypeName('input', mutation.properties?.input?.type || mutation.properties?.input, typeNameOverrides);
|
|
569
|
+
let unresolved = 0;
|
|
570
|
+
const patchAttrVarDefs = useCollapsedOpt
|
|
571
|
+
? [
|
|
572
|
+
t.variableDefinition({
|
|
573
|
+
variable: t.variable({ name: 'patch' }),
|
|
574
|
+
type: t.nonNullType({ type: t.namedType({ type: patchTypeName }) }),
|
|
575
|
+
}),
|
|
576
|
+
]
|
|
577
|
+
: patchAttrs
|
|
578
|
+
.filter((field) => !patchers.includes(field.name))
|
|
579
|
+
.map(({ name, type, isArray }) => {
|
|
580
|
+
let gqlType = null;
|
|
581
|
+
if (typeIndex) {
|
|
582
|
+
const pType = typeIndex.byName[patchTypeName];
|
|
583
|
+
const f = pType && pType.inputFields && pType.inputFields.find((x) => x.name === name);
|
|
584
|
+
if (f && f.type)
|
|
585
|
+
gqlType = refToTypeNode(f.type, typeNameOverrides);
|
|
586
|
+
}
|
|
587
|
+
if (!gqlType) {
|
|
588
|
+
const typeName = resolveTypeName(name, type, typeNameOverrides);
|
|
589
|
+
gqlType = t.namedType({ type: typeName });
|
|
590
|
+
if (isArray) {
|
|
591
|
+
gqlType = t.listType({ type: gqlType });
|
|
592
|
+
}
|
|
593
|
+
if (patchers.includes(name)) {
|
|
594
|
+
gqlType = t.nonNullType({ type: gqlType });
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
const nn = extractNamedTypeName(gqlType);
|
|
598
|
+
if (nn === 'JSON')
|
|
599
|
+
unresolved++;
|
|
600
|
+
return t.variableDefinition({ variable: t.variable({ name }), type: gqlType });
|
|
491
601
|
});
|
|
492
|
-
});
|
|
493
602
|
const patchByVarDefs = patchByAttrs.map(({ name, type, isNotNull, isArray, isArrayNotNull }) => {
|
|
494
|
-
let gqlType =
|
|
495
|
-
if (
|
|
496
|
-
|
|
603
|
+
let gqlType = null;
|
|
604
|
+
if (typeIndex && inputTypeName) {
|
|
605
|
+
const ref = typeIndex.getInputFieldType(inputTypeName, name);
|
|
606
|
+
const tn = refToTypeNode(ref, typeNameOverrides);
|
|
607
|
+
if (tn)
|
|
608
|
+
gqlType = tn;
|
|
497
609
|
}
|
|
498
|
-
if (
|
|
499
|
-
|
|
500
|
-
|
|
610
|
+
if (!gqlType) {
|
|
611
|
+
const typeName = resolveTypeName(name, type, typeNameOverrides);
|
|
612
|
+
gqlType = t.namedType({ type: typeName });
|
|
613
|
+
if (isNotNull) {
|
|
501
614
|
gqlType = t.nonNullType({ type: gqlType });
|
|
502
615
|
}
|
|
616
|
+
if (isArray) {
|
|
617
|
+
gqlType = t.listType({ type: gqlType });
|
|
618
|
+
if (isArrayNotNull) {
|
|
619
|
+
gqlType = t.nonNullType({ type: gqlType });
|
|
620
|
+
}
|
|
621
|
+
}
|
|
503
622
|
}
|
|
623
|
+
const nn = extractNamedTypeName(gqlType);
|
|
624
|
+
if (nn === 'JSON')
|
|
625
|
+
unresolved++;
|
|
504
626
|
return t.variableDefinition({ variable: t.variable({ name }), type: gqlType });
|
|
505
627
|
});
|
|
506
|
-
const
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
// @ts-ignore
|
|
523
|
-
name: field.name,
|
|
524
|
-
// @ts-ignore
|
|
525
|
-
value: t.variable({ name: field.name }),
|
|
526
|
-
})),
|
|
628
|
+
const mustUseRaw = useCollapsedOpt || unresolved > 0;
|
|
629
|
+
const selectArgs = mustUseRaw
|
|
630
|
+
? [t.argument({ name: 'input', value: t.variable({ name: 'input' }) })]
|
|
631
|
+
: [
|
|
632
|
+
t.argument({
|
|
633
|
+
name: 'input',
|
|
634
|
+
value: t.objectValue({
|
|
635
|
+
fields: [
|
|
636
|
+
...patchByAttrs.map((field) => t.objectField({ name: field.name, value: t.variable({ name: field.name }) })),
|
|
637
|
+
t.objectField({
|
|
638
|
+
name: 'patch',
|
|
639
|
+
value: useCollapsedOpt ? t.variable({ name: 'patch' }) : t.objectValue({
|
|
640
|
+
fields: patchAttrs
|
|
641
|
+
.filter((field) => !patchers.includes(field.name))
|
|
642
|
+
.map((field) => t.objectField({ name: field.name, value: t.variable({ name: field.name }) })),
|
|
643
|
+
}),
|
|
527
644
|
}),
|
|
528
|
-
|
|
529
|
-
|
|
645
|
+
],
|
|
646
|
+
}),
|
|
530
647
|
}),
|
|
531
|
-
|
|
532
|
-
];
|
|
533
|
-
const
|
|
648
|
+
];
|
|
649
|
+
const modelFields = selection?.modelFields?.[modelName] || selection?.defaultMutationModelFields || ['id'];
|
|
650
|
+
const nestedPatch = (modelFields.length > 0)
|
|
651
|
+
? [t.field({
|
|
652
|
+
name: modelName,
|
|
653
|
+
selectionSet: t.selectionSet({ selections: modelFields.map((f) => t.field({ name: f })) }),
|
|
654
|
+
})]
|
|
655
|
+
: [];
|
|
534
656
|
const ast = (0, exports.createGqlMutation)({
|
|
535
657
|
operationName,
|
|
536
658
|
mutationName,
|
|
537
659
|
selectArgs,
|
|
538
|
-
selections,
|
|
539
|
-
variableDefinitions:
|
|
540
|
-
|
|
660
|
+
selections: [...nestedPatch, t.field({ name: 'clientMutationId' })],
|
|
661
|
+
variableDefinitions: mustUseRaw
|
|
662
|
+
? [t.variableDefinition({ variable: t.variable({ name: 'input' }), type: t.nonNullType({ type: t.namedType({ type: inputTypeName }) }) })]
|
|
663
|
+
: [...patchByVarDefs, ...patchAttrVarDefs],
|
|
541
664
|
useModel: false,
|
|
542
665
|
});
|
|
543
666
|
return { name: mutationName, ast };
|
|
544
667
|
};
|
|
545
668
|
exports.patchOne = patchOne;
|
|
546
|
-
const deleteOne = ({ operationName, mutation, }) => {
|
|
669
|
+
const deleteOne = ({ operationName, mutation, }, typeNameOverrides, typeIndex) => {
|
|
547
670
|
const mutationName = inflection_1.default.camelize([inflection_1.default.underscore(operationName), 'mutation'].join('_'), true);
|
|
548
671
|
if (!mutation.properties?.input?.properties) {
|
|
549
672
|
console.log('no input field for mutation for ' + mutationName);
|
|
@@ -552,46 +675,59 @@ const deleteOne = ({ operationName, mutation, }) => {
|
|
|
552
675
|
const modelName = inflection_1.default.camelize([pluralize_1.default.singular(mutation.model)].join('_'), true);
|
|
553
676
|
// @ts-ignore
|
|
554
677
|
const deleteAttrs = objectToArray(mutation.properties.input.properties);
|
|
678
|
+
const inputTypeName = resolveTypeName('input', mutation.properties?.input?.type || mutation.properties?.input, typeNameOverrides);
|
|
679
|
+
let unresolved = 0;
|
|
555
680
|
const variableDefinitions = deleteAttrs.map(({ name, type, isNotNull, isArray }) => {
|
|
556
|
-
let gqlType =
|
|
557
|
-
if (
|
|
558
|
-
|
|
681
|
+
let gqlType = null;
|
|
682
|
+
if (typeIndex && inputTypeName) {
|
|
683
|
+
const ref = typeIndex.getInputFieldType(inputTypeName, name);
|
|
684
|
+
const tn = refToTypeNode(ref, typeNameOverrides);
|
|
685
|
+
if (tn)
|
|
686
|
+
gqlType = tn;
|
|
559
687
|
}
|
|
560
|
-
if (
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
688
|
+
if (!gqlType) {
|
|
689
|
+
const typeName = resolveTypeName(name, type, typeNameOverrides);
|
|
690
|
+
gqlType = t.namedType({ type: typeName });
|
|
691
|
+
if (isNotNull) {
|
|
692
|
+
gqlType = t.nonNullType({ type: gqlType });
|
|
693
|
+
}
|
|
694
|
+
if (isArray) {
|
|
695
|
+
gqlType = t.listType({ type: gqlType });
|
|
696
|
+
gqlType = t.nonNullType({ type: gqlType });
|
|
697
|
+
}
|
|
564
698
|
}
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
});
|
|
699
|
+
const nn = extractNamedTypeName(gqlType);
|
|
700
|
+
if (nn === 'JSON')
|
|
701
|
+
unresolved++;
|
|
702
|
+
return t.variableDefinition({ variable: t.variable({ name }), type: gqlType });
|
|
569
703
|
});
|
|
570
|
-
const
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
704
|
+
const mustUseRaw = unresolved > 0;
|
|
705
|
+
const selectArgs = mustUseRaw
|
|
706
|
+
? [t.argument({ name: 'input', value: t.variable({ name: 'input' }) })]
|
|
707
|
+
: [
|
|
708
|
+
t.argument({
|
|
709
|
+
name: 'input',
|
|
710
|
+
value: t.objectValue({
|
|
711
|
+
fields: deleteAttrs.map((f) => t.objectField({ name: f.name, value: t.variable({ name: f.name }) })),
|
|
712
|
+
}),
|
|
578
713
|
}),
|
|
579
|
-
|
|
580
|
-
];
|
|
714
|
+
];
|
|
581
715
|
const selections = [t.field({ name: 'clientMutationId' })];
|
|
582
716
|
const ast = (0, exports.createGqlMutation)({
|
|
583
717
|
operationName,
|
|
584
718
|
mutationName,
|
|
585
719
|
selectArgs,
|
|
586
720
|
selections,
|
|
587
|
-
variableDefinitions
|
|
721
|
+
variableDefinitions: mustUseRaw
|
|
722
|
+
? [t.variableDefinition({ variable: t.variable({ name: 'input' }), type: t.nonNullType({ type: t.namedType({ type: inputTypeName }) }) })]
|
|
723
|
+
: variableDefinitions,
|
|
588
724
|
modelName,
|
|
589
725
|
useModel: false,
|
|
590
726
|
});
|
|
591
727
|
return { name: mutationName, ast };
|
|
592
728
|
};
|
|
593
729
|
exports.deleteOne = deleteOne;
|
|
594
|
-
const createMutation = ({ operationName, mutation, }) => {
|
|
730
|
+
const createMutation = ({ operationName, mutation, selection, }, typeNameOverrides, typeIndex) => {
|
|
595
731
|
const mutationName = inflection_1.default.camelize([inflection_1.default.underscore(operationName), 'mutation'].join('_'), true);
|
|
596
732
|
if (!mutation.properties?.input?.properties) {
|
|
597
733
|
console.log('no input field for mutation for ' + mutationName);
|
|
@@ -599,41 +735,70 @@ const createMutation = ({ operationName, mutation, }) => {
|
|
|
599
735
|
}
|
|
600
736
|
// @ts-ignore
|
|
601
737
|
const otherAttrs = objectToArray(mutation.properties.input.properties);
|
|
602
|
-
const
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
738
|
+
const useRaw = selection?.mutationInputMode === 'raw';
|
|
739
|
+
const inputTypeName = resolveTypeName('input', mutation.properties?.input?.type || mutation.properties?.input, typeNameOverrides);
|
|
740
|
+
let unresolved = 0;
|
|
741
|
+
const builtVarDefs = otherAttrs.map(({ name, type, isArray, isArrayNotNull }) => {
|
|
742
|
+
let gqlType = null;
|
|
743
|
+
if (typeIndex && inputTypeName) {
|
|
744
|
+
const ref = typeIndex.getInputFieldType(inputTypeName, name);
|
|
745
|
+
const tn = refToTypeNode(ref, typeNameOverrides);
|
|
746
|
+
if (tn)
|
|
747
|
+
gqlType = tn;
|
|
748
|
+
}
|
|
749
|
+
if (!gqlType) {
|
|
750
|
+
const typeName = resolveTypeName(name, type, typeNameOverrides);
|
|
751
|
+
gqlType = t.namedType({ type: typeName });
|
|
752
|
+
gqlType = t.nonNullType({ type: gqlType });
|
|
753
|
+
if (isArray) {
|
|
754
|
+
gqlType = t.listType({ type: gqlType });
|
|
755
|
+
if (isArrayNotNull) {
|
|
756
|
+
gqlType = t.nonNullType({ type: gqlType });
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
if (gqlType.type && gqlType.type.type && gqlType.type.type.name === 'JSON') {
|
|
760
|
+
unresolved++;
|
|
610
761
|
}
|
|
611
762
|
}
|
|
612
|
-
return t.variableDefinition({
|
|
613
|
-
variable: t.variable({ name }),
|
|
614
|
-
type: gqlType,
|
|
615
|
-
});
|
|
763
|
+
return t.variableDefinition({ variable: t.variable({ name }), type: gqlType });
|
|
616
764
|
});
|
|
617
|
-
const
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
765
|
+
const mustUseRaw = useRaw || otherAttrs.length === 0 || unresolved > 0;
|
|
766
|
+
const variableDefinitions = mustUseRaw
|
|
767
|
+
? [t.variableDefinition({ variable: t.variable({ name: 'input' }), type: t.nonNullType({ type: t.namedType({ type: inputTypeName }) }) })]
|
|
768
|
+
: builtVarDefs;
|
|
769
|
+
const selectArgs = [
|
|
770
|
+
t.argument({
|
|
771
|
+
name: 'input',
|
|
772
|
+
value: mustUseRaw
|
|
773
|
+
? t.variable({ name: 'input' })
|
|
774
|
+
: t.objectValue({
|
|
775
|
+
fields: otherAttrs.map((f) => t.objectField({ name: f.name, value: t.variable({ name: f.name }) })),
|
|
626
776
|
}),
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
777
|
+
}),
|
|
778
|
+
];
|
|
779
|
+
const scalarOutputs = (mutation.outputs || [])
|
|
780
|
+
.filter((field) => field.type.kind === 'SCALAR')
|
|
781
|
+
.map((f) => f.name);
|
|
782
|
+
const objectOutput = (mutation.outputs || []).find((field) => field.type.kind === 'OBJECT');
|
|
783
|
+
const selections = [];
|
|
784
|
+
if (objectOutput?.name) {
|
|
785
|
+
const modelFieldsRaw = selection?.modelFields?.[objectOutput.name] || selection?.defaultMutationModelFields || [];
|
|
786
|
+
const shouldDropId = /Extension$/i.test(objectOutput.name);
|
|
787
|
+
const fallbackFields = shouldDropId ? [] : ['id'];
|
|
788
|
+
const modelFields = (selection?.forceModelOutput && modelFieldsRaw.length === 0) ? fallbackFields : (modelFieldsRaw.length > 0 ? modelFieldsRaw : []);
|
|
789
|
+
if (modelFields.length > 0) {
|
|
790
|
+
selections.push(t.field({
|
|
791
|
+
name: objectOutput.name,
|
|
792
|
+
selectionSet: t.selectionSet({ selections: modelFields.map((f) => t.field({ name: f })) }),
|
|
793
|
+
}));
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
if (scalarOutputs.length > 0) {
|
|
797
|
+
selections.push(...scalarOutputs.map((o) => t.field({ name: o })));
|
|
798
|
+
}
|
|
799
|
+
else {
|
|
800
|
+
selections.push(t.field({ name: 'clientMutationId' }));
|
|
635
801
|
}
|
|
636
|
-
const selections = outputFields.map((o) => t.field({ name: o }));
|
|
637
802
|
const ast = (0, exports.createGqlMutation)({
|
|
638
803
|
operationName,
|
|
639
804
|
mutationName,
|
|
@@ -644,35 +809,32 @@ const createMutation = ({ operationName, mutation, }) => {
|
|
|
644
809
|
return { name: mutationName, ast };
|
|
645
810
|
};
|
|
646
811
|
exports.createMutation = createMutation;
|
|
647
|
-
const generate = (gql) => {
|
|
812
|
+
const generate = (gql, selection, typeNameOverrides, typeIndex) => {
|
|
648
813
|
return Object.keys(gql).reduce((m, operationName) => {
|
|
649
814
|
const defn = gql[operationName];
|
|
650
815
|
let name;
|
|
651
816
|
let ast;
|
|
652
817
|
if (defn.qtype === 'mutation') {
|
|
653
818
|
if (defn.mutationType === 'create') {
|
|
654
|
-
({ name, ast } = (0, exports.createOne)({ operationName, mutation: defn }) ?? {});
|
|
819
|
+
({ name, ast } = (0, exports.createOne)({ operationName, mutation: defn, selection }, typeNameOverrides, typeIndex) ?? {});
|
|
655
820
|
}
|
|
656
821
|
else if (defn.mutationType === 'patch') {
|
|
657
|
-
({ name, ast } = (0, exports.patchOne)({ operationName, mutation: defn }) ?? {});
|
|
822
|
+
({ name, ast } = (0, exports.patchOne)({ operationName, mutation: defn, selection }, typeNameOverrides, typeIndex) ?? {});
|
|
658
823
|
}
|
|
659
824
|
else if (defn.mutationType === 'delete') {
|
|
660
|
-
({ name, ast } = (0, exports.deleteOne)({ operationName, mutation: defn }) ?? {});
|
|
825
|
+
({ name, ast } = (0, exports.deleteOne)({ operationName, mutation: defn }, typeNameOverrides, typeIndex) ?? {});
|
|
661
826
|
}
|
|
662
827
|
else {
|
|
663
|
-
({ name, ast } = (0, exports.createMutation)({ operationName, mutation: defn }) ?? {});
|
|
828
|
+
({ name, ast } = (0, exports.createMutation)({ operationName, mutation: defn, selection }, typeNameOverrides, typeIndex) ?? {});
|
|
664
829
|
}
|
|
665
830
|
}
|
|
666
831
|
else if (defn.qtype === 'getMany') {
|
|
667
|
-
// getMany + related
|
|
668
832
|
[
|
|
669
833
|
exports.getMany,
|
|
670
834
|
exports.getManyPaginatedEdges,
|
|
671
|
-
exports.getManyPaginatedNodes,
|
|
672
835
|
exports.getOrderByEnums,
|
|
673
836
|
exports.getFragment
|
|
674
837
|
].forEach(fn => {
|
|
675
|
-
// @ts-ignore
|
|
676
838
|
const result = fn({ operationName, query: defn });
|
|
677
839
|
if (result?.name && result?.ast) {
|
|
678
840
|
m[result.name] = result;
|
|
@@ -681,7 +843,7 @@ const generate = (gql) => {
|
|
|
681
843
|
}
|
|
682
844
|
else if (defn.qtype === 'getOne') {
|
|
683
845
|
// @ts-ignore
|
|
684
|
-
({ name, ast } = (0, exports.getOne)({ operationName, query: defn }) ?? {});
|
|
846
|
+
({ name, ast } = (0, exports.getOne)({ operationName, query: defn }, typeNameOverrides) ?? {});
|
|
685
847
|
}
|
|
686
848
|
else {
|
|
687
849
|
console.warn('Unknown qtype for key: ' + operationName);
|
|
@@ -733,8 +895,11 @@ const generateGranular = (gql, model, fields) => {
|
|
|
733
895
|
exports.generateGranular = generateGranular;
|
|
734
896
|
function getSelections(query, fields = []) {
|
|
735
897
|
const useAll = fields.length === 0;
|
|
898
|
+
const shouldDropId = typeof query.model === 'string' && /Extension$/i.test(query.model);
|
|
736
899
|
const mapItem = (item) => {
|
|
737
900
|
if (typeof item === 'string') {
|
|
901
|
+
if (shouldDropId && item === 'id')
|
|
902
|
+
return null;
|
|
738
903
|
if (!useAll && !fields.includes(item))
|
|
739
904
|
return null;
|
|
740
905
|
return t.field({ name: item });
|
|
@@ -750,14 +915,20 @@ function getSelections(query, fields = []) {
|
|
|
750
915
|
if (isMany) {
|
|
751
916
|
return t.field({
|
|
752
917
|
name: item.name,
|
|
753
|
-
args: [
|
|
754
|
-
t.argument({ name: 'first', value: t.intValue({ value: '3' }) }),
|
|
755
|
-
],
|
|
918
|
+
args: [t.argument({ name: 'first', value: t.intValue({ value: '3' }) })],
|
|
756
919
|
selectionSet: t.selectionSet({
|
|
757
920
|
selections: [
|
|
758
921
|
t.field({
|
|
759
|
-
name: '
|
|
760
|
-
selectionSet: t.selectionSet({
|
|
922
|
+
name: 'edges',
|
|
923
|
+
selectionSet: t.selectionSet({
|
|
924
|
+
selections: [
|
|
925
|
+
t.field({ name: 'cursor' }),
|
|
926
|
+
t.field({
|
|
927
|
+
name: 'node',
|
|
928
|
+
selectionSet: t.selectionSet({ selections: item.selection.map((s) => mapItem(s)).filter(Boolean) }),
|
|
929
|
+
}),
|
|
930
|
+
],
|
|
931
|
+
}),
|
|
761
932
|
}),
|
|
762
933
|
],
|
|
763
934
|
}),
|
|
@@ -770,5 +941,8 @@ function getSelections(query, fields = []) {
|
|
|
770
941
|
}
|
|
771
942
|
return null;
|
|
772
943
|
};
|
|
773
|
-
return query.selection
|
|
944
|
+
return query.selection
|
|
945
|
+
.filter((s) => !(shouldDropId && s === 'id'))
|
|
946
|
+
.map((field) => mapItem(field))
|
|
947
|
+
.filter((i) => Boolean(i));
|
|
774
948
|
}
|