@soda-gql/core 0.9.1 → 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.
- package/dist/adapter.cjs +2 -2
- package/dist/adapter.d.cts +2 -2
- package/dist/adapter.d.ts +2 -2
- package/dist/adapter.js +1 -1
- package/dist/{index-_6fYTfcA.d.cts → index-CHshzPrG.d.cts} +335 -65
- package/dist/index-CHshzPrG.d.cts.map +1 -0
- package/dist/{index-zCOsREx0.d.cts → index-CnQ5XVCf.d.ts} +2 -2
- package/dist/{index-BlVgxrXb.d.ts.map → index-CnQ5XVCf.d.ts.map} +1 -1
- package/dist/{index-BlVgxrXb.d.ts → index-DnLzk2bC.d.cts} +2 -2
- package/dist/index-DnLzk2bC.d.cts.map +1 -0
- package/dist/{index-B_TU5U2U.d.ts → index-J050RXPw.d.ts} +335 -65
- package/dist/index-J050RXPw.d.ts.map +1 -0
- package/dist/index.cjs +378 -52
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -125
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +4 -125
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +361 -46
- package/dist/index.js.map +1 -1
- package/dist/runtime.cjs +4 -0
- package/dist/runtime.cjs.map +1 -1
- package/dist/runtime.d.cts +13 -3
- package/dist/runtime.d.cts.map +1 -1
- package/dist/runtime.d.ts +13 -3
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +4 -0
- package/dist/runtime.js.map +1 -1
- package/dist/schema-builder-C9Qj5zQg.js +58 -0
- package/dist/schema-builder-C9Qj5zQg.js.map +1 -0
- package/dist/schema-builder-CJe30s_R.d.ts +63 -0
- package/dist/schema-builder-CJe30s_R.d.ts.map +1 -0
- package/dist/schema-builder-CwRmtxHx.d.cts +63 -0
- package/dist/schema-builder-CwRmtxHx.d.cts.map +1 -0
- package/dist/schema-builder-D2ay11cE.cjs +82 -0
- package/dist/schema-builder-D2ay11cE.cjs.map +1 -0
- package/package.json +1 -1
- package/dist/index-B_TU5U2U.d.ts.map +0 -1
- package/dist/index-_6fYTfcA.d.cts.map +0 -1
- package/dist/index-zCOsREx0.d.cts.map +0 -1
- package/dist/schema-BiYcVVvm.js +0 -171
- package/dist/schema-BiYcVVvm.js.map +0 -1
- package/dist/schema-D2MW4DOF.cjs +0 -207
- package/dist/schema-D2MW4DOF.cjs.map +0 -1
- package/dist/schema-builder-BI5PQkH7.d.cts +0 -97
- package/dist/schema-builder-BI5PQkH7.d.cts.map +0 -1
- package/dist/schema-builder-CF_AwsOM.d.ts +0 -97
- package/dist/schema-builder-CF_AwsOM.d.ts.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,37 +1,6 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_schema_builder = require('./schema-builder-D2ay11cE.cjs');
|
|
2
2
|
let graphql = require("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
|
/**
|
|
@@ -598,7 +598,7 @@ const createFieldFactoriesInner = (schema, typeName) => {
|
|
|
598
598
|
if (!typeDef) throw new Error(`Type ${typeName} is not defined in schema objects`);
|
|
599
599
|
const entries = Object.entries(typeDef.fields).map(([fieldName, type]) => {
|
|
600
600
|
const factory = (fieldArgs, extras) => {
|
|
601
|
-
const wrap = (value) =>
|
|
601
|
+
const wrap = (value) => require_schema_builder.wrapByKey(extras?.alias ?? fieldName, value);
|
|
602
602
|
const directives = extras?.directives ?? [];
|
|
603
603
|
if (type.kind === "object") {
|
|
604
604
|
const factoryReturn = ((nest) => {
|
|
@@ -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
|
|
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
|
|
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
|
|
724
|
-
this[GQL_ELEMENT_FACTORY] = createLazyEvaluator(define
|
|
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
|
|
798
|
-
super(define
|
|
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
|
|
817
|
-
return new Fragment(define
|
|
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
|
|
838
|
-
super(define
|
|
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
|
|
873
|
-
return new Operation(define
|
|
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);
|
|
@@ -1048,6 +1068,7 @@ const createOperationComposerFactory = (schema, adapter, transformDocument) => {
|
|
|
1048
1068
|
if (!fragmentUsages.some((u) => u.metadataBuilder) && !options.metadata && !transformDocument && !options.transformDocument) return {
|
|
1049
1069
|
operationType,
|
|
1050
1070
|
operationName,
|
|
1071
|
+
schemaLabel: schema.label,
|
|
1051
1072
|
variableNames,
|
|
1052
1073
|
documentSource: () => fields,
|
|
1053
1074
|
document,
|
|
@@ -1088,6 +1109,7 @@ const createOperationComposerFactory = (schema, adapter, transformDocument) => {
|
|
|
1088
1109
|
return {
|
|
1089
1110
|
operationType,
|
|
1090
1111
|
operationName,
|
|
1112
|
+
schemaLabel: schema.label,
|
|
1091
1113
|
variableNames,
|
|
1092
1114
|
documentSource: () => fields,
|
|
1093
1115
|
document: finalDocument,
|
|
@@ -1307,7 +1329,7 @@ const createVarBuilder = (inputTypeMethods) => {
|
|
|
1307
1329
|
const varBuilder = (varName) => {
|
|
1308
1330
|
const wrappedMethods = {};
|
|
1309
1331
|
for (const [typeName, method] of Object.entries(inputTypeMethods)) Object.defineProperty(wrappedMethods, typeName, {
|
|
1310
|
-
value: ((modifier, extras) =>
|
|
1332
|
+
value: ((modifier, extras) => require_schema_builder.wrapByKey(varName, method(modifier, extras))),
|
|
1311
1333
|
writable: false,
|
|
1312
1334
|
configurable: true
|
|
1313
1335
|
});
|
|
@@ -1377,16 +1399,313 @@ const createGqlElementComposer = (schema, options) => {
|
|
|
1377
1399
|
return elementComposer;
|
|
1378
1400
|
};
|
|
1379
1401
|
|
|
1402
|
+
//#endregion
|
|
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 graphql.Kind.NON_NULL_TYPE: return graphqlTypeToTypeScript(schema, typeNode.type, formatters);
|
|
1624
|
+
case graphql.Kind.LIST_TYPE: return `(${graphqlTypeToTypeScript(schema, typeNode.type, formatters)})[]`;
|
|
1625
|
+
case graphql.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 === graphql.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
|
+
|
|
1380
1695
|
//#endregion
|
|
1381
1696
|
exports.Fragment = Fragment;
|
|
1382
1697
|
exports.GqlElement = GqlElement;
|
|
1383
1698
|
exports.Operation = Operation;
|
|
1699
|
+
exports.VarRef = VarRef;
|
|
1384
1700
|
exports.appendToPath = appendToPath;
|
|
1701
|
+
exports.applyTypeModifier = applyTypeModifier;
|
|
1385
1702
|
exports.buildArgumentValue = buildArgumentValue;
|
|
1386
1703
|
exports.buildConstValueNode = buildConstValueNode;
|
|
1387
1704
|
exports.buildDocument = buildDocument;
|
|
1388
1705
|
exports.buildOperationTypeNode = buildOperationTypeNode;
|
|
1389
1706
|
exports.buildWithTypeModifier = buildWithTypeModifier;
|
|
1707
|
+
exports.calculateFieldType = calculateFieldType;
|
|
1708
|
+
exports.calculateFieldsType = calculateFieldsType;
|
|
1390
1709
|
exports.createColocateHelper = createColocateHelper;
|
|
1391
1710
|
exports.createDefaultAdapter = createDefaultAdapter;
|
|
1392
1711
|
exports.createDirectiveBuilder = createDirectiveBuilder;
|
|
@@ -1395,22 +1714,29 @@ exports.createFieldFactories = createFieldFactories;
|
|
|
1395
1714
|
exports.createGqlElementComposer = createGqlElementComposer;
|
|
1396
1715
|
exports.createGqlFragmentComposers = createGqlFragmentComposers;
|
|
1397
1716
|
exports.createOperationComposerFactory = createOperationComposerFactory;
|
|
1717
|
+
exports.createPrebuiltGqlElementComposer = createPrebuiltGqlElementComposer;
|
|
1398
1718
|
exports.createStandardDirectives = createStandardDirectives;
|
|
1399
1719
|
exports.createVarAssignments = createVarAssignments;
|
|
1400
1720
|
exports.createVarBuilder = createVarBuilder;
|
|
1401
1721
|
exports.createVarMethod = createVarMethod;
|
|
1402
1722
|
exports.createVarMethodFactory = createVarMethodFactory;
|
|
1723
|
+
exports.createVarRefFromVariable = createVarRefFromVariable;
|
|
1403
1724
|
exports.createVarRefs = createVarRefs;
|
|
1404
1725
|
exports.defaultMetadataAdapter = defaultMetadataAdapter;
|
|
1405
|
-
exports.
|
|
1406
|
-
exports.defineOperationRoots =
|
|
1407
|
-
exports.defineScalar =
|
|
1726
|
+
exports.defineEnum = require_schema_builder.defineEnum;
|
|
1727
|
+
exports.defineOperationRoots = require_schema_builder.defineOperationRoots;
|
|
1728
|
+
exports.defineScalar = require_schema_builder.defineScalar;
|
|
1729
|
+
exports.generateInputObjectType = generateInputObjectType;
|
|
1730
|
+
exports.generateInputType = generateInputType;
|
|
1731
|
+
exports.generateInputTypeFromSpecifiers = generateInputTypeFromSpecifiers;
|
|
1408
1732
|
exports.getCurrentFieldPath = getCurrentFieldPath;
|
|
1733
|
+
exports.getEnumType = getEnumType;
|
|
1734
|
+
exports.getScalarInputType = getScalarInputType;
|
|
1735
|
+
exports.getScalarOutputType = getScalarOutputType;
|
|
1736
|
+
exports.graphqlTypeToTypeScript = graphqlTypeToTypeScript;
|
|
1409
1737
|
exports.isDirectiveRef = isDirectiveRef;
|
|
1410
1738
|
exports.isListType = isListType;
|
|
1411
1739
|
exports.recordFragmentUsage = recordFragmentUsage;
|
|
1412
|
-
exports.unsafeInputType = require_schema.unsafeInputType;
|
|
1413
|
-
exports.unsafeOutputType = require_schema.unsafeOutputType;
|
|
1414
1740
|
exports.withFieldPath = withFieldPath;
|
|
1415
1741
|
exports.withFragmentUsageCollection = withFragmentUsageCollection;
|
|
1416
1742
|
//# sourceMappingURL=index.cjs.map
|