@contello/client 2.2.1 → 2.2.2
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/dist/cli.js +5 -3
- package/dist/{generate-FPYYN7UV.js → generate-Z4LVDAB4.js} +148 -164
- package/dist/index.cjs +169 -101
- package/dist/index.js +169 -101
- package/package.json +2 -2
package/dist/cli.js
CHANGED
|
@@ -3,13 +3,15 @@
|
|
|
3
3
|
// src/cli.ts
|
|
4
4
|
var command = process.argv[2];
|
|
5
5
|
switch (command) {
|
|
6
|
-
case "generate":
|
|
7
|
-
await import("./generate-
|
|
6
|
+
case "generate": {
|
|
7
|
+
await import("./generate-Z4LVDAB4.js");
|
|
8
8
|
break;
|
|
9
|
-
|
|
9
|
+
}
|
|
10
|
+
default: {
|
|
10
11
|
console.error(command ? `unknown command: ${command}` : "usage: contello-client <command>");
|
|
11
12
|
console.error("");
|
|
12
13
|
console.error("commands:");
|
|
13
14
|
console.error(" generate generate TypeScript types from GraphQL schema");
|
|
14
15
|
process.exit(1);
|
|
16
|
+
}
|
|
15
17
|
}
|
|
@@ -27,11 +27,11 @@ async function loadConfig(cwd) {
|
|
|
27
27
|
if (!project.output) throw new Error(`${name}: projects[${i}].output is required`);
|
|
28
28
|
}
|
|
29
29
|
return config;
|
|
30
|
-
} catch (
|
|
31
|
-
if (
|
|
30
|
+
} catch (error) {
|
|
31
|
+
if (error.code === "ERR_MODULE_NOT_FOUND" || error.code === "ENOENT") {
|
|
32
32
|
continue;
|
|
33
33
|
}
|
|
34
|
-
throw
|
|
34
|
+
throw error;
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
throw new Error(`no config file found (tried: ${CONFIG_NAMES.join(", ")})`);
|
|
@@ -156,8 +156,8 @@ function operationDocumentExpression(operation, allFragments) {
|
|
|
156
156
|
const usedFragments = /* @__PURE__ */ new Map();
|
|
157
157
|
collectUsedFragments(operation, allFragments, usedFragments);
|
|
158
158
|
const sortedFragments = sortFragmentsByDependency(usedFragments);
|
|
159
|
-
const
|
|
160
|
-
parts.
|
|
159
|
+
const refs = sortedFragments.map((f) => `\${${f.name.value}FragmentSchema}`);
|
|
160
|
+
const parts = refs.length > 0 ? [refs.join(String.raw`\n`), print(operation)] : [print(operation)];
|
|
161
161
|
return `\`${parts.join("\n")}\``;
|
|
162
162
|
}
|
|
163
163
|
function fragmentBundleExpression(fragment, allFragments) {
|
|
@@ -168,10 +168,10 @@ function fragmentBundleExpression(fragment, allFragments) {
|
|
|
168
168
|
if (sortedFragments.length === 1) {
|
|
169
169
|
return `${sortedFragments[0].name.value}FragmentSchema`;
|
|
170
170
|
}
|
|
171
|
-
return `\`${sortedFragments.map((f) => `\${${f.name.value}FragmentSchema}`).join(
|
|
171
|
+
return `\`${sortedFragments.map((f) => `\${${f.name.value}FragmentSchema}`).join(String.raw`\n`)}\``;
|
|
172
172
|
}
|
|
173
173
|
function generateFragmentSchemas(fragments) {
|
|
174
|
-
const sorted = [...fragments.values()].
|
|
174
|
+
const sorted = [...fragments.values()].toSorted((a, b) => a.name.value.localeCompare(b.name.value));
|
|
175
175
|
const lines = [];
|
|
176
176
|
for (const fragment of sorted) {
|
|
177
177
|
lines.push(`const ${fragment.name.value}FragmentSchema = \`${print(fragment)}\`;`);
|
|
@@ -284,86 +284,105 @@ function resolveSelectionSet(schema, selectionSet, parentType, fragments, indent
|
|
|
284
284
|
const fragmentRefs = [];
|
|
285
285
|
let hasTypenameField = false;
|
|
286
286
|
for (const selection of selectionSet.selections) {
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
const { namedType, isList, isNonNull } = unwrapType(schemaField.type);
|
|
298
|
-
let tsType;
|
|
299
|
-
if (selection.selectionSet && isCompositeType(namedType)) {
|
|
300
|
-
tsType = resolveSelectionSetType(schema, selection.selectionSet, namedType, fragments, `${indent} `);
|
|
301
|
-
} else if (isScalarType(namedType)) {
|
|
302
|
-
tsType = resolveScalarType(namedType.name);
|
|
303
|
-
} else if (isEnumType(namedType)) {
|
|
304
|
-
tsType = namedType.name;
|
|
305
|
-
} else {
|
|
306
|
-
tsType = namedType.toString();
|
|
307
|
-
}
|
|
308
|
-
if (isList) {
|
|
309
|
-
const wrapped = /[ |&]/.test(tsType) ? `(${tsType})` : tsType;
|
|
310
|
-
tsType = `${wrapped}[]`;
|
|
311
|
-
}
|
|
312
|
-
if (isNonNull) {
|
|
313
|
-
fields.push(`${indent} ${fieldName}: ${tsType};`);
|
|
314
|
-
} else {
|
|
315
|
-
fields.push(`${indent} ${fieldName}?: ${tsType} | undefined;`);
|
|
316
|
-
}
|
|
317
|
-
} else if (selection.kind === Kind2.INLINE_FRAGMENT) {
|
|
318
|
-
if (selection.typeCondition && selection.selectionSet) {
|
|
319
|
-
const typeName = selection.typeCondition.name.value;
|
|
320
|
-
const conditionalType = schema.getType(typeName);
|
|
321
|
-
if (!conditionalType) {
|
|
322
|
-
throw new Error(`unknown type "${typeName}" in inline fragment`);
|
|
287
|
+
switch (selection.kind) {
|
|
288
|
+
case Kind2.FIELD: {
|
|
289
|
+
const fieldName = selection.alias?.value ?? selection.name.value;
|
|
290
|
+
if (selection.name.value === "__typename") {
|
|
291
|
+
hasTypenameField = true;
|
|
292
|
+
continue;
|
|
293
|
+
}
|
|
294
|
+
const schemaField = isObjectType(parentType) || isInterfaceType(parentType) ? parentType.getFields()[selection.name.value] : void 0;
|
|
295
|
+
if (!schemaField) {
|
|
296
|
+
throw new Error(`field "${selection.name.value}" does not exist on type "${parentType.name}"`);
|
|
323
297
|
}
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
298
|
+
const { namedType, isList, isNonNull } = unwrapType(schemaField.type);
|
|
299
|
+
let tsType;
|
|
300
|
+
if (selection.selectionSet && isCompositeType(namedType)) {
|
|
301
|
+
tsType = resolveSelectionSetType(schema, selection.selectionSet, namedType, fragments, `${indent} `);
|
|
302
|
+
} else if (isScalarType(namedType)) {
|
|
303
|
+
tsType = resolveScalarType(namedType.name);
|
|
304
|
+
} else if (isEnumType(namedType)) {
|
|
305
|
+
tsType = namedType.name;
|
|
306
|
+
} else {
|
|
307
|
+
tsType = namedType.toString();
|
|
308
|
+
}
|
|
309
|
+
if (isList) {
|
|
310
|
+
const wrapped = /[ |&]/.test(tsType) ? `(${tsType})` : tsType;
|
|
311
|
+
tsType = `${wrapped}[]`;
|
|
312
|
+
}
|
|
313
|
+
if (isNonNull) {
|
|
314
|
+
fields.push(`${indent} ${fieldName}: ${tsType};`);
|
|
315
|
+
} else {
|
|
316
|
+
fields.push(`${indent} ${fieldName}?: ${tsType} | undefined;`);
|
|
317
|
+
}
|
|
318
|
+
break;
|
|
319
|
+
}
|
|
320
|
+
case Kind2.INLINE_FRAGMENT: {
|
|
321
|
+
if (selection.typeCondition && selection.selectionSet) {
|
|
322
|
+
const typeName = selection.typeCondition.name.value;
|
|
323
|
+
const conditionalType = schema.getType(typeName);
|
|
324
|
+
if (!conditionalType) {
|
|
325
|
+
throw new Error(`unknown type "${typeName}" in inline fragment`);
|
|
326
|
+
}
|
|
327
|
+
if (isCompositeType(conditionalType)) {
|
|
328
|
+
if (isInterfaceType(conditionalType)) {
|
|
329
|
+
const inner = resolveSelectionSet(schema, selection.selectionSet, conditionalType, fragments, indent);
|
|
330
|
+
fields.push(...inner.fields);
|
|
331
|
+
fragmentRefs.push(...inner.fragmentRefs);
|
|
332
|
+
} else if (isObjectType(conditionalType)) {
|
|
333
|
+
const nested = resolveSelectionSetType(
|
|
334
|
+
schema,
|
|
335
|
+
selection.selectionSet,
|
|
336
|
+
conditionalType,
|
|
337
|
+
fragments,
|
|
338
|
+
indent
|
|
339
|
+
);
|
|
340
|
+
if (nested.startsWith("{")) {
|
|
341
|
+
const model = isContelloModel(schema, conditionalType.name) ? deriveModelName(conditionalType.name) : void 0;
|
|
342
|
+
const discriminant = model ? `${indent} __typename: '${conditionalType.name}';
|
|
334
343
|
${indent} __model: '${model}';` : `${indent} __typename: '${conditionalType.name}';`;
|
|
335
|
-
|
|
344
|
+
inlineUnions.push(`{
|
|
336
345
|
${discriminant}
|
|
337
346
|
${nested.slice(2)}`);
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
347
|
+
} else {
|
|
348
|
+
const model = isContelloModel(schema, conditionalType.name) ? deriveModelName(conditionalType.name) : void 0;
|
|
349
|
+
const discriminant = model ? `{
|
|
341
350
|
${indent} __typename: '${conditionalType.name}';
|
|
342
351
|
${indent} __model: '${model}';
|
|
343
352
|
${indent}}` : `{
|
|
344
353
|
${indent} __typename: '${conditionalType.name}';
|
|
345
354
|
${indent}}`;
|
|
346
|
-
|
|
355
|
+
inlineUnions.push(`${discriminant} & ${nested}`);
|
|
356
|
+
}
|
|
357
|
+
} else {
|
|
358
|
+
const nested = resolveSelectionSetType(
|
|
359
|
+
schema,
|
|
360
|
+
selection.selectionSet,
|
|
361
|
+
conditionalType,
|
|
362
|
+
fragments,
|
|
363
|
+
indent
|
|
364
|
+
);
|
|
365
|
+
inlineUnions.push(nested);
|
|
347
366
|
}
|
|
348
|
-
} else {
|
|
349
|
-
const nested = resolveSelectionSetType(schema, selection.selectionSet, conditionalType, fragments, indent);
|
|
350
|
-
inlineUnions.push(nested);
|
|
351
367
|
}
|
|
352
368
|
}
|
|
369
|
+
break;
|
|
353
370
|
}
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
371
|
+
case Kind2.FRAGMENT_SPREAD: {
|
|
372
|
+
const fragmentName = selection.name.value;
|
|
373
|
+
const fragment = fragments.get(fragmentName);
|
|
374
|
+
if (fragment?.selectionSet) {
|
|
375
|
+
const fragmentType = schema.getType(fragment.typeCondition.name.value);
|
|
376
|
+
if (fragmentType && isCompositeType(fragmentType)) {
|
|
377
|
+
if (!checkTypesOverlap(schema, parentType, fragmentType)) {
|
|
378
|
+
throw new Error(
|
|
379
|
+
`fragment "${fragmentName}" (on ${fragmentType.name}) cannot be spread on type "${parentType.name}"`
|
|
380
|
+
);
|
|
381
|
+
}
|
|
382
|
+
fragmentRefs.push(`${fragmentName}Fragment`);
|
|
364
383
|
}
|
|
365
|
-
fragmentRefs.push(`${fragmentName}Fragment`);
|
|
366
384
|
}
|
|
385
|
+
break;
|
|
367
386
|
}
|
|
368
387
|
}
|
|
369
388
|
}
|
|
@@ -468,35 +487,27 @@ function generateOperationTypes(schema, operations, fragments) {
|
|
|
468
487
|
}
|
|
469
488
|
lines.push("");
|
|
470
489
|
const variablesType = generateVariablesType(schema, op);
|
|
471
|
-
lines.push(`export type ${variablesTypeName} = ${variablesType}
|
|
472
|
-
lines.push("");
|
|
490
|
+
lines.push(`export type ${variablesTypeName} = ${variablesType};`, "");
|
|
473
491
|
}
|
|
474
492
|
return lines.join("\n");
|
|
475
493
|
}
|
|
476
494
|
|
|
477
495
|
// src/generate/operations.ts
|
|
478
496
|
function generateOperationsType(operations) {
|
|
479
|
-
const lines = [];
|
|
480
|
-
lines.push("export type Operations = {");
|
|
497
|
+
const lines = ["export type Operations = {"];
|
|
481
498
|
for (const op of operations) {
|
|
482
499
|
const name = op.name.value;
|
|
483
500
|
const camelName = uncapitalize(name);
|
|
484
501
|
const typeSuffix = pascalCase(op.operation);
|
|
485
502
|
const resultType = `${name}${typeSuffix}`;
|
|
486
503
|
const variablesType = `${resultType}Variables`;
|
|
487
|
-
lines.push(` ${camelName}: {`);
|
|
488
|
-
lines.push(` document: string;`);
|
|
489
|
-
lines.push(` kind: '${op.operation}';`);
|
|
490
|
-
lines.push(` __result?: ${resultType} | undefined;`);
|
|
491
|
-
lines.push(` __variables?: ${variablesType} | undefined;`);
|
|
492
|
-
lines.push(" };");
|
|
504
|
+
lines.push(` ${camelName}: {`, ` document: string;`, ` kind: '${op.operation}';`, ` __result?: ${resultType} | undefined;`, ` __variables?: ${variablesType} | undefined;`, " };");
|
|
493
505
|
}
|
|
494
506
|
lines.push("};");
|
|
495
507
|
return lines.join("\n");
|
|
496
508
|
}
|
|
497
509
|
function generateOperationsConst(operations) {
|
|
498
|
-
const lines = [];
|
|
499
|
-
lines.push("const operations: Operations = {");
|
|
510
|
+
const lines = ["const operations: Operations = {"];
|
|
500
511
|
for (const op of operations) {
|
|
501
512
|
const camelName = uncapitalize(op.name.value);
|
|
502
513
|
lines.push(` ${camelName}: { document: ${camelName}Document, kind: '${op.operation}' },`);
|
|
@@ -525,30 +536,24 @@ function typeToTs(type, nonNull = false) {
|
|
|
525
536
|
const wrapped = inner.includes(" | ") ? `(${inner})` : inner;
|
|
526
537
|
return nonNull ? `${wrapped}[]` : `${wrapped}[] | undefined`;
|
|
527
538
|
}
|
|
528
|
-
|
|
529
|
-
if (isScalarType2(type)) {
|
|
530
|
-
name = resolveScalarType(type.name);
|
|
531
|
-
} else {
|
|
532
|
-
name = type.name;
|
|
533
|
-
}
|
|
539
|
+
const name = isScalarType2(type) ? resolveScalarType(type.name) : type.name;
|
|
534
540
|
return nonNull ? name : `${name} | undefined`;
|
|
535
541
|
}
|
|
536
542
|
function inputTypeToTs(type, nonNull = false) {
|
|
537
543
|
return typeToTs(type, nonNull);
|
|
538
544
|
}
|
|
539
545
|
function generateEnum(type) {
|
|
540
|
-
const values = type.getValues().map((v) => `'${v.value}'`).
|
|
546
|
+
const values = type.getValues().map((v) => `'${v.value}'`).toSorted().join(" | ");
|
|
541
547
|
return `export type ${type.name} = ${values};`;
|
|
542
548
|
}
|
|
543
549
|
function sortedFields(type) {
|
|
544
|
-
return Object.values(type.getFields()).
|
|
550
|
+
return Object.values(type.getFields()).toSorted((a, b) => a.name.localeCompare(b.name));
|
|
545
551
|
}
|
|
546
552
|
function generateObjectType(type, modelNames) {
|
|
547
553
|
const fields = sortedFields(type);
|
|
548
554
|
const lines = [];
|
|
549
555
|
const model = modelNames.get(type.name);
|
|
550
|
-
lines.push(`export type ${type.name} = {`);
|
|
551
|
-
lines.push(` __typename?: '${type.name}' | undefined;`);
|
|
556
|
+
lines.push(`export type ${type.name} = {`, ` __typename?: '${type.name}' | undefined;`);
|
|
552
557
|
if (model) {
|
|
553
558
|
lines.push(` __model?: '${model}' | undefined;`);
|
|
554
559
|
}
|
|
@@ -560,8 +565,7 @@ function generateObjectType(type, modelNames) {
|
|
|
560
565
|
}
|
|
561
566
|
function generateInputType(type) {
|
|
562
567
|
const fields = sortedFields(type);
|
|
563
|
-
const lines = [];
|
|
564
|
-
lines.push(`export type ${type.name} = {`);
|
|
568
|
+
const lines = [`export type ${type.name} = {`];
|
|
565
569
|
for (const field of fields) {
|
|
566
570
|
lines.push(` ${field.name}${isNonNullType2(field.type) ? "" : "?"}: ${inputTypeToTs(field.type)};`);
|
|
567
571
|
}
|
|
@@ -569,7 +573,7 @@ function generateInputType(type) {
|
|
|
569
573
|
return lines.join("\n");
|
|
570
574
|
}
|
|
571
575
|
function generateUnion(type) {
|
|
572
|
-
const members = type.getTypes().map((t) => t.name).
|
|
576
|
+
const members = type.getTypes().map((t) => t.name).toSorted();
|
|
573
577
|
return `export type ${type.name} = ${members.join(" | ")};`;
|
|
574
578
|
}
|
|
575
579
|
function isInternalType(name) {
|
|
@@ -579,24 +583,23 @@ function generateSchemaTypes(schema) {
|
|
|
579
583
|
const typeMap = schema.getTypeMap();
|
|
580
584
|
const lines = [];
|
|
581
585
|
const byName = (a, b) => a.name.localeCompare(b.name);
|
|
582
|
-
const customScalars = Object.values(typeMap).filter((t) => isScalarType2(t) && !isInternalType(t.name) && !SCALAR_MAP[t.name]).
|
|
586
|
+
const customScalars = Object.values(typeMap).filter((t) => isScalarType2(t) && !isInternalType(t.name) && !SCALAR_MAP[t.name]).toSorted(byName);
|
|
583
587
|
if (customScalars.length > 0) {
|
|
584
588
|
for (const scalar of customScalars) {
|
|
585
589
|
lines.push(`export type ${scalar.name} = ${DEFAULT_CUSTOM_SCALAR};`);
|
|
586
590
|
}
|
|
587
591
|
lines.push("");
|
|
588
592
|
}
|
|
589
|
-
const enums = Object.values(typeMap).filter((t) => isEnumType2(t) && !isInternalType(t.name)).
|
|
593
|
+
const enums = Object.values(typeMap).filter((t) => isEnumType2(t) && !isInternalType(t.name)).toSorted(byName);
|
|
590
594
|
for (const e of enums) {
|
|
591
595
|
lines.push(generateEnum(e));
|
|
592
596
|
}
|
|
593
597
|
if (enums.length > 0) {
|
|
594
598
|
lines.push("");
|
|
595
599
|
}
|
|
596
|
-
const inputs = Object.values(typeMap).filter((t) => isInputObjectType(t) && !isInternalType(t.name)).
|
|
600
|
+
const inputs = Object.values(typeMap).filter((t) => isInputObjectType(t) && !isInternalType(t.name)).toSorted(byName);
|
|
597
601
|
for (const input of inputs) {
|
|
598
|
-
lines.push(generateInputType(input));
|
|
599
|
-
lines.push("");
|
|
602
|
+
lines.push(generateInputType(input), "");
|
|
600
603
|
}
|
|
601
604
|
const modelNames = /* @__PURE__ */ new Map();
|
|
602
605
|
const entityModelNames = /* @__PURE__ */ new Map();
|
|
@@ -614,27 +617,23 @@ function generateSchemaTypes(schema) {
|
|
|
614
617
|
}
|
|
615
618
|
}
|
|
616
619
|
}
|
|
617
|
-
const objects = Object.values(typeMap).filter((t) => isObjectType2(t) && !isInternalType(t.name)).
|
|
620
|
+
const objects = Object.values(typeMap).filter((t) => isObjectType2(t) && !isInternalType(t.name)).toSorted(byName);
|
|
618
621
|
for (const obj of objects) {
|
|
619
|
-
lines.push(generateObjectType(obj, modelNames));
|
|
620
|
-
lines.push("");
|
|
622
|
+
lines.push(generateObjectType(obj, modelNames), "");
|
|
621
623
|
}
|
|
622
|
-
const interfaces = Object.values(typeMap).filter((t) => isInterfaceType2(t) && !isInternalType(t.name)).
|
|
624
|
+
const interfaces = Object.values(typeMap).filter((t) => isInterfaceType2(t) && !isInternalType(t.name)).toSorted(byName);
|
|
623
625
|
for (const iface of interfaces) {
|
|
624
|
-
const fields = Object.values(iface.getFields()).
|
|
626
|
+
const fields = Object.values(iface.getFields()).toSorted(
|
|
625
627
|
(a, b) => a.name.localeCompare(b.name)
|
|
626
628
|
);
|
|
627
|
-
const fieldLines = [];
|
|
628
|
-
fieldLines.push(`export type ${iface.name} = {`);
|
|
629
|
-
fieldLines.push(` __typename?: string | undefined;`);
|
|
629
|
+
const fieldLines = [`export type ${iface.name} = {`, ` __typename?: string | undefined;`];
|
|
630
630
|
for (const field of fields) {
|
|
631
631
|
fieldLines.push(` ${field.name}${isNonNullType2(field.type) ? "" : "?"}: ${typeToTs(field.type)};`);
|
|
632
632
|
}
|
|
633
633
|
fieldLines.push("};");
|
|
634
|
-
lines.push(fieldLines.join("\n"));
|
|
635
|
-
lines.push("");
|
|
634
|
+
lines.push(fieldLines.join("\n"), "");
|
|
636
635
|
}
|
|
637
|
-
const unions = Object.values(typeMap).filter((t) => isUnionType(t) && !isInternalType(t.name)).
|
|
636
|
+
const unions = Object.values(typeMap).filter((t) => isUnionType(t) && !isInternalType(t.name)).toSorted(byName);
|
|
638
637
|
for (const union of unions) {
|
|
639
638
|
lines.push(generateUnion(union));
|
|
640
639
|
}
|
|
@@ -750,15 +749,14 @@ function organize(entries) {
|
|
|
750
749
|
}
|
|
751
750
|
byKey.set(entry.binding.sourceKey, entry);
|
|
752
751
|
}
|
|
753
|
-
return [...byKey.values()].
|
|
752
|
+
return [...byKey.values()].toSorted((a, b) => a.binding.sourceKey.localeCompare(b.binding.sourceKey));
|
|
754
753
|
}
|
|
755
754
|
function generateSourcesType(entries) {
|
|
756
755
|
const sorted = organize(entries);
|
|
757
756
|
if (sorted.length === 0) {
|
|
758
757
|
return "";
|
|
759
758
|
}
|
|
760
|
-
const lines = [];
|
|
761
|
-
lines.push("export type Sources = {");
|
|
759
|
+
const lines = ["export type Sources = {"];
|
|
762
760
|
for (const { fragmentName, binding } of sorted) {
|
|
763
761
|
const type = `SourceDef<'${binding.sourceKey}', '${binding.cardinality}', ${fragmentName}Fragment>`;
|
|
764
762
|
lines.push(` ${binding.sourceKey}: ${type};`);
|
|
@@ -771,16 +769,9 @@ function generateSourcesConst(entries) {
|
|
|
771
769
|
if (sorted.length === 0) {
|
|
772
770
|
return "";
|
|
773
771
|
}
|
|
774
|
-
const lines = [];
|
|
775
|
-
lines.push("const sources: Sources = {");
|
|
772
|
+
const lines = ["const sources: Sources = {"];
|
|
776
773
|
for (const { fragmentName, binding, fragmentExpression } of sorted) {
|
|
777
|
-
lines.push(` ${binding.sourceKey}: {`);
|
|
778
|
-
lines.push(` document: ${fragmentExpression},`);
|
|
779
|
-
lines.push(` fragment: '${fragmentName}',`);
|
|
780
|
-
lines.push(` subscription: '${binding.fieldName}',`);
|
|
781
|
-
lines.push(` __model: '${binding.sourceKey}',`);
|
|
782
|
-
lines.push(` __cardinality: '${binding.cardinality}',`);
|
|
783
|
-
lines.push(` },`);
|
|
774
|
+
lines.push(` ${binding.sourceKey}: {`, ` document: ${fragmentExpression},`, ` fragment: '${fragmentName}',`, ` subscription: '${binding.fieldName}',`, ` __model: '${binding.sourceKey}',`, ` __cardinality: '${binding.cardinality}',`, ` },`);
|
|
784
775
|
}
|
|
785
776
|
lines.push("};");
|
|
786
777
|
return lines.join("\n");
|
|
@@ -818,6 +809,9 @@ function getNamedTypeName(type) {
|
|
|
818
809
|
var FLAT_REF_SELECTIONS = parse(
|
|
819
810
|
`{ __typename ... on ${FLAT_COMPONENT_TYPE} { _flatId } }`
|
|
820
811
|
).definitions.flatMap((d) => "selectionSet" in d ? [d.selectionSet] : [])[0];
|
|
812
|
+
var FLAT_REF_ONLY_SELECTIONS = parse(
|
|
813
|
+
`{ ... on ${FLAT_COMPONENT_TYPE} { _flatId } }`
|
|
814
|
+
).definitions.flatMap((d) => "selectionSet" in d ? [d.selectionSet] : [])[0];
|
|
821
815
|
function hasExistingFlatSpread(selectionSet) {
|
|
822
816
|
return selectionSet.selections.some(
|
|
823
817
|
(s) => s.kind === Kind3.INLINE_FRAGMENT && s.typeCondition?.name.value === FLAT_COMPONENT_TYPE
|
|
@@ -846,11 +840,11 @@ function transformSelectionSet(schema, selectionSet, parentTypeName, fragments,
|
|
|
846
840
|
return;
|
|
847
841
|
}
|
|
848
842
|
const transformed = transformSelectionSet(schema, node.selectionSet, typeName, fragments, refsOnly);
|
|
849
|
-
if (transformed
|
|
843
|
+
if (transformed === node.selectionSet) {
|
|
844
|
+
newSelections.push(node);
|
|
845
|
+
} else {
|
|
850
846
|
changed = true;
|
|
851
847
|
newSelections.push({ ...node, selectionSet: transformed });
|
|
852
|
-
} else {
|
|
853
|
-
newSelections.push(node);
|
|
854
848
|
}
|
|
855
849
|
}
|
|
856
850
|
for (const selection of selectionSet.selections) {
|
|
@@ -869,7 +863,7 @@ function transformSelectionSet(schema, selectionSet, parentTypeName, fragments,
|
|
|
869
863
|
continue;
|
|
870
864
|
}
|
|
871
865
|
changed = true;
|
|
872
|
-
newSelections.push({ ...selection, selectionSet:
|
|
866
|
+
newSelections.push({ ...selection, selectionSet: FLAT_REF_ONLY_SELECTIONS });
|
|
873
867
|
if (!refsOnly) {
|
|
874
868
|
newSelections.push({
|
|
875
869
|
kind: Kind3.FIELD,
|
|
@@ -1093,12 +1087,13 @@ async function main() {
|
|
|
1093
1087
|
rmSync(resolve2(cwd, project.output), { force: true });
|
|
1094
1088
|
const schema = await introspectSchema(project.url, project.project, project.token);
|
|
1095
1089
|
const patterns = Array.isArray(project.documents) ? project.documents : [project.documents];
|
|
1096
|
-
const
|
|
1090
|
+
const resolvedPaths = await Promise.all(patterns.map((p) => Array.fromAsync(glob(p, { cwd }))));
|
|
1091
|
+
const documentPaths = resolvedPaths.flat().toSorted();
|
|
1097
1092
|
if (documentPaths.length === 0) {
|
|
1098
1093
|
console.warn(` ${yellow("\u26A0")} no documents found matching "${project.documents}"`);
|
|
1099
1094
|
continue;
|
|
1100
1095
|
}
|
|
1101
|
-
const documents = documentPaths.map((p) => parse2(new Source(readFileSync(resolve2(cwd, p), "
|
|
1096
|
+
const documents = documentPaths.map((p) => parse2(new Source(readFileSync(resolve2(cwd, p), "utf8"), p)));
|
|
1102
1097
|
const fragments = collectFragments(documents);
|
|
1103
1098
|
const operations = collectOperations(documents);
|
|
1104
1099
|
validateDocuments(schema, fragments, operations);
|
|
@@ -1175,11 +1170,7 @@ async function main() {
|
|
|
1175
1170
|
});
|
|
1176
1171
|
}
|
|
1177
1172
|
const entityModels = extractEntityModels(schema);
|
|
1178
|
-
const parts = [];
|
|
1179
|
-
parts.push("/* eslint-disable */");
|
|
1180
|
-
parts.push("// @ts-nocheck");
|
|
1181
|
-
parts.push("/* this file is generated by contello-client \u2014 do not edit */");
|
|
1182
|
-
parts.push("");
|
|
1173
|
+
const parts = ["/* eslint-disable */", "// @ts-nocheck", "/* this file is generated by contello-client \u2014 do not edit */", ""];
|
|
1183
1174
|
const typeMap = schema.getTypeMap();
|
|
1184
1175
|
const clientImports = ["SourceDef"];
|
|
1185
1176
|
for (const scalar of MANAGED_SCALARS) {
|
|
@@ -1187,53 +1178,46 @@ async function main() {
|
|
|
1187
1178
|
clientImports.push(scalar);
|
|
1188
1179
|
}
|
|
1189
1180
|
}
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1181
|
+
parts.push(
|
|
1182
|
+
`import type { ${clientImports.toSorted().join(", ")} } from '@contello/client';`,
|
|
1183
|
+
"",
|
|
1184
|
+
generateSchemaTypes(schema),
|
|
1185
|
+
generateFragmentTypes(schema, fragments),
|
|
1186
|
+
generateOperationTypes(schema, operations, fragments)
|
|
1187
|
+
);
|
|
1196
1188
|
if (transformedFragments.size > 0) {
|
|
1197
|
-
parts.push(generateFragmentSchemas(transformedFragments));
|
|
1198
|
-
parts.push("");
|
|
1189
|
+
parts.push(generateFragmentSchemas(transformedFragments), "");
|
|
1199
1190
|
}
|
|
1200
|
-
for (
|
|
1201
|
-
const camelName = uncapitalize(
|
|
1191
|
+
for (const [i, operation] of operations.entries()) {
|
|
1192
|
+
const camelName = uncapitalize(operation.name.value);
|
|
1202
1193
|
const docExpr = operationDocumentExpression(transformedOperations[i], transformedFragments);
|
|
1203
1194
|
parts.push(`export const ${camelName}Document = ${docExpr};`);
|
|
1204
1195
|
}
|
|
1205
1196
|
if (operations.length > 0) {
|
|
1206
1197
|
parts.push("");
|
|
1207
1198
|
}
|
|
1208
|
-
parts.push(generateOperationsType(operations));
|
|
1209
|
-
parts.push("");
|
|
1199
|
+
parts.push(generateOperationsType(operations), "");
|
|
1210
1200
|
const sourcesTypeSrc = generateSourcesType(sourceEntries);
|
|
1211
1201
|
if (sourcesTypeSrc) {
|
|
1212
|
-
parts.push(sourcesTypeSrc);
|
|
1213
|
-
parts.push("");
|
|
1202
|
+
parts.push(sourcesTypeSrc, "");
|
|
1214
1203
|
}
|
|
1215
|
-
parts.push(generateOperationsConst(operations));
|
|
1216
|
-
parts.push("");
|
|
1204
|
+
parts.push(generateOperationsConst(operations), "");
|
|
1217
1205
|
const sourcesConstSrc = generateSourcesConst(sourceEntries);
|
|
1218
1206
|
if (sourcesConstSrc) {
|
|
1219
|
-
parts.push(sourcesConstSrc);
|
|
1220
|
-
parts.push("");
|
|
1207
|
+
parts.push(sourcesConstSrc, "");
|
|
1221
1208
|
}
|
|
1222
1209
|
if (entityModels.size > 0) {
|
|
1223
|
-
const sorted = [...entityModels.entries()].
|
|
1210
|
+
const sorted = [...entityModels.entries()].toSorted(([a], [b]) => a.localeCompare(b));
|
|
1224
1211
|
parts.push("const models = {");
|
|
1225
1212
|
for (const [model, typeName] of sorted) {
|
|
1226
1213
|
parts.push(` ${model}: '${typeName}',`);
|
|
1227
1214
|
}
|
|
1228
|
-
parts.push("} as const;");
|
|
1229
|
-
parts.push("");
|
|
1215
|
+
parts.push("} as const;", "");
|
|
1230
1216
|
} else {
|
|
1231
|
-
parts.push("const models = {} as const;");
|
|
1232
|
-
parts.push("");
|
|
1217
|
+
parts.push("const models = {} as const;", "");
|
|
1233
1218
|
}
|
|
1234
1219
|
const sourcesPart = sourcesConstSrc ? ", sources" : ", sources: {}";
|
|
1235
|
-
parts.push(`export const schema = { operations${sourcesPart}, models }
|
|
1236
|
-
parts.push("export type Schema = typeof schema;");
|
|
1220
|
+
parts.push(`export const schema = { operations${sourcesPart}, models };`, "export type Schema = typeof schema;");
|
|
1237
1221
|
let output = parts.join("\n");
|
|
1238
1222
|
if (project.namespace) {
|
|
1239
1223
|
const indented = output.split("\n").map((line) => line ? ` ${line}` : "").join("\n");
|
|
@@ -1244,14 +1228,14 @@ ${indented}
|
|
|
1244
1228
|
}
|
|
1245
1229
|
const outputPath = resolve2(cwd, project.output);
|
|
1246
1230
|
mkdirSync(dirname(outputPath), { recursive: true });
|
|
1247
|
-
writeFileSync(outputPath, output, "
|
|
1231
|
+
writeFileSync(outputPath, output, "utf8");
|
|
1248
1232
|
const elapsed = Math.round(performance.now() - start);
|
|
1249
1233
|
console.log(` ${green("\u2713")} ${relative(cwd, outputPath)} ${dim(`${elapsed}ms`)}`);
|
|
1250
1234
|
console.log(` ${dim("operations")} ${operations.length} ${dim("fragments")} ${fragments.size}`);
|
|
1251
1235
|
console.log("");
|
|
1252
1236
|
}
|
|
1253
1237
|
}
|
|
1254
|
-
main().catch((
|
|
1255
|
-
console.error(
|
|
1238
|
+
main().catch((error) => {
|
|
1239
|
+
console.error(error);
|
|
1256
1240
|
process.exit(1);
|
|
1257
1241
|
});
|