@contello/client 1.2.0 → 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-U3UYC2OW.js");
7
+ await import("./generate-PGYNYGNX.js");
8
8
  break;
9
9
  default:
10
10
  console.error(command ? `unknown command: ${command}` : "usage: contello-client <command>");
@@ -40,10 +40,15 @@ async function loadConfig(cwd) {
40
40
  // src/generate/documents.ts
41
41
  import {
42
42
  Kind,
43
+ NoFragmentCyclesRule,
44
+ NoUnusedFragmentsRule,
43
45
  getLocation,
44
46
  print,
47
+ specifiedRules,
45
48
  validate
46
49
  } from "graphql";
50
+ var SUPPRESSED_RULE_NAMES = /* @__PURE__ */ new Set([NoFragmentCyclesRule.name, NoUnusedFragmentsRule.name]);
51
+ var VALIDATION_RULES = specifiedRules.filter((rule) => !SUPPRESSED_RULE_NAMES.has(rule.name));
47
52
  function collectFragments(documents) {
48
53
  const fragments = /* @__PURE__ */ new Map();
49
54
  for (const doc of documents) {
@@ -126,7 +131,7 @@ function validateDocuments(schema, fragments, operations) {
126
131
  kind: Kind.DOCUMENT,
127
132
  definitions: [...fragments.values(), ...operations]
128
133
  };
129
- const errors = validate(schema, combined);
134
+ const errors = validate(schema, combined, VALIDATION_RULES);
130
135
  if (errors.length === 0) {
131
136
  return;
132
137
  }
@@ -251,9 +256,10 @@ function unwrapType(type) {
251
256
  }
252
257
  return { namedType: current, isList, isNonNull };
253
258
  }
254
- function resolveSelectionSet(schema, selectionSet, parentType, fragments, indent, visited) {
259
+ function resolveSelectionSet(schema, selectionSet, parentType, fragments, indent) {
255
260
  const fields = [];
256
261
  const inlineUnions = [];
262
+ const fragmentRefs = [];
257
263
  let hasTypenameField = false;
258
264
  for (const selection of selectionSet.selections) {
259
265
  if (selection.kind === Kind2.FIELD) {
@@ -269,7 +275,7 @@ function resolveSelectionSet(schema, selectionSet, parentType, fragments, indent
269
275
  const { namedType, isList, isNonNull } = unwrapType(schemaField.type);
270
276
  let tsType;
271
277
  if (selection.selectionSet && isCompositeType(namedType)) {
272
- tsType = resolveSelectionSetType(schema, selection.selectionSet, namedType, fragments, `${indent} `, visited);
278
+ tsType = resolveSelectionSetType(schema, selection.selectionSet, namedType, fragments, `${indent} `);
273
279
  } else if (isScalarType(namedType)) {
274
280
  tsType = resolveScalarType(namedType.name);
275
281
  } else if (isEnumType(namedType)) {
@@ -278,7 +284,7 @@ function resolveSelectionSet(schema, selectionSet, parentType, fragments, indent
278
284
  tsType = namedType.toString();
279
285
  }
280
286
  if (isList) {
281
- const wrapped = tsType.includes(" | ") ? `(${tsType})` : tsType;
287
+ const wrapped = /[ |&]/.test(tsType) ? `(${tsType})` : tsType;
282
288
  tsType = `${wrapped}[]`;
283
289
  }
284
290
  if (isNonNull) {
@@ -295,24 +301,11 @@ function resolveSelectionSet(schema, selectionSet, parentType, fragments, indent
295
301
  }
296
302
  if (isCompositeType(conditionalType)) {
297
303
  if (isInterfaceType(conditionalType)) {
298
- const inner = resolveSelectionSet(
299
- schema,
300
- selection.selectionSet,
301
- conditionalType,
302
- fragments,
303
- indent,
304
- visited
305
- );
304
+ const inner = resolveSelectionSet(schema, selection.selectionSet, conditionalType, fragments, indent);
306
305
  fields.push(...inner.fields);
306
+ fragmentRefs.push(...inner.fragmentRefs);
307
307
  } else if (isObjectType(conditionalType)) {
308
- const nested = resolveSelectionSetType(
309
- schema,
310
- selection.selectionSet,
311
- conditionalType,
312
- fragments,
313
- indent,
314
- visited
315
- );
308
+ const nested = resolveSelectionSetType(schema, selection.selectionSet, conditionalType, fragments, indent);
316
309
  if (nested.startsWith("{")) {
317
310
  const model = isContelloModel(schema, conditionalType.name) ? deriveModelName(conditionalType.name) : void 0;
318
311
  const discriminant = model ? `${indent} __typename: '${conditionalType.name}';
@@ -321,27 +314,23 @@ ${indent} __model: '${model}';` : `${indent} __typename: '${conditionalType.na
321
314
  ${discriminant}
322
315
  ${nested.slice(2)}`);
323
316
  } else {
324
- inlineUnions.push(nested);
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}`);
325
325
  }
326
326
  } else {
327
- const nested = resolveSelectionSetType(
328
- schema,
329
- selection.selectionSet,
330
- conditionalType,
331
- fragments,
332
- indent,
333
- visited
334
- );
327
+ const nested = resolveSelectionSetType(schema, selection.selectionSet, conditionalType, fragments, indent);
335
328
  inlineUnions.push(nested);
336
329
  }
337
330
  }
338
331
  }
339
332
  } else if (selection.kind === Kind2.FRAGMENT_SPREAD) {
340
333
  const fragmentName = selection.name.value;
341
- if (visited.has(fragmentName)) {
342
- inlineUnions.push(`${fragmentName}Fragment`);
343
- continue;
344
- }
345
334
  const fragment = fragments.get(fragmentName);
346
335
  if (fragment?.selectionSet) {
347
336
  const fragmentType = schema.getType(fragment.typeCondition.name.value);
@@ -351,50 +340,42 @@ ${nested.slice(2)}`);
351
340
  `fragment "${fragmentName}" (on ${fragmentType.name}) cannot be spread on type "${parentType.name}"`
352
341
  );
353
342
  }
354
- const nextVisited = new Set(visited);
355
- nextVisited.add(fragmentName);
356
- const fragmentResult = resolveSelectionSet(
357
- schema,
358
- fragment.selectionSet,
359
- fragmentType,
360
- fragments,
361
- indent,
362
- nextVisited
363
- );
364
- fields.push(...fragmentResult.fields);
365
- inlineUnions.push(...fragmentResult.inlineUnions);
366
- if (fragmentResult.hasTypenameField) {
367
- hasTypenameField = true;
368
- }
343
+ fragmentRefs.push(`${fragmentName}Fragment`);
369
344
  }
370
345
  }
371
346
  }
372
347
  }
373
- return { fields, inlineUnions, hasTypenameField };
348
+ return { fields, inlineUnions, fragmentRefs, hasTypenameField };
374
349
  }
375
- function resolveSelectionSetType(schema, selectionSet, parentType, fragments, indent, visited = /* @__PURE__ */ new Set()) {
376
- const { fields, inlineUnions, hasTypenameField } = resolveSelectionSet(
350
+ function resolveSelectionSetType(schema, selectionSet, parentType, fragments, indent) {
351
+ const { fields, inlineUnions, fragmentRefs, hasTypenameField } = resolveSelectionSet(
377
352
  schema,
378
353
  selectionSet,
379
354
  parentType,
380
355
  fragments,
381
- indent,
382
- visited
356
+ indent
383
357
  );
384
358
  if (hasTypenameField && inlineUnions.length === 0) {
385
359
  const typenameField = isObjectType(parentType) ? `${indent} __typename: '${parentType.name}';` : `${indent} __typename: string;`;
386
360
  fields.unshift(typenameField);
387
361
  }
388
- if (inlineUnions.length > 0 && fields.length === 0) {
362
+ if (fields.length === 0 && fragmentRefs.length === 0 && inlineUnions.length > 0) {
389
363
  return inlineUnions.join(" | ");
390
364
  }
391
- const baseType = fields.length > 0 ? `{
365
+ const parts = [];
366
+ if (fields.length > 0) {
367
+ parts.push(`{
392
368
  ${fields.join("\n")}
393
- ${indent}}` : "{}";
369
+ ${indent}}`);
370
+ }
371
+ parts.push(...fragmentRefs);
394
372
  if (inlineUnions.length > 0) {
395
- return `${baseType} & (${inlineUnions.join(" | ")})`;
373
+ parts.push(`(${inlineUnions.join(" | ")})`);
374
+ }
375
+ if (parts.length === 0) {
376
+ return "{}";
396
377
  }
397
- return baseType;
378
+ return parts.join(" & ");
398
379
  }
399
380
  function generateVariablesType(schema, operation) {
400
381
  const vars = operation.variableDefinitions ?? [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contello/client",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "description": "GraphQL client for Contello CMS with WebSocket transport, connection pooling, and built-in codegen",
5
5
  "type": "module",
6
6
  "license": "MIT",