@contello/client 1.2.1 → 1.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
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
var command = process.argv[2];
|
|
5
5
|
switch (command) {
|
|
6
6
|
case "generate":
|
|
7
|
-
await import("./generate-
|
|
7
|
+
await import("./generate-PGYNYGNX.js");
|
|
8
8
|
break;
|
|
9
9
|
default:
|
|
10
10
|
console.error(command ? `unknown command: ${command}` : "usage: contello-client <command>");
|
|
@@ -256,9 +256,10 @@ function unwrapType(type) {
|
|
|
256
256
|
}
|
|
257
257
|
return { namedType: current, isList, isNonNull };
|
|
258
258
|
}
|
|
259
|
-
function resolveSelectionSet(schema, selectionSet, parentType, fragments, indent
|
|
259
|
+
function resolveSelectionSet(schema, selectionSet, parentType, fragments, indent) {
|
|
260
260
|
const fields = [];
|
|
261
261
|
const inlineUnions = [];
|
|
262
|
+
const fragmentRefs = [];
|
|
262
263
|
let hasTypenameField = false;
|
|
263
264
|
for (const selection of selectionSet.selections) {
|
|
264
265
|
if (selection.kind === Kind2.FIELD) {
|
|
@@ -274,7 +275,7 @@ function resolveSelectionSet(schema, selectionSet, parentType, fragments, indent
|
|
|
274
275
|
const { namedType, isList, isNonNull } = unwrapType(schemaField.type);
|
|
275
276
|
let tsType;
|
|
276
277
|
if (selection.selectionSet && isCompositeType(namedType)) {
|
|
277
|
-
tsType = resolveSelectionSetType(schema, selection.selectionSet, namedType, fragments, `${indent}
|
|
278
|
+
tsType = resolveSelectionSetType(schema, selection.selectionSet, namedType, fragments, `${indent} `);
|
|
278
279
|
} else if (isScalarType(namedType)) {
|
|
279
280
|
tsType = resolveScalarType(namedType.name);
|
|
280
281
|
} else if (isEnumType(namedType)) {
|
|
@@ -283,7 +284,7 @@ function resolveSelectionSet(schema, selectionSet, parentType, fragments, indent
|
|
|
283
284
|
tsType = namedType.toString();
|
|
284
285
|
}
|
|
285
286
|
if (isList) {
|
|
286
|
-
const wrapped = tsType
|
|
287
|
+
const wrapped = /[ |&]/.test(tsType) ? `(${tsType})` : tsType;
|
|
287
288
|
tsType = `${wrapped}[]`;
|
|
288
289
|
}
|
|
289
290
|
if (isNonNull) {
|
|
@@ -300,24 +301,11 @@ function resolveSelectionSet(schema, selectionSet, parentType, fragments, indent
|
|
|
300
301
|
}
|
|
301
302
|
if (isCompositeType(conditionalType)) {
|
|
302
303
|
if (isInterfaceType(conditionalType)) {
|
|
303
|
-
const inner = resolveSelectionSet(
|
|
304
|
-
schema,
|
|
305
|
-
selection.selectionSet,
|
|
306
|
-
conditionalType,
|
|
307
|
-
fragments,
|
|
308
|
-
indent,
|
|
309
|
-
visited
|
|
310
|
-
);
|
|
304
|
+
const inner = resolveSelectionSet(schema, selection.selectionSet, conditionalType, fragments, indent);
|
|
311
305
|
fields.push(...inner.fields);
|
|
306
|
+
fragmentRefs.push(...inner.fragmentRefs);
|
|
312
307
|
} else if (isObjectType(conditionalType)) {
|
|
313
|
-
const nested = resolveSelectionSetType(
|
|
314
|
-
schema,
|
|
315
|
-
selection.selectionSet,
|
|
316
|
-
conditionalType,
|
|
317
|
-
fragments,
|
|
318
|
-
indent,
|
|
319
|
-
visited
|
|
320
|
-
);
|
|
308
|
+
const nested = resolveSelectionSetType(schema, selection.selectionSet, conditionalType, fragments, indent);
|
|
321
309
|
if (nested.startsWith("{")) {
|
|
322
310
|
const model = isContelloModel(schema, conditionalType.name) ? deriveModelName(conditionalType.name) : void 0;
|
|
323
311
|
const discriminant = model ? `${indent} __typename: '${conditionalType.name}';
|
|
@@ -326,27 +314,23 @@ ${indent} __model: '${model}';` : `${indent} __typename: '${conditionalType.na
|
|
|
326
314
|
${discriminant}
|
|
327
315
|
${nested.slice(2)}`);
|
|
328
316
|
} else {
|
|
329
|
-
|
|
317
|
+
const model = isContelloModel(schema, conditionalType.name) ? deriveModelName(conditionalType.name) : void 0;
|
|
318
|
+
const discriminant = model ? `{
|
|
319
|
+
${indent} __typename: '${conditionalType.name}';
|
|
320
|
+
${indent} __model: '${model}';
|
|
321
|
+
${indent}}` : `{
|
|
322
|
+
${indent} __typename: '${conditionalType.name}';
|
|
323
|
+
${indent}}`;
|
|
324
|
+
inlineUnions.push(`${discriminant} & ${nested}`);
|
|
330
325
|
}
|
|
331
326
|
} else {
|
|
332
|
-
const nested = resolveSelectionSetType(
|
|
333
|
-
schema,
|
|
334
|
-
selection.selectionSet,
|
|
335
|
-
conditionalType,
|
|
336
|
-
fragments,
|
|
337
|
-
indent,
|
|
338
|
-
visited
|
|
339
|
-
);
|
|
327
|
+
const nested = resolveSelectionSetType(schema, selection.selectionSet, conditionalType, fragments, indent);
|
|
340
328
|
inlineUnions.push(nested);
|
|
341
329
|
}
|
|
342
330
|
}
|
|
343
331
|
}
|
|
344
332
|
} else if (selection.kind === Kind2.FRAGMENT_SPREAD) {
|
|
345
333
|
const fragmentName = selection.name.value;
|
|
346
|
-
if (visited.has(fragmentName)) {
|
|
347
|
-
inlineUnions.push(`${fragmentName}Fragment`);
|
|
348
|
-
continue;
|
|
349
|
-
}
|
|
350
334
|
const fragment = fragments.get(fragmentName);
|
|
351
335
|
if (fragment?.selectionSet) {
|
|
352
336
|
const fragmentType = schema.getType(fragment.typeCondition.name.value);
|
|
@@ -356,50 +340,42 @@ ${nested.slice(2)}`);
|
|
|
356
340
|
`fragment "${fragmentName}" (on ${fragmentType.name}) cannot be spread on type "${parentType.name}"`
|
|
357
341
|
);
|
|
358
342
|
}
|
|
359
|
-
|
|
360
|
-
nextVisited.add(fragmentName);
|
|
361
|
-
const fragmentResult = resolveSelectionSet(
|
|
362
|
-
schema,
|
|
363
|
-
fragment.selectionSet,
|
|
364
|
-
fragmentType,
|
|
365
|
-
fragments,
|
|
366
|
-
indent,
|
|
367
|
-
nextVisited
|
|
368
|
-
);
|
|
369
|
-
fields.push(...fragmentResult.fields);
|
|
370
|
-
inlineUnions.push(...fragmentResult.inlineUnions);
|
|
371
|
-
if (fragmentResult.hasTypenameField) {
|
|
372
|
-
hasTypenameField = true;
|
|
373
|
-
}
|
|
343
|
+
fragmentRefs.push(`${fragmentName}Fragment`);
|
|
374
344
|
}
|
|
375
345
|
}
|
|
376
346
|
}
|
|
377
347
|
}
|
|
378
|
-
return { fields, inlineUnions, hasTypenameField };
|
|
348
|
+
return { fields, inlineUnions, fragmentRefs, hasTypenameField };
|
|
379
349
|
}
|
|
380
|
-
function resolveSelectionSetType(schema, selectionSet, parentType, fragments, indent
|
|
381
|
-
const { fields, inlineUnions, hasTypenameField } = resolveSelectionSet(
|
|
350
|
+
function resolveSelectionSetType(schema, selectionSet, parentType, fragments, indent) {
|
|
351
|
+
const { fields, inlineUnions, fragmentRefs, hasTypenameField } = resolveSelectionSet(
|
|
382
352
|
schema,
|
|
383
353
|
selectionSet,
|
|
384
354
|
parentType,
|
|
385
355
|
fragments,
|
|
386
|
-
indent
|
|
387
|
-
visited
|
|
356
|
+
indent
|
|
388
357
|
);
|
|
389
358
|
if (hasTypenameField && inlineUnions.length === 0) {
|
|
390
359
|
const typenameField = isObjectType(parentType) ? `${indent} __typename: '${parentType.name}';` : `${indent} __typename: string;`;
|
|
391
360
|
fields.unshift(typenameField);
|
|
392
361
|
}
|
|
393
|
-
if (
|
|
362
|
+
if (fields.length === 0 && fragmentRefs.length === 0 && inlineUnions.length > 0) {
|
|
394
363
|
return inlineUnions.join(" | ");
|
|
395
364
|
}
|
|
396
|
-
const
|
|
365
|
+
const parts = [];
|
|
366
|
+
if (fields.length > 0) {
|
|
367
|
+
parts.push(`{
|
|
397
368
|
${fields.join("\n")}
|
|
398
|
-
${indent}}`
|
|
369
|
+
${indent}}`);
|
|
370
|
+
}
|
|
371
|
+
parts.push(...fragmentRefs);
|
|
399
372
|
if (inlineUnions.length > 0) {
|
|
400
|
-
|
|
373
|
+
parts.push(`(${inlineUnions.join(" | ")})`);
|
|
374
|
+
}
|
|
375
|
+
if (parts.length === 0) {
|
|
376
|
+
return "{}";
|
|
401
377
|
}
|
|
402
|
-
return
|
|
378
|
+
return parts.join(" & ");
|
|
403
379
|
}
|
|
404
380
|
function generateVariablesType(schema, operation) {
|
|
405
381
|
const vars = operation.variableDefinitions ?? [];
|