@soda-gql/core 0.9.0 → 0.10.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.
Files changed (54) hide show
  1. package/README.md +67 -0
  2. package/dist/adapter.cjs +52 -5
  3. package/dist/adapter.cjs.map +1 -1
  4. package/dist/adapter.d.cts +52 -5
  5. package/dist/adapter.d.cts.map +1 -1
  6. package/dist/adapter.d.ts +52 -5
  7. package/dist/adapter.d.ts.map +1 -1
  8. package/dist/adapter.js +51 -4
  9. package/dist/adapter.js.map +1 -1
  10. package/dist/{index-wkJ6KSwK.d.ts → index-CHshzPrG.d.cts} +343 -66
  11. package/dist/index-CHshzPrG.d.cts.map +1 -0
  12. package/dist/{schema-2qqtKss4.d.ts → index-CnQ5XVCf.d.ts} +371 -300
  13. package/dist/index-CnQ5XVCf.d.ts.map +1 -0
  14. package/dist/{schema-CPTxQbTv.d.cts → index-DnLzk2bC.d.cts} +371 -300
  15. package/dist/index-DnLzk2bC.d.cts.map +1 -0
  16. package/dist/{index-Ib9pb2Si.d.cts → index-J050RXPw.d.ts} +343 -66
  17. package/dist/index-J050RXPw.d.ts.map +1 -0
  18. package/dist/index.cjs +425 -67
  19. package/dist/index.cjs.map +1 -1
  20. package/dist/index.d.cts +4 -125
  21. package/dist/index.d.cts.map +1 -1
  22. package/dist/index.d.ts +4 -125
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +408 -61
  25. package/dist/index.js.map +1 -1
  26. package/dist/runtime.cjs +4 -0
  27. package/dist/runtime.cjs.map +1 -1
  28. package/dist/runtime.d.cts +13 -3
  29. package/dist/runtime.d.cts.map +1 -1
  30. package/dist/runtime.d.ts +13 -3
  31. package/dist/runtime.d.ts.map +1 -1
  32. package/dist/runtime.js +4 -0
  33. package/dist/runtime.js.map +1 -1
  34. package/dist/schema-builder-C9Qj5zQg.js +58 -0
  35. package/dist/schema-builder-C9Qj5zQg.js.map +1 -0
  36. package/dist/schema-builder-CJe30s_R.d.ts +63 -0
  37. package/dist/schema-builder-CJe30s_R.d.ts.map +1 -0
  38. package/dist/schema-builder-CwRmtxHx.d.cts +63 -0
  39. package/dist/schema-builder-CwRmtxHx.d.cts.map +1 -0
  40. package/dist/schema-builder-D2ay11cE.cjs +82 -0
  41. package/dist/schema-builder-D2ay11cE.cjs.map +1 -0
  42. package/package.json +1 -1
  43. package/dist/index-Ib9pb2Si.d.cts.map +0 -1
  44. package/dist/index-wkJ6KSwK.d.ts.map +0 -1
  45. package/dist/schema-2qqtKss4.d.ts.map +0 -1
  46. package/dist/schema-BiYcVVvm.js +0 -171
  47. package/dist/schema-BiYcVVvm.js.map +0 -1
  48. package/dist/schema-CPTxQbTv.d.cts.map +0 -1
  49. package/dist/schema-D2MW4DOF.cjs +0 -207
  50. package/dist/schema-D2MW4DOF.cjs.map +0 -1
  51. package/dist/schema-builder-BYJd50o2.d.cts +0 -97
  52. package/dist/schema-builder-BYJd50o2.d.cts.map +0 -1
  53. package/dist/schema-builder-Dhss2O1I.d.ts +0 -97
  54. package/dist/schema-builder-Dhss2O1I.d.ts.map +0 -1
package/dist/index.js CHANGED
@@ -1,37 +1,6 @@
1
- import { a as unsafeOutputType, i as unsafeInputType, n as defineOperationRoots, o as wrapByKey, r as defineScalar, t as define } from "./schema-BiYcVVvm.js";
1
+ import { i as wrapByKey, n as defineOperationRoots, r as defineScalar, t as defineEnum } from "./schema-builder-C9Qj5zQg.js";
2
2
  import { Kind, OperationTypeNode } from "graphql";
3
3
 
4
- //#region packages/core/src/types/type-foundation/directive-ref.ts
5
- /**
6
- * A reference to a directive that can be applied to fields.
7
- *
8
- * DirectiveRef carries type information about the directive via the TBrand
9
- * type parameter, but this information is only used for type inference,
10
- * not for runtime validation.
11
- *
12
- * @example
13
- * ```typescript
14
- * const skipDirective = new DirectiveRef({
15
- * name: "skip",
16
- * arguments: { if: true },
17
- * locations: ["FIELD", "FRAGMENT_SPREAD", "INLINE_FRAGMENT"],
18
- * });
19
- * ```
20
- */
21
- var DirectiveRef = class {
22
- constructor(inner) {
23
- this.inner = inner;
24
- }
25
- /**
26
- * Extracts the inner structure from a DirectiveRef.
27
- * Used by build-document.ts to generate DirectiveNode.
28
- */
29
- static getInner(ref) {
30
- return ref.inner;
31
- }
32
- };
33
-
34
- //#endregion
35
4
  //#region packages/core/src/types/type-foundation/var-ref.ts
36
5
  var VarRef = class {
37
6
  constructor(inner) {
@@ -62,6 +31,37 @@ function createVarRefFromNestedValue(value) {
62
31
  });
63
32
  }
64
33
 
34
+ //#endregion
35
+ //#region packages/core/src/types/type-foundation/directive-ref.ts
36
+ /**
37
+ * A reference to a directive that can be applied to fields.
38
+ *
39
+ * DirectiveRef carries type information about the directive via the TBrand
40
+ * type parameter, but this information is only used for type inference,
41
+ * not for runtime validation.
42
+ *
43
+ * @example
44
+ * ```typescript
45
+ * const skipDirective = new DirectiveRef({
46
+ * name: "skip",
47
+ * arguments: { if: true },
48
+ * locations: ["FIELD", "FRAGMENT_SPREAD", "INLINE_FRAGMENT"],
49
+ * });
50
+ * ```
51
+ */
52
+ var DirectiveRef = class {
53
+ constructor(inner) {
54
+ this.inner = inner;
55
+ }
56
+ /**
57
+ * Extracts the inner structure from a DirectiveRef.
58
+ * Used by build-document.ts to generate DirectiveNode.
59
+ */
60
+ static getInner(ref) {
61
+ return ref.inner;
62
+ }
63
+ };
64
+
65
65
  //#endregion
66
66
  //#region packages/core/src/composer/build-document.ts
67
67
  /**
@@ -667,7 +667,7 @@ const createFieldFactoriesInner = (schema, typeName) => {
667
667
  * @param createDepGenerator - Function to create evaluation generator for a dependency
668
668
  * @returns An executor generator function
669
669
  */
670
- const createLazyEvaluator = (define$1, getDeps, createDepGenerator) => {
670
+ const createLazyEvaluator = (define, getDeps, createDepGenerator) => {
671
671
  let cache = null;
672
672
  let promise = null;
673
673
  return function* execute(context) {
@@ -677,7 +677,7 @@ const createLazyEvaluator = (define$1, getDeps, createDepGenerator) => {
677
677
  return cache.value;
678
678
  }
679
679
  if (getDeps) for (const dep of getDeps()) yield* createDepGenerator(dep);
680
- const defined = define$1(context);
680
+ const defined = define(context);
681
681
  if (!(defined instanceof Promise)) return (cache = { value: defined }).value;
682
682
  promise = defined.then((value) => {
683
683
  cache = { value };
@@ -720,8 +720,8 @@ const GQL_ELEMENT_CONTEXT = Symbol("GQL_ELEMENT_CONTEXT");
720
720
  var GqlElement = class GqlElement {
721
721
  [GQL_ELEMENT_FACTORY];
722
722
  [GQL_ELEMENT_CONTEXT] = null;
723
- constructor(define$1, getDeps) {
724
- this[GQL_ELEMENT_FACTORY] = createLazyEvaluator(define$1, getDeps, GqlElement.createEvaluationGenerator);
723
+ constructor(define, getDeps) {
724
+ this[GQL_ELEMENT_FACTORY] = createLazyEvaluator(define, getDeps, GqlElement.createEvaluationGenerator);
725
725
  Object.defineProperty(this, "$infer", { get() {
726
726
  throw new Error("This property is only for type meta. Do not access this property directly.");
727
727
  } });
@@ -792,15 +792,28 @@ var GqlElement = class GqlElement {
792
792
  * @template TVariables - Variables required when spreading
793
793
  * @template TFields - The selected fields structure
794
794
  * @template TOutput - Inferred output type from selected fields
795
+ * @template TKey - Optional unique key for prebuilt type lookup
795
796
  */
796
797
  var Fragment = class Fragment extends GqlElement {
797
- constructor(define$1) {
798
- super(define$1);
798
+ constructor(define) {
799
+ super(define);
799
800
  }
800
801
  /** The GraphQL type name this fragment selects from. */
801
802
  get typename() {
802
803
  return GqlElement.get(this).typename;
803
804
  }
805
+ /** Optional unique key for prebuilt type lookup. */
806
+ get key() {
807
+ return GqlElement.get(this).key;
808
+ }
809
+ /** The schema label this fragment belongs to. */
810
+ get schemaLabel() {
811
+ return GqlElement.get(this).schemaLabel;
812
+ }
813
+ /** Variable definitions for this fragment. */
814
+ get variableDefinitions() {
815
+ return GqlElement.get(this).variableDefinitions;
816
+ }
804
817
  /**
805
818
  * Spreads this fragment's fields into a parent selection.
806
819
  * Pass variables if the fragment defines any.
@@ -813,8 +826,8 @@ var Fragment = class Fragment extends GqlElement {
813
826
  * Prefer using the `gql(({ fragment }) => ...)` API instead.
814
827
  * @internal
815
828
  */
816
- static create(define$1) {
817
- return new Fragment(define$1);
829
+ static create(define) {
830
+ return new Fragment(define);
818
831
  }
819
832
  };
820
833
 
@@ -834,8 +847,8 @@ var Fragment = class Fragment extends GqlElement {
834
847
  * @template TData - Inferred response data type
835
848
  */
836
849
  var Operation = class Operation extends GqlElement {
837
- constructor(define$1) {
838
- super(define$1);
850
+ constructor(define) {
851
+ super(define);
839
852
  }
840
853
  /** The operation type: 'query', 'mutation', or 'subscription'. */
841
854
  get operationType() {
@@ -845,6 +858,10 @@ var Operation = class Operation extends GqlElement {
845
858
  get operationName() {
846
859
  return GqlElement.get(this).operationName;
847
860
  }
861
+ /** The schema label this operation belongs to. */
862
+ get schemaLabel() {
863
+ return GqlElement.get(this).schemaLabel;
864
+ }
848
865
  /** List of variable names defined for this operation. */
849
866
  get variableNames() {
850
867
  return GqlElement.get(this).variableNames;
@@ -869,8 +886,8 @@ var Operation = class Operation extends GqlElement {
869
886
  * Prefer using the `gql(({ query }) => ...)` API instead.
870
887
  * @internal
871
888
  */
872
- static create(define$1) {
873
- return new Operation(define$1);
889
+ static create(define) {
890
+ return new Operation(define);
874
891
  }
875
892
  };
876
893
 
@@ -968,9 +985,12 @@ const createGqlFragmentComposers = (schema, _adapter) => {
968
985
  const createFragmentComposer = (typename) => {
969
986
  return (options) => {
970
987
  const varDefinitions = options.variables ?? {};
971
- const { metadata, fields } = options;
988
+ const { key, metadata, fields } = options;
972
989
  return Fragment.create(() => ({
973
990
  typename,
991
+ key,
992
+ schemaLabel: schema.label,
993
+ variableDefinitions: varDefinitions,
974
994
  spread: (variables) => {
975
995
  const f = createFieldFactories(schema, typename);
976
996
  const $ = createVarAssignments(varDefinitions, variables);
@@ -1018,11 +1038,12 @@ const defaultMetadataAdapter = createDefaultAdapter();
1018
1038
  *
1019
1039
  * @param schema - The GraphQL schema definition
1020
1040
  * @param adapter - Optional metadata adapter for custom metadata handling
1041
+ * @param transformDocument - Optional document transformer called after building
1021
1042
  * @returns Operation type selector function
1022
1043
  *
1023
1044
  * @internal Used by `createGqlElementComposer`
1024
1045
  */
1025
- const createOperationComposerFactory = (schema, adapter) => {
1046
+ const createOperationComposerFactory = (schema, adapter, transformDocument) => {
1026
1047
  const resolvedAdapter = adapter ?? defaultMetadataAdapter;
1027
1048
  return (operationType) => {
1028
1049
  const operationTypeName = schema.operations[operationType];
@@ -1043,23 +1064,26 @@ const createOperationComposerFactory = (schema, adapter) => {
1043
1064
  variables,
1044
1065
  fields
1045
1066
  });
1046
- const createDefinition = (metadata) => ({
1067
+ const variableNames = Object.keys(variables);
1068
+ if (!fragmentUsages.some((u) => u.metadataBuilder) && !options.metadata && !transformDocument && !options.transformDocument) return {
1047
1069
  operationType,
1048
1070
  operationName,
1049
- variableNames: Object.keys(variables),
1071
+ schemaLabel: schema.label,
1072
+ variableNames,
1050
1073
  documentSource: () => fields,
1051
1074
  document,
1052
- metadata
1053
- });
1054
- if (!fragmentUsages.some((u) => u.metadataBuilder) && !options.metadata) return createDefinition(void 0);
1055
- const fragmentMetadataResults = fragmentUsages.map((usage) => usage.metadataBuilder ? usage.metadataBuilder() : void 0);
1056
- const hasAsyncFragmentMetadata = fragmentMetadataResults.some((r) => r instanceof Promise);
1057
- const buildOperationMetadata = (resolvedFragmentMetadata) => {
1075
+ metadata: void 0
1076
+ };
1077
+ const aggregateFragmentMetadata = (resolvedFragmentMetadata) => {
1058
1078
  const fragmentMetaInfos = fragmentUsages.map((usage, index) => ({
1059
1079
  metadata: resolvedFragmentMetadata[index],
1060
1080
  fieldPath: usage.path
1061
1081
  }));
1062
- const aggregatedFragmentMetadata = resolvedAdapter.aggregateFragmentMetadata(fragmentMetaInfos);
1082
+ return resolvedAdapter.aggregateFragmentMetadata(fragmentMetaInfos);
1083
+ };
1084
+ const fragmentMetadataResults = fragmentUsages.map((usage) => usage.metadataBuilder ? usage.metadataBuilder() : void 0);
1085
+ const hasAsyncFragmentMetadata = fragmentMetadataResults.some((r) => r instanceof Promise);
1086
+ const buildOperationMetadata = (aggregatedFragmentMetadata) => {
1063
1087
  const schemaLevel = resolvedAdapter.schemaLevel;
1064
1088
  return options.metadata?.({
1065
1089
  $,
@@ -1068,12 +1092,41 @@ const createOperationComposerFactory = (schema, adapter) => {
1068
1092
  schemaLevel
1069
1093
  });
1070
1094
  };
1095
+ const makeCreateDefinition = (aggregated$1) => {
1096
+ return ({ metadata }) => {
1097
+ let finalDocument = options.transformDocument ? options.transformDocument({
1098
+ document,
1099
+ metadata
1100
+ }) : document;
1101
+ if (transformDocument) finalDocument = transformDocument({
1102
+ document: finalDocument,
1103
+ operationName,
1104
+ operationType,
1105
+ variableNames,
1106
+ schemaLevel: resolvedAdapter.schemaLevel,
1107
+ fragmentMetadata: aggregated$1
1108
+ });
1109
+ return {
1110
+ operationType,
1111
+ operationName,
1112
+ schemaLabel: schema.label,
1113
+ variableNames,
1114
+ documentSource: () => fields,
1115
+ document: finalDocument,
1116
+ metadata
1117
+ };
1118
+ };
1119
+ };
1071
1120
  if (hasAsyncFragmentMetadata) return Promise.all(fragmentMetadataResults).then(async (resolvedFragmentMetadata) => {
1072
- return createDefinition(await buildOperationMetadata(resolvedFragmentMetadata));
1121
+ const aggregated$1 = aggregateFragmentMetadata(resolvedFragmentMetadata);
1122
+ const operationMetadata = await buildOperationMetadata(aggregated$1);
1123
+ return makeCreateDefinition(aggregated$1)({ metadata: operationMetadata });
1073
1124
  });
1074
- const operationMetadataResult = buildOperationMetadata(fragmentMetadataResults);
1075
- if (operationMetadataResult instanceof Promise) return operationMetadataResult.then(createDefinition);
1076
- return createDefinition(operationMetadataResult);
1125
+ const aggregated = aggregateFragmentMetadata(fragmentMetadataResults);
1126
+ const createDefinition = makeCreateDefinition(aggregated);
1127
+ const operationMetadataResult = buildOperationMetadata(aggregated);
1128
+ if (operationMetadataResult instanceof Promise) return operationMetadataResult.then((metadata) => createDefinition({ metadata }));
1129
+ return createDefinition({ metadata: operationMetadataResult });
1077
1130
  });
1078
1131
  };
1079
1132
  };
@@ -1329,8 +1382,9 @@ const createGqlElementComposer = (schema, options) => {
1329
1382
  const { adapter, inputTypeMethods, directiveMethods } = options;
1330
1383
  const helpers = adapter?.helpers;
1331
1384
  const metadataAdapter = adapter?.metadata;
1385
+ const transformDocument = adapter?.transformDocument;
1332
1386
  const fragment = createGqlFragmentComposers(schema, metadataAdapter);
1333
- const createOperationComposer = createOperationComposerFactory(schema, metadataAdapter);
1387
+ const createOperationComposer = createOperationComposerFactory(schema, metadataAdapter, transformDocument);
1334
1388
  const context = {
1335
1389
  fragment,
1336
1390
  query: { operation: createOperationComposer("query") },
@@ -1346,5 +1400,298 @@ const createGqlElementComposer = (schema, options) => {
1346
1400
  };
1347
1401
 
1348
1402
  //#endregion
1349
- export { Fragment, GqlElement, Operation, appendToPath, buildArgumentValue, buildConstValueNode, buildDocument, buildOperationTypeNode, buildWithTypeModifier, createColocateHelper, createDefaultAdapter, createDirectiveBuilder, createDirectiveMethod, createFieldFactories, createGqlElementComposer, createGqlFragmentComposers, createOperationComposerFactory, createStandardDirectives, createVarAssignments, createVarBuilder, createVarMethod, createVarMethodFactory, createVarRefs, defaultMetadataAdapter, define, defineOperationRoots, defineScalar, getCurrentFieldPath, isDirectiveRef, isListType, recordFragmentUsage, unsafeInputType, unsafeOutputType, withFieldPath, withFragmentUsageCollection };
1403
+ //#region packages/core/src/composer/prebuilt-composer.ts
1404
+ /**
1405
+ * Creates a prebuilt GQL element composer for a given schema.
1406
+ *
1407
+ * This composer has the same runtime behavior as `createGqlElementComposer`,
1408
+ * but the returned elements have their types resolved from the PrebuiltTypeRegistry
1409
+ * instead of using complex type inference.
1410
+ *
1411
+ * Use this when bundling with tools like tsdown that may lose type information.
1412
+ *
1413
+ * @param schema - The GraphQL schema definition
1414
+ * @param options - Configuration including input type methods and optional adapter
1415
+ * @returns Prebuilt element composer function
1416
+ *
1417
+ * @example
1418
+ * ```typescript
1419
+ * // Generated by codegen in prebuilt/index.ts
1420
+ * import type { PrebuiltTypes } from "./types";
1421
+ *
1422
+ * const gql = createPrebuiltGqlElementComposer<
1423
+ * Schema,
1424
+ * PrebuiltTypes,
1425
+ * FragmentBuilders,
1426
+ * DirectiveMethods,
1427
+ * Context
1428
+ * >(schema, { inputTypeMethods });
1429
+ *
1430
+ * // Types are resolved from PrebuiltTypes registry
1431
+ * const GetUser = gql(({ query }) =>
1432
+ * query.operation({
1433
+ * name: "GetUser",
1434
+ * fields: ({ f }) => ({ ...f.user({ id: "1" })(({ f }) => ({ ...f.id() })) }),
1435
+ * })
1436
+ * );
1437
+ * // GetUser.$infer.output is PrebuiltTypes["operations"]["GetUser"]["output"]
1438
+ * ```
1439
+ */
1440
+ const createPrebuiltGqlElementComposer = (schema, options) => {
1441
+ return createGqlElementComposer(schema, options);
1442
+ };
1443
+
1444
+ //#endregion
1445
+ //#region packages/core/src/prebuilt/type-calculator.ts
1446
+ /**
1447
+ * Type calculator for generating TypeScript type strings from field selections.
1448
+ *
1449
+ * This is a runtime reimplementation of the InferFields type-level computation,
1450
+ * used for generating prebuilt types that can be bundled without losing type information.
1451
+ *
1452
+ * @module
1453
+ */
1454
+ /**
1455
+ * Apply a type modifier to a base type string.
1456
+ *
1457
+ * Modifier format:
1458
+ * - "!" = required (T)
1459
+ * - "?" = optional (T | null | undefined)
1460
+ * - "![]!" = required array of required items (T[])
1461
+ * - "![]?" = optional array of required items (T[] | null | undefined)
1462
+ * - "?[]!" = required array of optional items ((T | null | undefined)[])
1463
+ * - "?[]?" = optional array of optional items ((T | null | undefined)[] | null | undefined)
1464
+ * - Deeper nesting follows the same pattern
1465
+ */
1466
+ const applyTypeModifier = (baseType, modifier) => {
1467
+ if (modifier === "!") return baseType;
1468
+ if (modifier === "?") return `(${baseType} | null | undefined)`;
1469
+ const parts = modifier.split("[]");
1470
+ if (parts.length < 2) return baseType;
1471
+ let result = baseType;
1472
+ if (parts[0] === "?") result = `(${result} | null | undefined)`;
1473
+ for (let i = 1; i < parts.length; i++) {
1474
+ const arrayNullability = parts[i];
1475
+ result = `(${result})[]`;
1476
+ if (arrayNullability === "?") result = `(${result} | null | undefined)`;
1477
+ }
1478
+ return result;
1479
+ };
1480
+ /**
1481
+ * Get the TypeScript type string for a scalar output type from the schema.
1482
+ *
1483
+ * Returns a `ScalarOutput<"Name">` reference for all scalars in the schema.
1484
+ * The actual type is resolved at compile time from the inject file's scalar definitions.
1485
+ * This allows users to customize even built-in scalars (ID, String, etc.).
1486
+ */
1487
+ const getScalarOutputType = (schema, scalarName) => {
1488
+ if (schema.scalar[scalarName]) return `ScalarOutput<"${scalarName}">`;
1489
+ return "unknown";
1490
+ };
1491
+ /**
1492
+ * Get the TypeScript type string for a scalar input type from the schema.
1493
+ *
1494
+ * Returns a `ScalarInput<"Name">` reference for all scalars in the schema.
1495
+ * Used for input/variable types in operations.
1496
+ */
1497
+ const getScalarInputType = (schema, scalarName) => {
1498
+ if (schema.scalar[scalarName]) return `ScalarInput<"${scalarName}">`;
1499
+ return "unknown";
1500
+ };
1501
+ /**
1502
+ * Get the TypeScript type string for an enum type from the schema.
1503
+ */
1504
+ const getEnumType = (schema, enumName) => {
1505
+ const enumDef = schema.enum[enumName];
1506
+ if (!enumDef) return "string";
1507
+ const values = Object.keys(enumDef.values);
1508
+ if (values.length === 0) return "never";
1509
+ return values.map((v) => `"${v}"`).join(" | ");
1510
+ };
1511
+ /**
1512
+ * Default depth limit for input object type generation.
1513
+ */
1514
+ const DEFAULT_INPUT_DEPTH = 3;
1515
+ /**
1516
+ * Generate a TypeScript type string for an input object type.
1517
+ *
1518
+ * Recursively expands fields using ScalarInput for scalars.
1519
+ * Returns `unknown` if depth is exhausted or circular reference is detected.
1520
+ *
1521
+ * @param schema - The GraphQL schema
1522
+ * @param inputName - The input object type name
1523
+ * @param options - Generation options including depth limits
1524
+ * @param seen - Set of already visited input names (for circular reference detection)
1525
+ * @param currentDepth - Current recursion depth
1526
+ */
1527
+ const generateInputObjectType = (schema, inputName, options = {}, seen = /* @__PURE__ */ new Set(), currentDepth) => {
1528
+ const inputDef = schema.input[inputName];
1529
+ if (!inputDef) return "unknown";
1530
+ const depthOverrides = options.depthOverrides ?? {};
1531
+ const defaultDepth = options.defaultDepth ?? DEFAULT_INPUT_DEPTH;
1532
+ const maxDepth = depthOverrides[inputName] ?? defaultDepth;
1533
+ const depth = currentDepth ?? maxDepth;
1534
+ if (depth <= 0) return "unknown";
1535
+ if (seen.has(inputName)) return "unknown";
1536
+ const newSeen = new Set(seen);
1537
+ newSeen.add(inputName);
1538
+ const fields = inputDef.fields;
1539
+ const fieldEntries = Object.entries(fields);
1540
+ if (fieldEntries.length === 0) return "{}";
1541
+ return `{ ${fieldEntries.map(([fieldName, specifier]) => {
1542
+ const fieldType = generateInputFieldType(schema, specifier, options, newSeen, depth - 1);
1543
+ const isOptional = specifier.modifier === "?" || specifier.modifier.endsWith("?");
1544
+ if (specifier.defaultValue != null || isOptional) return `readonly ${fieldName}?: ${fieldType}`;
1545
+ return `readonly ${fieldName}: ${fieldType}`;
1546
+ }).join("; ")} }`;
1547
+ };
1548
+ /**
1549
+ * Generate a TypeScript type string for an input field based on its specifier.
1550
+ */
1551
+ const generateInputFieldType = (schema, specifier, options, seen, depth) => {
1552
+ let baseType;
1553
+ const { formatters } = options;
1554
+ switch (specifier.kind) {
1555
+ case "scalar":
1556
+ baseType = formatters?.scalarInput?.(specifier.name) ?? getScalarInputType(schema, specifier.name);
1557
+ break;
1558
+ case "enum":
1559
+ baseType = getEnumType(schema, specifier.name);
1560
+ break;
1561
+ case "input":
1562
+ baseType = formatters?.inputObject?.(specifier.name) ?? generateInputObjectType(schema, specifier.name, options, seen, depth);
1563
+ break;
1564
+ default: baseType = "unknown";
1565
+ }
1566
+ return applyTypeModifier(baseType, specifier.modifier);
1567
+ };
1568
+ /**
1569
+ * Calculate the TypeScript type string for a single field selection.
1570
+ *
1571
+ * @param schema - The GraphQL schema
1572
+ * @param selection - The field selection to calculate type for
1573
+ * @param formatters - Optional formatters for customizing type names
1574
+ */
1575
+ const calculateFieldType = (schema, selection, formatters) => {
1576
+ const { type } = selection;
1577
+ if (type.kind === "object" && selection.object) return applyTypeModifier(calculateFieldsType(schema, selection.object, formatters), type.modifier);
1578
+ if (type.kind === "union" && selection.union) return applyTypeModifier(calculateUnionType(schema, selection.union, formatters), type.modifier);
1579
+ if (type.kind === "typename") return applyTypeModifier(`"${type.name}"`, type.modifier);
1580
+ if (type.kind === "scalar") return applyTypeModifier(formatters?.scalarOutput?.(type.name) ?? getScalarOutputType(schema, type.name), type.modifier);
1581
+ if (type.kind === "enum") return applyTypeModifier(getEnumType(schema, type.name), type.modifier);
1582
+ return "unknown";
1583
+ };
1584
+ /**
1585
+ * Calculate the TypeScript type string for a union type selection.
1586
+ */
1587
+ const calculateUnionType = (schema, union, formatters) => {
1588
+ const memberTypes = [];
1589
+ for (const [_typeName, fields] of Object.entries(union)) if (fields) {
1590
+ const memberType = calculateFieldsType(schema, fields, formatters);
1591
+ memberTypes.push(memberType);
1592
+ }
1593
+ if (memberTypes.length === 0) return "never";
1594
+ return memberTypes.join(" | ");
1595
+ };
1596
+ /**
1597
+ * Calculate the TypeScript type string for a set of field selections.
1598
+ * This is the main entry point for type calculation.
1599
+ *
1600
+ * @param schema - The GraphQL schema
1601
+ * @param fields - The field selections to calculate types for
1602
+ * @param formatters - Optional formatters for customizing type names
1603
+ */
1604
+ const calculateFieldsType = (schema, fields, formatters) => {
1605
+ const entries = Object.entries(fields);
1606
+ if (entries.length === 0) return "{}";
1607
+ return `{ ${entries.map(([alias, selection]) => {
1608
+ return `readonly ${alias}: ${calculateFieldType(schema, selection, formatters)}`;
1609
+ }).join("; ")} }`;
1610
+ };
1611
+ /**
1612
+ * Convert a GraphQL TypeNode to a TypeScript type string for input types.
1613
+ *
1614
+ * Handles NonNullType, ListType, and NamedType recursively.
1615
+ * Uses ScalarInput for scalar types since this is used for input/variable types.
1616
+ *
1617
+ * @param schema - The GraphQL schema
1618
+ * @param typeNode - The GraphQL type node to convert
1619
+ * @param formatters - Optional formatters for customizing type names
1620
+ */
1621
+ const graphqlTypeToTypeScript = (schema, typeNode, formatters) => {
1622
+ switch (typeNode.kind) {
1623
+ case Kind.NON_NULL_TYPE: return graphqlTypeToTypeScript(schema, typeNode.type, formatters);
1624
+ case Kind.LIST_TYPE: return `(${graphqlTypeToTypeScript(schema, typeNode.type, formatters)})[]`;
1625
+ case Kind.NAMED_TYPE: {
1626
+ const name = typeNode.name.value;
1627
+ if (schema.scalar[name]) return formatters?.scalarInput?.(name) ?? getScalarInputType(schema, name);
1628
+ if (schema.enum[name]) return getEnumType(schema, name);
1629
+ return formatters?.inputObject?.(name) ?? name;
1630
+ }
1631
+ }
1632
+ };
1633
+ /**
1634
+ * Generate a TypeScript type string for operation input variables.
1635
+ *
1636
+ * Extracts variable types from GraphQL VariableDefinitionNode AST.
1637
+ *
1638
+ * @param schema - The GraphQL schema
1639
+ * @param variableDefinitions - Variable definition nodes from the operation
1640
+ * @param formatters - Optional formatters for customizing type names
1641
+ */
1642
+ const generateInputType = (schema, variableDefinitions, formatters) => {
1643
+ if (variableDefinitions.length === 0) return "{}";
1644
+ return `{ ${variableDefinitions.map((varDef) => {
1645
+ const name = varDef.variable.name.value;
1646
+ const isRequired = varDef.type.kind === Kind.NON_NULL_TYPE;
1647
+ const tsType = graphqlTypeToTypeScript(schema, varDef.type, formatters);
1648
+ const finalType = isRequired ? tsType : `(${tsType} | null | undefined)`;
1649
+ return `readonly ${name}${isRequired ? "" : "?"}: ${finalType}`;
1650
+ }).join("; ")} }`;
1651
+ };
1652
+ /**
1653
+ * Generate TypeScript type for a single input field from its specifier.
1654
+ * Used by generateInputTypeFromSpecifiers.
1655
+ */
1656
+ const generateInputFieldTypeFromSpecifier = (schema, specifier, options) => {
1657
+ let baseType;
1658
+ const { formatters } = options;
1659
+ switch (specifier.kind) {
1660
+ case "scalar":
1661
+ baseType = formatters?.scalarInput?.(specifier.name) ?? getScalarInputType(schema, specifier.name);
1662
+ break;
1663
+ case "enum":
1664
+ baseType = getEnumType(schema, specifier.name);
1665
+ break;
1666
+ case "input":
1667
+ baseType = formatters?.inputObject?.(specifier.name) ?? generateInputObjectType(schema, specifier.name, options);
1668
+ break;
1669
+ default: baseType = "unknown";
1670
+ }
1671
+ return applyTypeModifier(baseType, specifier.modifier);
1672
+ };
1673
+ /**
1674
+ * Generate a TypeScript type string for input variables from InputTypeSpecifiers.
1675
+ *
1676
+ * Unlike generateInputType which works with GraphQL AST VariableDefinitionNode[],
1677
+ * this function works with soda-gql's internal InputTypeSpecifiers format.
1678
+ * Used for generating Fragment input types in prebuilt mode.
1679
+ *
1680
+ * @param schema - The GraphQL schema
1681
+ * @param specifiers - Input type specifiers (variable definitions)
1682
+ * @param options - Generation options including depth limits
1683
+ */
1684
+ const generateInputTypeFromSpecifiers = (schema, specifiers, options = {}) => {
1685
+ const entries = Object.entries(specifiers);
1686
+ if (entries.length === 0) return "void";
1687
+ return `{ ${entries.map(([name, specifier]) => {
1688
+ const isOuterRequired = specifier.modifier.endsWith("!");
1689
+ const hasDefault = specifier.defaultValue != null;
1690
+ const baseType = generateInputFieldTypeFromSpecifier(schema, specifier, options);
1691
+ return `readonly ${name}${!isOuterRequired || hasDefault ? "?" : ""}: ${baseType}`;
1692
+ }).join("; ")} }`;
1693
+ };
1694
+
1695
+ //#endregion
1696
+ export { Fragment, GqlElement, Operation, VarRef, appendToPath, applyTypeModifier, buildArgumentValue, buildConstValueNode, buildDocument, buildOperationTypeNode, buildWithTypeModifier, calculateFieldType, calculateFieldsType, createColocateHelper, createDefaultAdapter, createDirectiveBuilder, createDirectiveMethod, createFieldFactories, createGqlElementComposer, createGqlFragmentComposers, createOperationComposerFactory, createPrebuiltGqlElementComposer, createStandardDirectives, createVarAssignments, createVarBuilder, createVarMethod, createVarMethodFactory, createVarRefFromVariable, createVarRefs, defaultMetadataAdapter, defineEnum, defineOperationRoots, defineScalar, generateInputObjectType, generateInputType, generateInputTypeFromSpecifiers, getCurrentFieldPath, getEnumType, getScalarInputType, getScalarOutputType, graphqlTypeToTypeScript, isDirectiveRef, isListType, recordFragmentUsage, withFieldPath, withFragmentUsageCollection };
1350
1697
  //# sourceMappingURL=index.js.map