@contello/client 1.2.2 → 2.1.0

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-PGYNYGNX.js");
7
+ await import("./generate-FPYYN7UV.js");
8
8
  break;
9
9
  default:
10
10
  console.error(command ? `unknown command: ${command}` : "usage: contello-client <command>");
@@ -4,7 +4,7 @@
4
4
  import { mkdirSync, readFileSync, rmSync, writeFileSync } from "fs";
5
5
  import { glob } from "fs/promises";
6
6
  import { dirname, relative, resolve as resolve2 } from "path";
7
- import { Source, parse as parse2 } from "graphql";
7
+ import { Kind as Kind5, Source, parse as parse2, visit } from "graphql";
8
8
 
9
9
  // src/generate/config.ts
10
10
  import { resolve } from "path";
@@ -103,7 +103,7 @@ function collectUsedFragments(node, allFragments, collected) {
103
103
  function sortFragmentsByDependency(fragments) {
104
104
  const sorted = [];
105
105
  const visited = /* @__PURE__ */ new Set();
106
- function visit(name) {
106
+ function visit2(name) {
107
107
  if (visited.has(name)) {
108
108
  return;
109
109
  }
@@ -116,13 +116,13 @@ function sortFragmentsByDependency(fragments) {
116
116
  collectUsedFragments(fragment, fragments, deps);
117
117
  for (const [depName] of deps) {
118
118
  if (depName !== name) {
119
- visit(depName);
119
+ visit2(depName);
120
120
  }
121
121
  }
122
122
  sorted.push(fragment);
123
123
  }
124
124
  for (const [name] of fragments) {
125
- visit(name);
125
+ visit2(name);
126
126
  }
127
127
  return sorted;
128
128
  }
@@ -152,12 +152,31 @@ ${locations.join("\n")}` : ` ${err.message}`;
152
152
  ${formatted.join("\n\n")}
153
153
  `);
154
154
  }
155
- function generateDocumentString(operation, allFragments) {
155
+ 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 parts = [...sortedFragments.map((f) => print(f)), print(operation)];
160
- return parts.join("\n");
159
+ const parts = sortedFragments.map((f) => `\${${f.name.value}FragmentSchema}`);
160
+ parts.push(print(operation));
161
+ return `\`${parts.join("\n")}\``;
162
+ }
163
+ function fragmentBundleExpression(fragment, allFragments) {
164
+ const usedFragments = /* @__PURE__ */ new Map();
165
+ collectUsedFragments(fragment, allFragments, usedFragments);
166
+ usedFragments.set(fragment.name.value, fragment);
167
+ const sortedFragments = sortFragmentsByDependency(usedFragments);
168
+ if (sortedFragments.length === 1) {
169
+ return `${sortedFragments[0].name.value}FragmentSchema`;
170
+ }
171
+ return `\`${sortedFragments.map((f) => `\${${f.name.value}FragmentSchema}`).join("\n")}\``;
172
+ }
173
+ function generateFragmentSchemas(fragments) {
174
+ const sorted = [...fragments.values()].sort((a, b) => a.name.value.localeCompare(b.name.value));
175
+ const lines = [];
176
+ for (const fragment of sorted) {
177
+ lines.push(`const ${fragment.name.value}FragmentSchema = \`${print(fragment)}\`;`);
178
+ }
179
+ return lines.join("\n");
161
180
  }
162
181
 
163
182
  // src/generate/introspect.ts
@@ -203,8 +222,11 @@ var SCALAR_MAP = {
203
222
  Float: "number",
204
223
  Boolean: "boolean",
205
224
  ID: "string",
206
- DateTime: "string"
225
+ DateTime: "string",
226
+ LocalDateTime: "LocalDateTime",
227
+ LocalDate: "LocalDate"
207
228
  };
229
+ var MANAGED_SCALARS = ["LocalDateTime", "LocalDate"];
208
230
  var DEFAULT_CUSTOM_SCALAR = "unknown";
209
231
  function resolveScalarType(name) {
210
232
  return SCALAR_MAP[name] ?? DEFAULT_CUSTOM_SCALAR;
@@ -453,7 +475,7 @@ function generateOperationTypes(schema, operations, fragments) {
453
475
  }
454
476
 
455
477
  // src/generate/operations.ts
456
- function generateOperationsObject(operations) {
478
+ function generateOperationsType(operations) {
457
479
  const lines = [];
458
480
  lines.push("export type Operations = {");
459
481
  for (const op of operations) {
@@ -470,13 +492,16 @@ function generateOperationsObject(operations) {
470
492
  lines.push(" };");
471
493
  }
472
494
  lines.push("};");
473
- lines.push("");
474
- lines.push("export const operations: Operations = {");
495
+ return lines.join("\n");
496
+ }
497
+ function generateOperationsConst(operations) {
498
+ const lines = [];
499
+ lines.push("const operations: Operations = {");
475
500
  for (const op of operations) {
476
501
  const camelName = uncapitalize(op.name.value);
477
502
  lines.push(` ${camelName}: { document: ${camelName}Document, kind: '${op.operation}' },`);
478
503
  }
479
- lines.push("} as Operations;");
504
+ lines.push("};");
480
505
  return lines.join("\n");
481
506
  }
482
507
 
@@ -616,17 +641,148 @@ function generateSchemaTypes(schema) {
616
641
  if (unions.length > 0) {
617
642
  lines.push("");
618
643
  }
619
- if (entityModelNames.size > 0) {
620
- const sorted = [...entityModelNames.entries()].sort(([a], [b]) => a.localeCompare(b));
621
- lines.push("export const models = {");
622
- for (const [typeName, model] of sorted) {
623
- lines.push(` ${model}: '${typeName}',`);
644
+ return lines.join("\n");
645
+ }
646
+ function extractEntityModels(schema) {
647
+ const map = /* @__PURE__ */ new Map();
648
+ const union = schema.getTypeMap()["ContelloEntity"];
649
+ if (!union || !isUnionType(union)) {
650
+ return map;
651
+ }
652
+ for (const member of union.getTypes()) {
653
+ const model = deriveModelName(member.name);
654
+ if (model) {
655
+ map.set(model, member.name);
624
656
  }
625
- lines.push("} as const;");
626
- lines.push("");
627
- lines.push("export type ModelType = keyof typeof models;");
628
- lines.push("");
629
657
  }
658
+ return map;
659
+ }
660
+
661
+ // src/generate/sources.ts
662
+ import {
663
+ isListType as isListType3,
664
+ isNonNullType as isNonNullType3,
665
+ isObjectType as isObjectType3
666
+ } from "graphql";
667
+ var BUILT_IN_SOURCES = [
668
+ { typeName: "ContelloRoute", cardinality: "route", fieldName: "contelloRoutesBatch" },
669
+ { typeName: "ContelloAsset", cardinality: "asset", fieldName: "contelloAssetsBatch" },
670
+ { typeName: "ContelloI18nMessage", cardinality: "i18nMessage", fieldName: "contelloI18nMessagesBatch" }
671
+ ];
672
+ function indexEntitySources(schema) {
673
+ const result = /* @__PURE__ */ new Map();
674
+ const subscription = schema.getSubscriptionType();
675
+ if (!subscription) {
676
+ return result;
677
+ }
678
+ const singletonCandidates = /* @__PURE__ */ new Map();
679
+ for (const field of Object.values(subscription.getFields())) {
680
+ const { entityType, isList } = unwrapEntity(field.type);
681
+ if (!entityType || !isContelloModel(schema, entityType.name)) {
682
+ continue;
683
+ }
684
+ const model = deriveModelName(entityType.name);
685
+ if (!model) {
686
+ continue;
687
+ }
688
+ if (isList) {
689
+ result.set(entityType.name, { cardinality: "entity", fieldName: field.name, model });
690
+ } else if (!singletonCandidates.has(entityType.name)) {
691
+ singletonCandidates.set(entityType.name, { fieldName: field.name });
692
+ }
693
+ }
694
+ for (const [typeName, { fieldName }] of singletonCandidates) {
695
+ if (result.has(typeName)) {
696
+ continue;
697
+ }
698
+ const model = deriveModelName(typeName);
699
+ if (!model) {
700
+ continue;
701
+ }
702
+ result.set(typeName, { cardinality: "singleton", fieldName, model });
703
+ }
704
+ return result;
705
+ }
706
+ function indexBuiltInSources(schema) {
707
+ const result = /* @__PURE__ */ new Map();
708
+ const subscription = schema.getSubscriptionType();
709
+ if (!subscription) {
710
+ return result;
711
+ }
712
+ const fields = subscription.getFields();
713
+ for (const builtIn of BUILT_IN_SOURCES) {
714
+ if (!schema.getType(builtIn.typeName) || !fields[builtIn.fieldName]) {
715
+ continue;
716
+ }
717
+ result.set(builtIn.typeName, {
718
+ cardinality: builtIn.cardinality,
719
+ fieldName: builtIn.fieldName
720
+ });
721
+ }
722
+ return result;
723
+ }
724
+ function unwrapEntity(type) {
725
+ let current = type;
726
+ let isList = false;
727
+ if (isNonNullType3(current)) {
728
+ current = current.ofType;
729
+ }
730
+ if (isListType3(current)) {
731
+ isList = true;
732
+ current = current.ofType;
733
+ if (isNonNullType3(current)) {
734
+ current = current.ofType;
735
+ }
736
+ }
737
+ return {
738
+ entityType: isObjectType3(current) ? current : void 0,
739
+ isList
740
+ };
741
+ }
742
+ function organize(entries) {
743
+ const byKey = /* @__PURE__ */ new Map();
744
+ for (const entry of entries) {
745
+ const existing = byKey.get(entry.binding.sourceKey);
746
+ if (existing) {
747
+ throw new Error(
748
+ `multiple fragments target the same source key "${entry.binding.sourceKey}": "${existing.fragmentName}" and "${entry.fragmentName}". A source key can have at most one fragment \u2014 merge the fragments or use the raw client for the alternative shape.`
749
+ );
750
+ }
751
+ byKey.set(entry.binding.sourceKey, entry);
752
+ }
753
+ return [...byKey.values()].sort((a, b) => a.binding.sourceKey.localeCompare(b.binding.sourceKey));
754
+ }
755
+ function generateSourcesType(entries) {
756
+ const sorted = organize(entries);
757
+ if (sorted.length === 0) {
758
+ return "";
759
+ }
760
+ const lines = [];
761
+ lines.push("export type Sources = {");
762
+ for (const { fragmentName, binding } of sorted) {
763
+ const type = `SourceDef<'${binding.sourceKey}', '${binding.cardinality}', ${fragmentName}Fragment>`;
764
+ lines.push(` ${binding.sourceKey}: ${type};`);
765
+ }
766
+ lines.push("};");
767
+ return lines.join("\n");
768
+ }
769
+ function generateSourcesConst(entries) {
770
+ const sorted = organize(entries);
771
+ if (sorted.length === 0) {
772
+ return "";
773
+ }
774
+ const lines = [];
775
+ lines.push("const sources: Sources = {");
776
+ 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(` },`);
784
+ }
785
+ lines.push("};");
630
786
  return lines.join("\n");
631
787
  }
632
788
 
@@ -634,9 +790,9 @@ function generateSchemaTypes(schema) {
634
790
  import {
635
791
  Kind as Kind3,
636
792
  isCompositeType as isCompositeType2,
637
- isListType as isListType3,
638
- isNonNullType as isNonNullType3,
639
- isObjectType as isObjectType3,
793
+ isListType as isListType4,
794
+ isNonNullType as isNonNullType4,
795
+ isObjectType as isObjectType4,
640
796
  isUnionType as isUnionType2,
641
797
  parse
642
798
  } from "graphql";
@@ -655,8 +811,8 @@ function isComponentSubtype(schema, typeName) {
655
811
  return union.getTypes().some((t) => t.name === typeName);
656
812
  }
657
813
  function getNamedTypeName(type) {
658
- if (isNonNullType3(type)) return getNamedTypeName(type.ofType);
659
- if (isListType3(type)) return getNamedTypeName(type.ofType);
814
+ if (isNonNullType4(type)) return getNamedTypeName(type.ofType);
815
+ if (isListType4(type)) return getNamedTypeName(type.ofType);
660
816
  return type?.name;
661
817
  }
662
818
  var FLAT_REF_SELECTIONS = parse(
@@ -678,7 +834,7 @@ function addFlatRefSelections(selectionSet) {
678
834
  }
679
835
  function transformSelectionSet(schema, selectionSet, parentTypeName, fragments, refsOnly = false) {
680
836
  const parentType = schema.getType(parentTypeName);
681
- if (!parentType || !isObjectType3(parentType)) {
837
+ if (!parentType || !isObjectType4(parentType)) {
682
838
  return selectionSet;
683
839
  }
684
840
  const parentFields = parentType.getFields();
@@ -705,8 +861,8 @@ function transformSelectionSet(schema, selectionSet, parentTypeName, fragments,
705
861
  newSelections.push(selection);
706
862
  continue;
707
863
  }
708
- const namedTypeName = getNamedTypeName(schemaField.type);
709
- if (namedTypeName && isComponentUnion(schema, namedTypeName) && selection.selectionSet) {
864
+ const namedTypeName2 = getNamedTypeName(schemaField.type);
865
+ if (namedTypeName2 && isComponentUnion(schema, namedTypeName2) && selection.selectionSet) {
710
866
  const flatFieldName = `${FLAT_PREFIX}${fieldName}`;
711
867
  if (!parentFields[flatFieldName]) {
712
868
  newSelections.push(selection);
@@ -721,15 +877,15 @@ function transformSelectionSet(schema, selectionSet, parentTypeName, fragments,
721
877
  selectionSet: addFlatRefSelections(selection.selectionSet)
722
878
  });
723
879
  }
724
- } else if (namedTypeName) {
725
- recurse(selection, namedTypeName);
880
+ } else if (namedTypeName2) {
881
+ recurse(selection, namedTypeName2);
726
882
  } else {
727
883
  newSelections.push(selection);
728
884
  }
729
885
  } else if (selection.kind === Kind3.INLINE_FRAGMENT) {
730
886
  const typeName = selection.typeCondition?.name.value;
731
887
  const type = typeName ? schema.getType(typeName) : void 0;
732
- if (typeName && type && isCompositeType2(type) && isObjectType3(type)) {
888
+ if (typeName && type && isCompositeType2(type) && isObjectType4(type)) {
733
889
  recurse(selection, typeName);
734
890
  } else {
735
891
  newSelections.push(selection);
@@ -764,7 +920,7 @@ function transformFragment(schema, fragment, fragments) {
764
920
  if (typeName === COMPONENT_UNION) {
765
921
  selectionSet = addFlatRefSelections(selectionSet);
766
922
  }
767
- if (isObjectType3(type)) {
923
+ if (isObjectType4(type)) {
768
924
  const refsOnly = isComponentSubtype(schema, typeName);
769
925
  selectionSet = transformSelectionSet(schema, selectionSet, typeName, fragments, refsOnly);
770
926
  } else if (isUnionType2(type)) {
@@ -772,7 +928,7 @@ function transformFragment(schema, fragment, fragments) {
772
928
  if (sel.kind === Kind3.INLINE_FRAGMENT && sel.typeCondition && sel.selectionSet) {
773
929
  const inlineTypeName = sel.typeCondition.name.value;
774
930
  const inlineType = schema.getType(inlineTypeName);
775
- if (inlineType && isObjectType3(inlineType)) {
931
+ if (inlineType && isObjectType4(inlineType)) {
776
932
  const transformed = transformSelectionSet(schema, sel.selectionSet, inlineTypeName, fragments, true);
777
933
  if (transformed !== sel.selectionSet) {
778
934
  return { ...sel, selectionSet: transformed };
@@ -789,6 +945,125 @@ function transformFragment(schema, fragment, fragments) {
789
945
  return { ...fragment, selectionSet };
790
946
  }
791
947
 
948
+ // src/generate/transform-scalars.ts
949
+ import {
950
+ Kind as Kind4,
951
+ isCompositeType as isCompositeType3,
952
+ isInterfaceType as isInterfaceType3,
953
+ isListType as isListType5,
954
+ isNonNullType as isNonNullType5,
955
+ isObjectType as isObjectType5,
956
+ isScalarType as isScalarType3
957
+ } from "graphql";
958
+ var SCALAR_PREFIX = {
959
+ LocalDateTime: "_ldt_",
960
+ LocalDate: "_ld_"
961
+ };
962
+ function namedTypeName(type) {
963
+ let current = type;
964
+ while (isNonNullType5(current) || isListType5(current)) {
965
+ current = current.ofType;
966
+ }
967
+ return isScalarType3(current) ? current.name : void 0;
968
+ }
969
+ function prefixFor(typeName) {
970
+ return SCALAR_PREFIX[typeName];
971
+ }
972
+ function transformSelectionSet2(schema, selectionSet, parentTypeName) {
973
+ const parentType = schema.getType(parentTypeName);
974
+ if (!parentType || !isObjectType5(parentType) && !isInterfaceType3(parentType)) {
975
+ return selectionSet;
976
+ }
977
+ const parentFields = parentType.getFields();
978
+ const newSelections = [];
979
+ let changed = false;
980
+ for (const selection of selectionSet.selections) {
981
+ if (selection.kind === Kind4.FIELD) {
982
+ const schemaField = parentFields[selection.name.value];
983
+ if (!schemaField) {
984
+ newSelections.push(selection);
985
+ continue;
986
+ }
987
+ const scalarName = namedTypeName(schemaField.type);
988
+ const prefix = scalarName ? prefixFor(scalarName) : void 0;
989
+ if (prefix) {
990
+ const aliasValue = selection.alias?.value ?? selection.name.value;
991
+ if (aliasValue.startsWith(prefix)) {
992
+ newSelections.push(selection);
993
+ } else {
994
+ changed = true;
995
+ newSelections.push({
996
+ ...selection,
997
+ alias: { kind: Kind4.NAME, value: `${prefix}${aliasValue}` }
998
+ });
999
+ }
1000
+ continue;
1001
+ }
1002
+ if (selection.selectionSet) {
1003
+ const nestedTypeName = namedCompositeName(schemaField.type);
1004
+ if (nestedTypeName) {
1005
+ const transformed = transformSelectionSet2(schema, selection.selectionSet, nestedTypeName);
1006
+ if (transformed !== selection.selectionSet) {
1007
+ changed = true;
1008
+ newSelections.push({ ...selection, selectionSet: transformed });
1009
+ continue;
1010
+ }
1011
+ }
1012
+ }
1013
+ newSelections.push(selection);
1014
+ } else if (selection.kind === Kind4.INLINE_FRAGMENT) {
1015
+ const typeName = selection.typeCondition?.name.value;
1016
+ if (typeName && selection.selectionSet) {
1017
+ const type = schema.getType(typeName);
1018
+ if (type && isCompositeType3(type)) {
1019
+ const transformed = transformSelectionSet2(schema, selection.selectionSet, typeName);
1020
+ if (transformed !== selection.selectionSet) {
1021
+ changed = true;
1022
+ newSelections.push({ ...selection, selectionSet: transformed });
1023
+ continue;
1024
+ }
1025
+ }
1026
+ }
1027
+ newSelections.push(selection);
1028
+ } else {
1029
+ newSelections.push(selection);
1030
+ }
1031
+ }
1032
+ if (!changed) {
1033
+ return selectionSet;
1034
+ }
1035
+ return { ...selectionSet, selections: newSelections };
1036
+ }
1037
+ function namedCompositeName(type) {
1038
+ let current = type;
1039
+ while (isNonNullType5(current) || isListType5(current)) {
1040
+ current = current.ofType;
1041
+ }
1042
+ return isCompositeType3(current) ? current.name : void 0;
1043
+ }
1044
+ function transformScalarOperation(schema, operation) {
1045
+ const rootType = operation.operation === "query" ? schema.getQueryType() : operation.operation === "mutation" ? schema.getMutationType() : schema.getSubscriptionType();
1046
+ if (!rootType || !operation.selectionSet) {
1047
+ return operation;
1048
+ }
1049
+ const transformed = transformSelectionSet2(schema, operation.selectionSet, rootType.name);
1050
+ if (transformed === operation.selectionSet) {
1051
+ return operation;
1052
+ }
1053
+ return { ...operation, selectionSet: transformed };
1054
+ }
1055
+ function transformScalarFragment(schema, fragment) {
1056
+ const type = schema.getType(fragment.typeCondition.name.value);
1057
+ if (!type || !isCompositeType3(type)) {
1058
+ return fragment;
1059
+ }
1060
+ const transformed = transformSelectionSet2(schema, fragment.selectionSet, type.name);
1061
+ if (transformed === fragment.selectionSet) {
1062
+ return fragment;
1063
+ }
1064
+ return { ...fragment, selectionSet: transformed };
1065
+ }
1066
+
792
1067
  // src/generate/index.ts
793
1068
  var ctrlSeq = "\x1B[";
794
1069
  var styled = (s, pre, post) => `${ctrlSeq}${pre}${s}${ctrlSeq}${post}`;
@@ -797,6 +1072,15 @@ var bold = (s) => styled(s, "1m", "22m");
797
1072
  var green = (s) => styled(s, "32m", "39m");
798
1073
  var yellow = (s) => styled(s, "33m", "39m");
799
1074
  var cyan = (s) => styled(s, "36m", "39m");
1075
+ function collectFragmentSpreads(fragment) {
1076
+ const refs = /* @__PURE__ */ new Set();
1077
+ visit(fragment, {
1078
+ [Kind5.FRAGMENT_SPREAD](node) {
1079
+ refs.add(node.name.value);
1080
+ }
1081
+ });
1082
+ return refs;
1083
+ }
800
1084
  async function main() {
801
1085
  const cwd = process.cwd();
802
1086
  const config = await loadConfig(cwd);
@@ -822,21 +1106,134 @@ async function main() {
822
1106
  [...fragments].map(([name, frag]) => [name, transformFragment(schema, frag, fragments)])
823
1107
  );
824
1108
  const transformedOperations = operations.map((op) => transformOperation(schema, op, transformedFragments));
1109
+ for (const [name, frag] of transformedFragments) {
1110
+ transformedFragments.set(name, transformScalarFragment(schema, frag));
1111
+ }
1112
+ for (let i = 0; i < transformedOperations.length; i++) {
1113
+ transformedOperations[i] = transformScalarOperation(schema, transformedOperations[i]);
1114
+ }
1115
+ const entityBindings = indexEntitySources(schema);
1116
+ const builtInBindings = indexBuiltInSources(schema);
1117
+ const sourceEntries = [];
1118
+ for (const [name, fragment] of fragments) {
1119
+ const entityBinding = entityBindings.get(fragment.typeCondition.name.value);
1120
+ if (!entityBinding) {
1121
+ continue;
1122
+ }
1123
+ const transformed = transformedFragments.get(name);
1124
+ if (!transformed) {
1125
+ continue;
1126
+ }
1127
+ sourceEntries.push({
1128
+ fragmentName: name,
1129
+ binding: {
1130
+ cardinality: entityBinding.cardinality,
1131
+ fieldName: entityBinding.fieldName,
1132
+ sourceKey: entityBinding.model
1133
+ },
1134
+ fragmentExpression: fragmentBundleExpression(transformed, transformedFragments)
1135
+ });
1136
+ }
1137
+ for (const [typeName, builtInBinding] of builtInBindings) {
1138
+ const candidates = [];
1139
+ for (const [name, fragment] of fragments) {
1140
+ if (fragment.typeCondition.name.value === typeName) {
1141
+ candidates.push({ name, fragment });
1142
+ }
1143
+ }
1144
+ if (candidates.length === 0) {
1145
+ continue;
1146
+ }
1147
+ const candidateNames = new Set(candidates.map((c) => c.name));
1148
+ const spreadByOther = /* @__PURE__ */ new Set();
1149
+ for (const { name, fragment } of candidates) {
1150
+ for (const ref of collectFragmentSpreads(fragment)) {
1151
+ if (ref !== name && candidateNames.has(ref)) {
1152
+ spreadByOther.add(ref);
1153
+ }
1154
+ }
1155
+ }
1156
+ const roots = candidates.filter((c) => !spreadByOther.has(c.name));
1157
+ const picked = roots.length === 1 ? roots[0] : void 0;
1158
+ if (!picked) {
1159
+ throw new Error(
1160
+ `multiple fragments target the built-in type "${typeName}" without a clear root: ${candidates.map((c) => `"${c.name}"`).join(", ")}. Either have one fragment spread the others, or remove the extras.`
1161
+ );
1162
+ }
1163
+ const transformed = transformedFragments.get(picked.name);
1164
+ if (!transformed) {
1165
+ continue;
1166
+ }
1167
+ sourceEntries.push({
1168
+ fragmentName: picked.name,
1169
+ binding: {
1170
+ cardinality: builtInBinding.cardinality,
1171
+ fieldName: builtInBinding.fieldName,
1172
+ sourceKey: uncapitalize(picked.name)
1173
+ },
1174
+ fragmentExpression: fragmentBundleExpression(transformed, transformedFragments)
1175
+ });
1176
+ }
1177
+ const entityModels = extractEntityModels(schema);
825
1178
  const parts = [];
826
1179
  parts.push("/* eslint-disable */");
827
1180
  parts.push("// @ts-nocheck");
828
1181
  parts.push("/* this file is generated by contello-client \u2014 do not edit */");
829
1182
  parts.push("");
1183
+ const typeMap = schema.getTypeMap();
1184
+ const clientImports = ["SourceDef"];
1185
+ for (const scalar of MANAGED_SCALARS) {
1186
+ if (typeMap[scalar]) {
1187
+ clientImports.push(scalar);
1188
+ }
1189
+ }
1190
+ clientImports.sort();
1191
+ parts.push(`import type { ${clientImports.join(", ")} } from '@contello/client';`);
1192
+ parts.push("");
830
1193
  parts.push(generateSchemaTypes(schema));
831
1194
  parts.push(generateFragmentTypes(schema, fragments));
832
1195
  parts.push(generateOperationTypes(schema, operations, fragments));
1196
+ if (transformedFragments.size > 0) {
1197
+ parts.push(generateFragmentSchemas(transformedFragments));
1198
+ parts.push("");
1199
+ }
833
1200
  for (let i = 0; i < operations.length; i++) {
834
1201
  const camelName = uncapitalize(operations[i].name.value);
835
- const docString = generateDocumentString(transformedOperations[i], transformedFragments);
836
- parts.push(`export const ${camelName}Document = \`${docString}\`;`);
1202
+ const docExpr = operationDocumentExpression(transformedOperations[i], transformedFragments);
1203
+ parts.push(`export const ${camelName}Document = ${docExpr};`);
1204
+ }
1205
+ if (operations.length > 0) {
1206
+ parts.push("");
1207
+ }
1208
+ parts.push(generateOperationsType(operations));
1209
+ parts.push("");
1210
+ const sourcesTypeSrc = generateSourcesType(sourceEntries);
1211
+ if (sourcesTypeSrc) {
1212
+ parts.push(sourcesTypeSrc);
1213
+ parts.push("");
1214
+ }
1215
+ parts.push(generateOperationsConst(operations));
1216
+ parts.push("");
1217
+ const sourcesConstSrc = generateSourcesConst(sourceEntries);
1218
+ if (sourcesConstSrc) {
1219
+ parts.push(sourcesConstSrc);
1220
+ parts.push("");
1221
+ }
1222
+ if (entityModels.size > 0) {
1223
+ const sorted = [...entityModels.entries()].sort(([a], [b]) => a.localeCompare(b));
1224
+ parts.push("const models = {");
1225
+ for (const [model, typeName] of sorted) {
1226
+ parts.push(` ${model}: '${typeName}',`);
1227
+ }
1228
+ parts.push("} as const;");
1229
+ parts.push("");
1230
+ } else {
1231
+ parts.push("const models = {} as const;");
837
1232
  parts.push("");
838
1233
  }
839
- parts.push(generateOperationsObject(operations));
1234
+ const sourcesPart = sourcesConstSrc ? ", sources" : ", sources: {}";
1235
+ parts.push(`export const schema = { operations${sourcesPart}, models };`);
1236
+ parts.push("export type Schema = typeof schema;");
840
1237
  let output = parts.join("\n");
841
1238
  if (project.namespace) {
842
1239
  const indented = output.split("\n").map((line) => line ? ` ${line}` : "").join("\n");