@constructive-io/graphql-codegen 2.17.29 → 2.17.30
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 -3
package/esm/gql.js
CHANGED
|
@@ -12,6 +12,52 @@ const NON_MUTABLE_PROPS = [
|
|
|
12
12
|
'updatedBy',
|
|
13
13
|
];
|
|
14
14
|
const objectToArray = (obj) => Object.keys(obj).map((k) => ({ name: k, ...obj[k] }));
|
|
15
|
+
function refToTypeNode(ref, overrides) {
|
|
16
|
+
if (!ref)
|
|
17
|
+
return null;
|
|
18
|
+
if (ref.kind === 'NON_NULL') {
|
|
19
|
+
const inner = refToTypeNode(ref.ofType, overrides);
|
|
20
|
+
return t.nonNullType({ type: inner });
|
|
21
|
+
}
|
|
22
|
+
if (ref.kind === 'LIST') {
|
|
23
|
+
const inner = refToTypeNode(ref.ofType, overrides);
|
|
24
|
+
return t.listType({ type: inner });
|
|
25
|
+
}
|
|
26
|
+
const name = (overrides && overrides[ref.name]) || ref.name;
|
|
27
|
+
return t.namedType({ type: name });
|
|
28
|
+
}
|
|
29
|
+
function resolveTypeName(name, type, overrides) {
|
|
30
|
+
if (typeof type === 'string') {
|
|
31
|
+
const base = type;
|
|
32
|
+
const mapped = overrides && overrides[base];
|
|
33
|
+
return mapped || base;
|
|
34
|
+
}
|
|
35
|
+
if (type && typeof type === 'object') {
|
|
36
|
+
if (typeof type.name === 'string' && type.name.length > 0)
|
|
37
|
+
return type.name;
|
|
38
|
+
let t = type;
|
|
39
|
+
while (t && typeof t === 'object' && t.ofType)
|
|
40
|
+
t = t.ofType;
|
|
41
|
+
if (t && typeof t.name === 'string' && t.name.length > 0) {
|
|
42
|
+
const base = t.name;
|
|
43
|
+
const mapped = overrides && overrides[base];
|
|
44
|
+
return mapped || base;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return 'JSON';
|
|
48
|
+
}
|
|
49
|
+
function refToNamedTypeName(ref) {
|
|
50
|
+
let r = ref;
|
|
51
|
+
while (r && (r.kind === 'NON_NULL' || r.kind === 'LIST'))
|
|
52
|
+
r = r.ofType;
|
|
53
|
+
return r && r.name ? r.name : null;
|
|
54
|
+
}
|
|
55
|
+
function extractNamedTypeName(node) {
|
|
56
|
+
let n = node;
|
|
57
|
+
while (n && !n.name && n.type)
|
|
58
|
+
n = n.type;
|
|
59
|
+
return n && n.name ? n.name : null;
|
|
60
|
+
}
|
|
15
61
|
export const createGqlMutation = ({ operationName, mutationName, selectArgs, variableDefinitions, modelName, selections, useModel = true, }) => {
|
|
16
62
|
const opSel = !modelName
|
|
17
63
|
? [
|
|
@@ -58,8 +104,27 @@ export const getMany = ({ operationName, query, fields, }) => {
|
|
|
58
104
|
selections: [
|
|
59
105
|
t.field({ name: 'totalCount' }),
|
|
60
106
|
t.field({
|
|
61
|
-
name: '
|
|
62
|
-
selectionSet: t.selectionSet({
|
|
107
|
+
name: 'pageInfo',
|
|
108
|
+
selectionSet: t.selectionSet({
|
|
109
|
+
selections: [
|
|
110
|
+
t.field({ name: 'hasNextPage' }),
|
|
111
|
+
t.field({ name: 'hasPreviousPage' }),
|
|
112
|
+
t.field({ name: 'endCursor' }),
|
|
113
|
+
t.field({ name: 'startCursor' }),
|
|
114
|
+
],
|
|
115
|
+
}),
|
|
116
|
+
}),
|
|
117
|
+
t.field({
|
|
118
|
+
name: 'edges',
|
|
119
|
+
selectionSet: t.selectionSet({
|
|
120
|
+
selections: [
|
|
121
|
+
t.field({ name: 'cursor' }),
|
|
122
|
+
t.field({
|
|
123
|
+
name: 'node',
|
|
124
|
+
selectionSet: t.selectionSet({ selections }),
|
|
125
|
+
}),
|
|
126
|
+
],
|
|
127
|
+
}),
|
|
63
128
|
}),
|
|
64
129
|
],
|
|
65
130
|
}),
|
|
@@ -307,12 +372,13 @@ export const getFragment = ({ operationName, query, }) => {
|
|
|
307
372
|
});
|
|
308
373
|
return { name: queryName, ast };
|
|
309
374
|
};
|
|
310
|
-
export const getOne = ({ operationName, query, fields, }) => {
|
|
375
|
+
export const getOne = ({ operationName, query, fields, }, typeNameOverrides) => {
|
|
311
376
|
const queryName = inflection.camelize(['get', inflection.underscore(operationName), 'query'].join('_'), true);
|
|
312
377
|
const variableDefinitions = objectToArray(query.properties)
|
|
313
378
|
.filter((field) => field.isNotNull)
|
|
314
379
|
.map(({ name, type, isNotNull, isArray, isArrayNotNull }) => {
|
|
315
|
-
|
|
380
|
+
const typeName = resolveTypeName(name, type, typeNameOverrides);
|
|
381
|
+
let gqlType = t.namedType({ type: typeName });
|
|
316
382
|
if (isNotNull) {
|
|
317
383
|
gqlType = t.nonNullType({ type: gqlType });
|
|
318
384
|
}
|
|
@@ -353,7 +419,7 @@ export const getOne = ({ operationName, query, fields, }) => {
|
|
|
353
419
|
});
|
|
354
420
|
return { name: queryName, ast };
|
|
355
421
|
};
|
|
356
|
-
export const createOne = ({ operationName, mutation, }) => {
|
|
422
|
+
export const createOne = ({ operationName, mutation, selection, }, typeNameOverrides, typeIndex) => {
|
|
357
423
|
const mutationName = inflection.camelize([inflection.underscore(operationName), 'mutation'].join('_'), true);
|
|
358
424
|
if (!mutation.properties?.input?.properties) {
|
|
359
425
|
console.log('no input field for mutation for ' + mutationName);
|
|
@@ -361,54 +427,80 @@ export const createOne = ({ operationName, mutation, }) => {
|
|
|
361
427
|
}
|
|
362
428
|
const modelName = inflection.camelize([plz.singular(mutation.model)].join('_'), true);
|
|
363
429
|
const allAttrs = objectToArray(mutation.properties.input.properties[modelName].properties);
|
|
364
|
-
const attrs = allAttrs.filter((field) => !NON_MUTABLE_PROPS.includes(field.name));
|
|
430
|
+
const attrs = allAttrs.filter((field) => field.name === 'id' ? Boolean(field.isNotNull) : !NON_MUTABLE_PROPS.includes(field.name));
|
|
431
|
+
const useRaw = selection?.mutationInputMode === 'raw';
|
|
432
|
+
const inputTypeName = resolveTypeName('input', mutation.properties?.input?.type || mutation.properties?.input, typeNameOverrides);
|
|
433
|
+
let unresolved = 0;
|
|
434
|
+
let modelInputName = null;
|
|
435
|
+
if (typeIndex && inputTypeName) {
|
|
436
|
+
const modelRef = typeIndex.getInputFieldType(inputTypeName, modelName);
|
|
437
|
+
modelInputName = refToNamedTypeName(modelRef);
|
|
438
|
+
}
|
|
365
439
|
const variableDefinitions = attrs.map(({ name, type, isNotNull, isArray, isArrayNotNull }) => {
|
|
366
|
-
let gqlType =
|
|
367
|
-
if (
|
|
368
|
-
|
|
440
|
+
let gqlType = null;
|
|
441
|
+
if (typeIndex && modelInputName) {
|
|
442
|
+
const fieldTypeRef = typeIndex.getInputFieldType(modelInputName, name);
|
|
443
|
+
const tn = refToTypeNode(fieldTypeRef, typeNameOverrides);
|
|
444
|
+
if (tn)
|
|
445
|
+
gqlType = tn;
|
|
369
446
|
}
|
|
370
|
-
if (
|
|
371
|
-
|
|
372
|
-
|
|
447
|
+
if (!gqlType) {
|
|
448
|
+
const typeName = resolveTypeName(name, type, typeNameOverrides);
|
|
449
|
+
gqlType = t.namedType({ type: typeName });
|
|
450
|
+
if (isNotNull) {
|
|
373
451
|
gqlType = t.nonNullType({ type: gqlType });
|
|
374
452
|
}
|
|
453
|
+
if (isArray) {
|
|
454
|
+
gqlType = t.listType({ type: gqlType });
|
|
455
|
+
if (isArrayNotNull) {
|
|
456
|
+
gqlType = t.nonNullType({ type: gqlType });
|
|
457
|
+
}
|
|
458
|
+
}
|
|
375
459
|
}
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
});
|
|
460
|
+
const nn = extractNamedTypeName(gqlType);
|
|
461
|
+
if (nn === 'JSON')
|
|
462
|
+
unresolved++;
|
|
463
|
+
return t.variableDefinition({ variable: t.variable({ name }), type: gqlType });
|
|
380
464
|
});
|
|
381
|
-
const
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
465
|
+
const mustUseRaw = useRaw || unresolved > 0;
|
|
466
|
+
const selectArgs = mustUseRaw
|
|
467
|
+
? [t.argument({ name: 'input', value: t.variable({ name: 'input' }) })]
|
|
468
|
+
: [
|
|
469
|
+
t.argument({
|
|
470
|
+
name: 'input',
|
|
471
|
+
value: t.objectValue({
|
|
472
|
+
fields: [
|
|
473
|
+
t.objectField({
|
|
474
|
+
name: modelName,
|
|
475
|
+
value: t.objectValue({
|
|
476
|
+
fields: attrs.map((field) => t.objectField({ name: field.name, value: t.variable({ name: field.name }) })),
|
|
477
|
+
}),
|
|
393
478
|
}),
|
|
394
|
-
|
|
395
|
-
|
|
479
|
+
],
|
|
480
|
+
}),
|
|
396
481
|
}),
|
|
397
|
-
|
|
398
|
-
];
|
|
482
|
+
];
|
|
399
483
|
const selections = allAttrs.map((field) => t.field({ name: 'id' }));
|
|
484
|
+
const modelFields = selection?.modelFields?.[modelName] || selection?.defaultMutationModelFields || ['id'];
|
|
485
|
+
const nested = (modelFields.length > 0)
|
|
486
|
+
? [t.field({
|
|
487
|
+
name: modelName,
|
|
488
|
+
selectionSet: t.selectionSet({ selections: modelFields.map((f) => t.field({ name: f })) }),
|
|
489
|
+
})]
|
|
490
|
+
: [];
|
|
400
491
|
const ast = createGqlMutation({
|
|
401
492
|
operationName,
|
|
402
493
|
mutationName,
|
|
403
494
|
selectArgs,
|
|
404
|
-
selections: [t.field({ name: 'clientMutationId' })],
|
|
405
|
-
variableDefinitions
|
|
406
|
-
|
|
495
|
+
selections: [...nested, t.field({ name: 'clientMutationId' })],
|
|
496
|
+
variableDefinitions: mustUseRaw
|
|
497
|
+
? [t.variableDefinition({ variable: t.variable({ name: 'input' }), type: t.nonNullType({ type: t.namedType({ type: inputTypeName }) }) })]
|
|
498
|
+
: variableDefinitions,
|
|
407
499
|
useModel: false,
|
|
408
500
|
});
|
|
409
501
|
return { name: mutationName, ast };
|
|
410
502
|
};
|
|
411
|
-
export const patchOne = ({ operationName, mutation, }) => {
|
|
503
|
+
export const patchOne = ({ operationName, mutation, selection, }, typeNameOverrides, typeIndex) => {
|
|
412
504
|
const mutationName = inflection.camelize([inflection.underscore(operationName), 'mutation'].join('_'), true);
|
|
413
505
|
if (!mutation.properties?.input?.properties) {
|
|
414
506
|
console.log('no input field for mutation for ' + mutationName);
|
|
@@ -422,79 +514,110 @@ export const patchOne = ({ operationName, mutation, }) => {
|
|
|
422
514
|
(prop) => !NON_MUTABLE_PROPS.includes(prop.name));
|
|
423
515
|
const patchByAttrs = objectToArray(mutation.properties.input.properties).filter((n) => n.name !== 'patch');
|
|
424
516
|
const patchers = patchByAttrs.map((p) => p.name);
|
|
425
|
-
const
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
517
|
+
const useCollapsedOpt = selection?.mutationInputMode === 'patchCollapsed';
|
|
518
|
+
const ModelPascal = inflection.camelize(plz.singular(mutation.model), false);
|
|
519
|
+
const patchTypeName = `${ModelPascal}Patch`;
|
|
520
|
+
const inputTypeName = resolveTypeName('input', mutation.properties?.input?.type || mutation.properties?.input, typeNameOverrides);
|
|
521
|
+
let unresolved = 0;
|
|
522
|
+
const patchAttrVarDefs = useCollapsedOpt
|
|
523
|
+
? [
|
|
524
|
+
t.variableDefinition({
|
|
525
|
+
variable: t.variable({ name: 'patch' }),
|
|
526
|
+
type: t.nonNullType({ type: t.namedType({ type: patchTypeName }) }),
|
|
527
|
+
}),
|
|
528
|
+
]
|
|
529
|
+
: patchAttrs
|
|
530
|
+
.filter((field) => !patchers.includes(field.name))
|
|
531
|
+
.map(({ name, type, isArray }) => {
|
|
532
|
+
let gqlType = null;
|
|
533
|
+
if (typeIndex) {
|
|
534
|
+
const pType = typeIndex.byName[patchTypeName];
|
|
535
|
+
const f = pType && pType.inputFields && pType.inputFields.find((x) => x.name === name);
|
|
536
|
+
if (f && f.type)
|
|
537
|
+
gqlType = refToTypeNode(f.type, typeNameOverrides);
|
|
538
|
+
}
|
|
539
|
+
if (!gqlType) {
|
|
540
|
+
const typeName = resolveTypeName(name, type, typeNameOverrides);
|
|
541
|
+
gqlType = t.namedType({ type: typeName });
|
|
542
|
+
if (isArray) {
|
|
543
|
+
gqlType = t.listType({ type: gqlType });
|
|
544
|
+
}
|
|
545
|
+
if (patchers.includes(name)) {
|
|
546
|
+
gqlType = t.nonNullType({ type: gqlType });
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
const nn = extractNamedTypeName(gqlType);
|
|
550
|
+
if (nn === 'JSON')
|
|
551
|
+
unresolved++;
|
|
552
|
+
return t.variableDefinition({ variable: t.variable({ name }), type: gqlType });
|
|
443
553
|
});
|
|
444
|
-
});
|
|
445
554
|
const patchByVarDefs = patchByAttrs.map(({ name, type, isNotNull, isArray, isArrayNotNull }) => {
|
|
446
|
-
let gqlType =
|
|
447
|
-
if (
|
|
448
|
-
|
|
555
|
+
let gqlType = null;
|
|
556
|
+
if (typeIndex && inputTypeName) {
|
|
557
|
+
const ref = typeIndex.getInputFieldType(inputTypeName, name);
|
|
558
|
+
const tn = refToTypeNode(ref, typeNameOverrides);
|
|
559
|
+
if (tn)
|
|
560
|
+
gqlType = tn;
|
|
449
561
|
}
|
|
450
|
-
if (
|
|
451
|
-
|
|
452
|
-
|
|
562
|
+
if (!gqlType) {
|
|
563
|
+
const typeName = resolveTypeName(name, type, typeNameOverrides);
|
|
564
|
+
gqlType = t.namedType({ type: typeName });
|
|
565
|
+
if (isNotNull) {
|
|
453
566
|
gqlType = t.nonNullType({ type: gqlType });
|
|
454
567
|
}
|
|
568
|
+
if (isArray) {
|
|
569
|
+
gqlType = t.listType({ type: gqlType });
|
|
570
|
+
if (isArrayNotNull) {
|
|
571
|
+
gqlType = t.nonNullType({ type: gqlType });
|
|
572
|
+
}
|
|
573
|
+
}
|
|
455
574
|
}
|
|
575
|
+
const nn = extractNamedTypeName(gqlType);
|
|
576
|
+
if (nn === 'JSON')
|
|
577
|
+
unresolved++;
|
|
456
578
|
return t.variableDefinition({ variable: t.variable({ name }), type: gqlType });
|
|
457
579
|
});
|
|
458
|
-
const
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
// @ts-ignore
|
|
475
|
-
name: field.name,
|
|
476
|
-
// @ts-ignore
|
|
477
|
-
value: t.variable({ name: field.name }),
|
|
478
|
-
})),
|
|
580
|
+
const mustUseRaw = useCollapsedOpt || unresolved > 0;
|
|
581
|
+
const selectArgs = mustUseRaw
|
|
582
|
+
? [t.argument({ name: 'input', value: t.variable({ name: 'input' }) })]
|
|
583
|
+
: [
|
|
584
|
+
t.argument({
|
|
585
|
+
name: 'input',
|
|
586
|
+
value: t.objectValue({
|
|
587
|
+
fields: [
|
|
588
|
+
...patchByAttrs.map((field) => t.objectField({ name: field.name, value: t.variable({ name: field.name }) })),
|
|
589
|
+
t.objectField({
|
|
590
|
+
name: 'patch',
|
|
591
|
+
value: useCollapsedOpt ? t.variable({ name: 'patch' }) : t.objectValue({
|
|
592
|
+
fields: patchAttrs
|
|
593
|
+
.filter((field) => !patchers.includes(field.name))
|
|
594
|
+
.map((field) => t.objectField({ name: field.name, value: t.variable({ name: field.name }) })),
|
|
595
|
+
}),
|
|
479
596
|
}),
|
|
480
|
-
|
|
481
|
-
|
|
597
|
+
],
|
|
598
|
+
}),
|
|
482
599
|
}),
|
|
483
|
-
|
|
484
|
-
];
|
|
485
|
-
const
|
|
600
|
+
];
|
|
601
|
+
const modelFields = selection?.modelFields?.[modelName] || selection?.defaultMutationModelFields || ['id'];
|
|
602
|
+
const nestedPatch = (modelFields.length > 0)
|
|
603
|
+
? [t.field({
|
|
604
|
+
name: modelName,
|
|
605
|
+
selectionSet: t.selectionSet({ selections: modelFields.map((f) => t.field({ name: f })) }),
|
|
606
|
+
})]
|
|
607
|
+
: [];
|
|
486
608
|
const ast = createGqlMutation({
|
|
487
609
|
operationName,
|
|
488
610
|
mutationName,
|
|
489
611
|
selectArgs,
|
|
490
|
-
selections,
|
|
491
|
-
variableDefinitions:
|
|
492
|
-
|
|
612
|
+
selections: [...nestedPatch, t.field({ name: 'clientMutationId' })],
|
|
613
|
+
variableDefinitions: mustUseRaw
|
|
614
|
+
? [t.variableDefinition({ variable: t.variable({ name: 'input' }), type: t.nonNullType({ type: t.namedType({ type: inputTypeName }) }) })]
|
|
615
|
+
: [...patchByVarDefs, ...patchAttrVarDefs],
|
|
493
616
|
useModel: false,
|
|
494
617
|
});
|
|
495
618
|
return { name: mutationName, ast };
|
|
496
619
|
};
|
|
497
|
-
export const deleteOne = ({ operationName, mutation, }) => {
|
|
620
|
+
export const deleteOne = ({ operationName, mutation, }, typeNameOverrides, typeIndex) => {
|
|
498
621
|
const mutationName = inflection.camelize([inflection.underscore(operationName), 'mutation'].join('_'), true);
|
|
499
622
|
if (!mutation.properties?.input?.properties) {
|
|
500
623
|
console.log('no input field for mutation for ' + mutationName);
|
|
@@ -503,45 +626,58 @@ export const deleteOne = ({ operationName, mutation, }) => {
|
|
|
503
626
|
const modelName = inflection.camelize([plz.singular(mutation.model)].join('_'), true);
|
|
504
627
|
// @ts-ignore
|
|
505
628
|
const deleteAttrs = objectToArray(mutation.properties.input.properties);
|
|
629
|
+
const inputTypeName = resolveTypeName('input', mutation.properties?.input?.type || mutation.properties?.input, typeNameOverrides);
|
|
630
|
+
let unresolved = 0;
|
|
506
631
|
const variableDefinitions = deleteAttrs.map(({ name, type, isNotNull, isArray }) => {
|
|
507
|
-
let gqlType =
|
|
508
|
-
if (
|
|
509
|
-
|
|
632
|
+
let gqlType = null;
|
|
633
|
+
if (typeIndex && inputTypeName) {
|
|
634
|
+
const ref = typeIndex.getInputFieldType(inputTypeName, name);
|
|
635
|
+
const tn = refToTypeNode(ref, typeNameOverrides);
|
|
636
|
+
if (tn)
|
|
637
|
+
gqlType = tn;
|
|
510
638
|
}
|
|
511
|
-
if (
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
639
|
+
if (!gqlType) {
|
|
640
|
+
const typeName = resolveTypeName(name, type, typeNameOverrides);
|
|
641
|
+
gqlType = t.namedType({ type: typeName });
|
|
642
|
+
if (isNotNull) {
|
|
643
|
+
gqlType = t.nonNullType({ type: gqlType });
|
|
644
|
+
}
|
|
645
|
+
if (isArray) {
|
|
646
|
+
gqlType = t.listType({ type: gqlType });
|
|
647
|
+
gqlType = t.nonNullType({ type: gqlType });
|
|
648
|
+
}
|
|
515
649
|
}
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
});
|
|
650
|
+
const nn = extractNamedTypeName(gqlType);
|
|
651
|
+
if (nn === 'JSON')
|
|
652
|
+
unresolved++;
|
|
653
|
+
return t.variableDefinition({ variable: t.variable({ name }), type: gqlType });
|
|
520
654
|
});
|
|
521
|
-
const
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
655
|
+
const mustUseRaw = unresolved > 0;
|
|
656
|
+
const selectArgs = mustUseRaw
|
|
657
|
+
? [t.argument({ name: 'input', value: t.variable({ name: 'input' }) })]
|
|
658
|
+
: [
|
|
659
|
+
t.argument({
|
|
660
|
+
name: 'input',
|
|
661
|
+
value: t.objectValue({
|
|
662
|
+
fields: deleteAttrs.map((f) => t.objectField({ name: f.name, value: t.variable({ name: f.name }) })),
|
|
663
|
+
}),
|
|
529
664
|
}),
|
|
530
|
-
|
|
531
|
-
];
|
|
665
|
+
];
|
|
532
666
|
const selections = [t.field({ name: 'clientMutationId' })];
|
|
533
667
|
const ast = createGqlMutation({
|
|
534
668
|
operationName,
|
|
535
669
|
mutationName,
|
|
536
670
|
selectArgs,
|
|
537
671
|
selections,
|
|
538
|
-
variableDefinitions
|
|
672
|
+
variableDefinitions: mustUseRaw
|
|
673
|
+
? [t.variableDefinition({ variable: t.variable({ name: 'input' }), type: t.nonNullType({ type: t.namedType({ type: inputTypeName }) }) })]
|
|
674
|
+
: variableDefinitions,
|
|
539
675
|
modelName,
|
|
540
676
|
useModel: false,
|
|
541
677
|
});
|
|
542
678
|
return { name: mutationName, ast };
|
|
543
679
|
};
|
|
544
|
-
export const createMutation = ({ operationName, mutation, }) => {
|
|
680
|
+
export const createMutation = ({ operationName, mutation, selection, }, typeNameOverrides, typeIndex) => {
|
|
545
681
|
const mutationName = inflection.camelize([inflection.underscore(operationName), 'mutation'].join('_'), true);
|
|
546
682
|
if (!mutation.properties?.input?.properties) {
|
|
547
683
|
console.log('no input field for mutation for ' + mutationName);
|
|
@@ -549,41 +685,70 @@ export const createMutation = ({ operationName, mutation, }) => {
|
|
|
549
685
|
}
|
|
550
686
|
// @ts-ignore
|
|
551
687
|
const otherAttrs = objectToArray(mutation.properties.input.properties);
|
|
552
|
-
const
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
688
|
+
const useRaw = selection?.mutationInputMode === 'raw';
|
|
689
|
+
const inputTypeName = resolveTypeName('input', mutation.properties?.input?.type || mutation.properties?.input, typeNameOverrides);
|
|
690
|
+
let unresolved = 0;
|
|
691
|
+
const builtVarDefs = otherAttrs.map(({ name, type, isArray, isArrayNotNull }) => {
|
|
692
|
+
let gqlType = null;
|
|
693
|
+
if (typeIndex && inputTypeName) {
|
|
694
|
+
const ref = typeIndex.getInputFieldType(inputTypeName, name);
|
|
695
|
+
const tn = refToTypeNode(ref, typeNameOverrides);
|
|
696
|
+
if (tn)
|
|
697
|
+
gqlType = tn;
|
|
698
|
+
}
|
|
699
|
+
if (!gqlType) {
|
|
700
|
+
const typeName = resolveTypeName(name, type, typeNameOverrides);
|
|
701
|
+
gqlType = t.namedType({ type: typeName });
|
|
702
|
+
gqlType = t.nonNullType({ type: gqlType });
|
|
703
|
+
if (isArray) {
|
|
704
|
+
gqlType = t.listType({ type: gqlType });
|
|
705
|
+
if (isArrayNotNull) {
|
|
706
|
+
gqlType = t.nonNullType({ type: gqlType });
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
if (gqlType.type && gqlType.type.type && gqlType.type.type.name === 'JSON') {
|
|
710
|
+
unresolved++;
|
|
560
711
|
}
|
|
561
712
|
}
|
|
562
|
-
return t.variableDefinition({
|
|
563
|
-
variable: t.variable({ name }),
|
|
564
|
-
type: gqlType,
|
|
565
|
-
});
|
|
713
|
+
return t.variableDefinition({ variable: t.variable({ name }), type: gqlType });
|
|
566
714
|
});
|
|
567
|
-
const
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
715
|
+
const mustUseRaw = useRaw || otherAttrs.length === 0 || unresolved > 0;
|
|
716
|
+
const variableDefinitions = mustUseRaw
|
|
717
|
+
? [t.variableDefinition({ variable: t.variable({ name: 'input' }), type: t.nonNullType({ type: t.namedType({ type: inputTypeName }) }) })]
|
|
718
|
+
: builtVarDefs;
|
|
719
|
+
const selectArgs = [
|
|
720
|
+
t.argument({
|
|
721
|
+
name: 'input',
|
|
722
|
+
value: mustUseRaw
|
|
723
|
+
? t.variable({ name: 'input' })
|
|
724
|
+
: t.objectValue({
|
|
725
|
+
fields: otherAttrs.map((f) => t.objectField({ name: f.name, value: t.variable({ name: f.name }) })),
|
|
576
726
|
}),
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
727
|
+
}),
|
|
728
|
+
];
|
|
729
|
+
const scalarOutputs = (mutation.outputs || [])
|
|
730
|
+
.filter((field) => field.type.kind === 'SCALAR')
|
|
731
|
+
.map((f) => f.name);
|
|
732
|
+
const objectOutput = (mutation.outputs || []).find((field) => field.type.kind === 'OBJECT');
|
|
733
|
+
const selections = [];
|
|
734
|
+
if (objectOutput?.name) {
|
|
735
|
+
const modelFieldsRaw = selection?.modelFields?.[objectOutput.name] || selection?.defaultMutationModelFields || [];
|
|
736
|
+
const shouldDropId = /Extension$/i.test(objectOutput.name);
|
|
737
|
+
const fallbackFields = shouldDropId ? [] : ['id'];
|
|
738
|
+
const modelFields = (selection?.forceModelOutput && modelFieldsRaw.length === 0) ? fallbackFields : (modelFieldsRaw.length > 0 ? modelFieldsRaw : []);
|
|
739
|
+
if (modelFields.length > 0) {
|
|
740
|
+
selections.push(t.field({
|
|
741
|
+
name: objectOutput.name,
|
|
742
|
+
selectionSet: t.selectionSet({ selections: modelFields.map((f) => t.field({ name: f })) }),
|
|
743
|
+
}));
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
if (scalarOutputs.length > 0) {
|
|
747
|
+
selections.push(...scalarOutputs.map((o) => t.field({ name: o })));
|
|
748
|
+
}
|
|
749
|
+
else {
|
|
750
|
+
selections.push(t.field({ name: 'clientMutationId' }));
|
|
585
751
|
}
|
|
586
|
-
const selections = outputFields.map((o) => t.field({ name: o }));
|
|
587
752
|
const ast = createGqlMutation({
|
|
588
753
|
operationName,
|
|
589
754
|
mutationName,
|
|
@@ -593,35 +758,32 @@ export const createMutation = ({ operationName, mutation, }) => {
|
|
|
593
758
|
});
|
|
594
759
|
return { name: mutationName, ast };
|
|
595
760
|
};
|
|
596
|
-
export const generate = (gql) => {
|
|
761
|
+
export const generate = (gql, selection, typeNameOverrides, typeIndex) => {
|
|
597
762
|
return Object.keys(gql).reduce((m, operationName) => {
|
|
598
763
|
const defn = gql[operationName];
|
|
599
764
|
let name;
|
|
600
765
|
let ast;
|
|
601
766
|
if (defn.qtype === 'mutation') {
|
|
602
767
|
if (defn.mutationType === 'create') {
|
|
603
|
-
({ name, ast } = createOne({ operationName, mutation: defn }) ?? {});
|
|
768
|
+
({ name, ast } = createOne({ operationName, mutation: defn, selection }, typeNameOverrides, typeIndex) ?? {});
|
|
604
769
|
}
|
|
605
770
|
else if (defn.mutationType === 'patch') {
|
|
606
|
-
({ name, ast } = patchOne({ operationName, mutation: defn }) ?? {});
|
|
771
|
+
({ name, ast } = patchOne({ operationName, mutation: defn, selection }, typeNameOverrides, typeIndex) ?? {});
|
|
607
772
|
}
|
|
608
773
|
else if (defn.mutationType === 'delete') {
|
|
609
|
-
({ name, ast } = deleteOne({ operationName, mutation: defn }) ?? {});
|
|
774
|
+
({ name, ast } = deleteOne({ operationName, mutation: defn }, typeNameOverrides, typeIndex) ?? {});
|
|
610
775
|
}
|
|
611
776
|
else {
|
|
612
|
-
({ name, ast } = createMutation({ operationName, mutation: defn }) ?? {});
|
|
777
|
+
({ name, ast } = createMutation({ operationName, mutation: defn, selection }, typeNameOverrides, typeIndex) ?? {});
|
|
613
778
|
}
|
|
614
779
|
}
|
|
615
780
|
else if (defn.qtype === 'getMany') {
|
|
616
|
-
// getMany + related
|
|
617
781
|
[
|
|
618
782
|
getMany,
|
|
619
783
|
getManyPaginatedEdges,
|
|
620
|
-
getManyPaginatedNodes,
|
|
621
784
|
getOrderByEnums,
|
|
622
785
|
getFragment
|
|
623
786
|
].forEach(fn => {
|
|
624
|
-
// @ts-ignore
|
|
625
787
|
const result = fn({ operationName, query: defn });
|
|
626
788
|
if (result?.name && result?.ast) {
|
|
627
789
|
m[result.name] = result;
|
|
@@ -630,7 +792,7 @@ export const generate = (gql) => {
|
|
|
630
792
|
}
|
|
631
793
|
else if (defn.qtype === 'getOne') {
|
|
632
794
|
// @ts-ignore
|
|
633
|
-
({ name, ast } = getOne({ operationName, query: defn }) ?? {});
|
|
795
|
+
({ name, ast } = getOne({ operationName, query: defn }, typeNameOverrides) ?? {});
|
|
634
796
|
}
|
|
635
797
|
else {
|
|
636
798
|
console.warn('Unknown qtype for key: ' + operationName);
|
|
@@ -680,8 +842,11 @@ export const generateGranular = (gql, model, fields) => {
|
|
|
680
842
|
};
|
|
681
843
|
export function getSelections(query, fields = []) {
|
|
682
844
|
const useAll = fields.length === 0;
|
|
845
|
+
const shouldDropId = typeof query.model === 'string' && /Extension$/i.test(query.model);
|
|
683
846
|
const mapItem = (item) => {
|
|
684
847
|
if (typeof item === 'string') {
|
|
848
|
+
if (shouldDropId && item === 'id')
|
|
849
|
+
return null;
|
|
685
850
|
if (!useAll && !fields.includes(item))
|
|
686
851
|
return null;
|
|
687
852
|
return t.field({ name: item });
|
|
@@ -697,14 +862,20 @@ export function getSelections(query, fields = []) {
|
|
|
697
862
|
if (isMany) {
|
|
698
863
|
return t.field({
|
|
699
864
|
name: item.name,
|
|
700
|
-
args: [
|
|
701
|
-
t.argument({ name: 'first', value: t.intValue({ value: '3' }) }),
|
|
702
|
-
],
|
|
865
|
+
args: [t.argument({ name: 'first', value: t.intValue({ value: '3' }) })],
|
|
703
866
|
selectionSet: t.selectionSet({
|
|
704
867
|
selections: [
|
|
705
868
|
t.field({
|
|
706
|
-
name: '
|
|
707
|
-
selectionSet: t.selectionSet({
|
|
869
|
+
name: 'edges',
|
|
870
|
+
selectionSet: t.selectionSet({
|
|
871
|
+
selections: [
|
|
872
|
+
t.field({ name: 'cursor' }),
|
|
873
|
+
t.field({
|
|
874
|
+
name: 'node',
|
|
875
|
+
selectionSet: t.selectionSet({ selections: item.selection.map((s) => mapItem(s)).filter(Boolean) }),
|
|
876
|
+
}),
|
|
877
|
+
],
|
|
878
|
+
}),
|
|
708
879
|
}),
|
|
709
880
|
],
|
|
710
881
|
}),
|
|
@@ -717,5 +888,8 @@ export function getSelections(query, fields = []) {
|
|
|
717
888
|
}
|
|
718
889
|
return null;
|
|
719
890
|
};
|
|
720
|
-
return query.selection
|
|
891
|
+
return query.selection
|
|
892
|
+
.filter((s) => !(shouldDropId && s === 'id'))
|
|
893
|
+
.map((field) => mapItem(field))
|
|
894
|
+
.filter((i) => Boolean(i));
|
|
721
895
|
}
|