@soda-gql/core 0.8.0 → 0.9.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 +1 -1
- package/dist/adapter.d.cts +2 -2
- package/dist/adapter.d.ts +2 -2
- package/dist/adapter.js +1 -1
- package/dist/{index-rBHv6RJz.d.ts → index-Ib9pb2Si.d.cts} +565 -35
- package/dist/index-Ib9pb2Si.d.cts.map +1 -0
- package/dist/{index-x4fr7bdv.d.cts → index-wkJ6KSwK.d.ts} +565 -35
- package/dist/index-wkJ6KSwK.d.ts.map +1 -0
- package/dist/index.cjs +428 -35
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +43 -4
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +43 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +425 -36
- package/dist/index.js.map +1 -1
- package/dist/runtime.cjs +16 -10
- package/dist/runtime.cjs.map +1 -1
- package/dist/runtime.d.cts +2 -2
- package/dist/runtime.d.ts +2 -2
- package/dist/runtime.js +16 -10
- package/dist/runtime.js.map +1 -1
- package/dist/{schema-CFMmSBhl.d.ts → schema-2qqtKss4.d.ts} +39 -37
- package/dist/schema-2qqtKss4.d.ts.map +1 -0
- package/dist/{schema-BbCrsNkQ.js → schema-BiYcVVvm.js} +86 -1
- package/dist/schema-BiYcVVvm.js.map +1 -0
- package/dist/{schema-C1lnWj-m.d.cts → schema-CPTxQbTv.d.cts} +39 -37
- package/dist/schema-CPTxQbTv.d.cts.map +1 -0
- package/dist/{schema-DuWaRhdp.cjs → schema-D2MW4DOF.cjs} +86 -1
- package/dist/schema-D2MW4DOF.cjs.map +1 -0
- package/dist/schema-builder-BYJd50o2.d.cts +97 -0
- package/dist/schema-builder-BYJd50o2.d.cts.map +1 -0
- package/dist/schema-builder-Dhss2O1I.d.ts +97 -0
- package/dist/schema-builder-Dhss2O1I.d.ts.map +1 -0
- package/package.json +1 -1
- package/dist/index-rBHv6RJz.d.ts.map +0 -1
- package/dist/index-x4fr7bdv.d.cts.map +0 -1
- package/dist/schema-BbCrsNkQ.js.map +0 -1
- package/dist/schema-C1lnWj-m.d.cts.map +0 -1
- package/dist/schema-CFMmSBhl.d.ts.map +0 -1
- package/dist/schema-DuWaRhdp.cjs.map +0 -1
- package/dist/schema-builder-CQa-dOw9.d.ts +0 -40
- package/dist/schema-builder-CQa-dOw9.d.ts.map +0 -1
- package/dist/schema-builder-QkY6hZn6.d.cts +0 -40
- package/dist/schema-builder-QkY6hZn6.d.cts.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,37 @@
|
|
|
1
|
-
const require_schema = require('./schema-
|
|
1
|
+
const require_schema = require('./schema-D2MW4DOF.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
|
|
4
35
|
//#region packages/core/src/types/type-foundation/var-ref.ts
|
|
5
36
|
var VarRef = class {
|
|
6
37
|
constructor(inner) {
|
|
@@ -33,6 +64,12 @@ function createVarRefFromNestedValue(value) {
|
|
|
33
64
|
|
|
34
65
|
//#endregion
|
|
35
66
|
//#region packages/core/src/composer/build-document.ts
|
|
67
|
+
/**
|
|
68
|
+
* Converts an assignable input value to a GraphQL AST ValueNode.
|
|
69
|
+
*
|
|
70
|
+
* Handles primitives, arrays, objects, and variable references.
|
|
71
|
+
* Returns null for undefined values (field is omitted).
|
|
72
|
+
*/
|
|
36
73
|
const buildArgumentValue = (value) => {
|
|
37
74
|
if (value === void 0) return null;
|
|
38
75
|
if (value === null) return { kind: graphql.Kind.NULL };
|
|
@@ -91,6 +128,41 @@ const buildArguments = (args) => Object.entries(args ?? {}).map(([name, value])
|
|
|
91
128
|
value: valueNode
|
|
92
129
|
} : null;
|
|
93
130
|
}).filter((item) => item !== null);
|
|
131
|
+
/**
|
|
132
|
+
* Validates that a directive can be used at the specified location.
|
|
133
|
+
*
|
|
134
|
+
* @param directive - The directive reference to validate
|
|
135
|
+
* @param expectedLocation - The location where the directive is being used
|
|
136
|
+
* @throws Error if the directive is not valid at the specified location
|
|
137
|
+
*/
|
|
138
|
+
const validateDirectiveLocation = (directive, expectedLocation) => {
|
|
139
|
+
const inner = DirectiveRef.getInner(directive);
|
|
140
|
+
if (!inner.locations.includes(expectedLocation)) throw new Error(`Directive @${inner.name} cannot be used on ${expectedLocation}. Valid locations: ${inner.locations.join(", ")}`);
|
|
141
|
+
};
|
|
142
|
+
/**
|
|
143
|
+
* Builds DirectiveNode array from field directives.
|
|
144
|
+
*
|
|
145
|
+
* Filters for DirectiveRef instances, validates their locations,
|
|
146
|
+
* and converts them to GraphQL AST DirectiveNode objects.
|
|
147
|
+
*
|
|
148
|
+
* @param directives - Array of directive references (or unknown values)
|
|
149
|
+
* @param location - The location context for validation
|
|
150
|
+
* @returns Array of DirectiveNode for the GraphQL AST
|
|
151
|
+
*/
|
|
152
|
+
const buildDirectives = (directives, location) => {
|
|
153
|
+
return directives.filter((d) => d instanceof DirectiveRef).map((directive) => {
|
|
154
|
+
validateDirectiveLocation(directive, location);
|
|
155
|
+
const inner = DirectiveRef.getInner(directive);
|
|
156
|
+
return {
|
|
157
|
+
kind: graphql.Kind.DIRECTIVE,
|
|
158
|
+
name: {
|
|
159
|
+
kind: graphql.Kind.NAME,
|
|
160
|
+
value: inner.name
|
|
161
|
+
},
|
|
162
|
+
arguments: buildArguments(inner.arguments)
|
|
163
|
+
};
|
|
164
|
+
});
|
|
165
|
+
};
|
|
94
166
|
const buildUnionSelection = (union) => Object.entries(union).map(([typeName, object]) => {
|
|
95
167
|
return object ? {
|
|
96
168
|
kind: graphql.Kind.INLINE_FRAGMENT,
|
|
@@ -107,25 +179,35 @@ const buildUnionSelection = (union) => Object.entries(union).map(([typeName, obj
|
|
|
107
179
|
}
|
|
108
180
|
} : null;
|
|
109
181
|
}).filter((item) => item !== null);
|
|
110
|
-
const buildField = (field) => Object.entries(field).map(([alias, { args, field: field$1, object, union }]) =>
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
kind: graphql.Kind.
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
182
|
+
const buildField = (field) => Object.entries(field).map(([alias, { args, field: field$1, object, union, directives }]) => {
|
|
183
|
+
const builtDirectives = buildDirectives(directives, "FIELD");
|
|
184
|
+
return {
|
|
185
|
+
kind: graphql.Kind.FIELD,
|
|
186
|
+
name: {
|
|
187
|
+
kind: graphql.Kind.NAME,
|
|
188
|
+
value: field$1
|
|
189
|
+
},
|
|
190
|
+
alias: alias !== field$1 ? {
|
|
191
|
+
kind: graphql.Kind.NAME,
|
|
192
|
+
value: alias
|
|
193
|
+
} : void 0,
|
|
194
|
+
arguments: buildArguments(args),
|
|
195
|
+
directives: builtDirectives.length > 0 ? builtDirectives : void 0,
|
|
196
|
+
selectionSet: object ? {
|
|
197
|
+
kind: graphql.Kind.SELECTION_SET,
|
|
198
|
+
selections: buildField(object)
|
|
199
|
+
} : union ? {
|
|
200
|
+
kind: graphql.Kind.SELECTION_SET,
|
|
201
|
+
selections: buildUnionSelection(union)
|
|
202
|
+
} : void 0
|
|
203
|
+
};
|
|
204
|
+
});
|
|
205
|
+
/**
|
|
206
|
+
* Converts a constant value to a GraphQL AST ConstValueNode.
|
|
207
|
+
*
|
|
208
|
+
* Unlike `buildArgumentValue`, this only handles literal values
|
|
209
|
+
* (no variable references). Used for default values.
|
|
210
|
+
*/
|
|
129
211
|
const buildConstValueNode = (value) => {
|
|
130
212
|
if (value === void 0) return null;
|
|
131
213
|
if (value === null) return { kind: graphql.Kind.NULL };
|
|
@@ -161,6 +243,18 @@ const buildConstValueNode = (value) => {
|
|
|
161
243
|
};
|
|
162
244
|
throw new Error(`Unknown value type: ${typeof value}`);
|
|
163
245
|
};
|
|
246
|
+
/**
|
|
247
|
+
* Wraps a named type with modifiers (non-null, list).
|
|
248
|
+
*
|
|
249
|
+
* Modifier format: starts with `?` (nullable) or `!` (non-null),
|
|
250
|
+
* followed by `[]?` or `[]!` pairs for lists.
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* - `"!"` → `String!`
|
|
254
|
+
* - `"?"` → `String`
|
|
255
|
+
* - `"![]!"` → `[String!]!`
|
|
256
|
+
* - `"?[]?"` → `[String]`
|
|
257
|
+
*/
|
|
164
258
|
const buildWithTypeModifier = (modifier, buildType) => {
|
|
165
259
|
const baseType = buildType();
|
|
166
260
|
if (modifier === "?") return baseType;
|
|
@@ -238,6 +332,9 @@ const buildVariables = (variables) => {
|
|
|
238
332
|
}))
|
|
239
333
|
}));
|
|
240
334
|
};
|
|
335
|
+
/**
|
|
336
|
+
* Converts an operation type string to a GraphQL AST OperationTypeNode.
|
|
337
|
+
*/
|
|
241
338
|
const buildOperationTypeNode = (operation) => {
|
|
242
339
|
switch (operation) {
|
|
243
340
|
case "query": return graphql.OperationTypeNode.QUERY;
|
|
@@ -246,6 +343,16 @@ const buildOperationTypeNode = (operation) => {
|
|
|
246
343
|
default: throw new Error(`Unknown operation type: ${operation}`);
|
|
247
344
|
}
|
|
248
345
|
};
|
|
346
|
+
/**
|
|
347
|
+
* Builds a TypedDocumentNode from operation options.
|
|
348
|
+
*
|
|
349
|
+
* This is the main entry point for converting field selections into
|
|
350
|
+
* a GraphQL document AST. The result can be used with any GraphQL
|
|
351
|
+
* client that supports TypedDocumentNode.
|
|
352
|
+
*
|
|
353
|
+
* @param options - Operation configuration (name, type, variables, fields)
|
|
354
|
+
* @returns TypedDocumentNode with inferred input/output types
|
|
355
|
+
*/
|
|
249
356
|
const buildDocument = (options) => {
|
|
250
357
|
const { operationName, operationType, variables, fields } = options;
|
|
251
358
|
return {
|
|
@@ -304,6 +411,85 @@ const createColocateHelper = () => {
|
|
|
304
411
|
return $colocate;
|
|
305
412
|
};
|
|
306
413
|
|
|
414
|
+
//#endregion
|
|
415
|
+
//#region packages/core/src/composer/directive-builder.ts
|
|
416
|
+
/**
|
|
417
|
+
* Directive builder utilities for creating field-level directives.
|
|
418
|
+
*
|
|
419
|
+
* Provides type-safe methods for creating directive references that can be
|
|
420
|
+
* applied to field selections. The builder follows a similar pattern to
|
|
421
|
+
* the variable builder ($var).
|
|
422
|
+
*
|
|
423
|
+
* @module
|
|
424
|
+
*/
|
|
425
|
+
/**
|
|
426
|
+
* Creates a directive method factory for a specific directive.
|
|
427
|
+
*
|
|
428
|
+
* @param name - The directive name (without @)
|
|
429
|
+
* @param locations - Valid locations where the directive can be applied
|
|
430
|
+
* @returns A function that creates DirectiveRef instances
|
|
431
|
+
*
|
|
432
|
+
* @example
|
|
433
|
+
* ```typescript
|
|
434
|
+
* const skipMethod = createDirectiveMethod("skip", ["FIELD", "FRAGMENT_SPREAD", "INLINE_FRAGMENT"] as const);
|
|
435
|
+
* const skipDirective = skipMethod({ if: true });
|
|
436
|
+
* ```
|
|
437
|
+
*/
|
|
438
|
+
const createDirectiveMethod = (name, locations) => {
|
|
439
|
+
return (args) => new DirectiveRef({
|
|
440
|
+
name,
|
|
441
|
+
arguments: args,
|
|
442
|
+
locations
|
|
443
|
+
});
|
|
444
|
+
};
|
|
445
|
+
/**
|
|
446
|
+
* Standard directive locations for @skip and @include.
|
|
447
|
+
*/
|
|
448
|
+
const CONDITIONAL_DIRECTIVE_LOCATIONS = [
|
|
449
|
+
"FIELD",
|
|
450
|
+
"FRAGMENT_SPREAD",
|
|
451
|
+
"INLINE_FRAGMENT"
|
|
452
|
+
];
|
|
453
|
+
/**
|
|
454
|
+
* Creates the standard GraphQL directives (@skip, @include).
|
|
455
|
+
* These are always available regardless of schema definition.
|
|
456
|
+
*
|
|
457
|
+
* @returns Object containing skip and include directive methods
|
|
458
|
+
*
|
|
459
|
+
* @example
|
|
460
|
+
* ```typescript
|
|
461
|
+
* const $dir = createStandardDirectives();
|
|
462
|
+
* const skipDirective = $dir.skip({ if: true });
|
|
463
|
+
* ```
|
|
464
|
+
*/
|
|
465
|
+
const createStandardDirectives = () => ({
|
|
466
|
+
skip: createDirectiveMethod("skip", CONDITIONAL_DIRECTIVE_LOCATIONS),
|
|
467
|
+
include: createDirectiveMethod("include", CONDITIONAL_DIRECTIVE_LOCATIONS)
|
|
468
|
+
});
|
|
469
|
+
/**
|
|
470
|
+
* Creates a directive builder with standard directives and optional custom directives.
|
|
471
|
+
*
|
|
472
|
+
* @param customDirectives - Additional directive methods from schema (generated by codegen)
|
|
473
|
+
* @returns Combined directive builder with all available directives
|
|
474
|
+
*
|
|
475
|
+
* @internal Used by codegen to create schema-specific directive builders
|
|
476
|
+
*/
|
|
477
|
+
const createDirectiveBuilder = (customDirectives) => {
|
|
478
|
+
return {
|
|
479
|
+
...createStandardDirectives(),
|
|
480
|
+
...customDirectives ?? {}
|
|
481
|
+
};
|
|
482
|
+
};
|
|
483
|
+
/**
|
|
484
|
+
* Type guard to check if a value is a DirectiveRef.
|
|
485
|
+
*
|
|
486
|
+
* @param value - Value to check
|
|
487
|
+
* @returns True if value is a DirectiveRef instance
|
|
488
|
+
*/
|
|
489
|
+
const isDirectiveRef = (value) => {
|
|
490
|
+
return value instanceof DirectiveRef;
|
|
491
|
+
};
|
|
492
|
+
|
|
307
493
|
//#endregion
|
|
308
494
|
//#region packages/core/src/composer/field-path-context.ts
|
|
309
495
|
/**
|
|
@@ -387,6 +573,18 @@ const ensureCacheMapBySchema = (schema) => {
|
|
|
387
573
|
cacheMapBySchema.set(schema, cacheMap);
|
|
388
574
|
return cacheMap;
|
|
389
575
|
};
|
|
576
|
+
/**
|
|
577
|
+
* Creates field selection factories for a given object type.
|
|
578
|
+
*
|
|
579
|
+
* Returns an object with a factory for each field defined on the type.
|
|
580
|
+
* Factories are cached per schema+type to avoid recreation.
|
|
581
|
+
*
|
|
582
|
+
* @param schema - The GraphQL schema definition
|
|
583
|
+
* @param typeName - The object type name to create factories for
|
|
584
|
+
* @returns Object mapping field names to their selection factories
|
|
585
|
+
*
|
|
586
|
+
* @internal Used by operation and fragment composers
|
|
587
|
+
*/
|
|
390
588
|
const createFieldFactories = (schema, typeName) => {
|
|
391
589
|
const cacheMap = ensureCacheMapBySchema(schema);
|
|
392
590
|
const cached = cacheMap.get(typeName);
|
|
@@ -401,6 +599,7 @@ const createFieldFactoriesInner = (schema, typeName) => {
|
|
|
401
599
|
const entries = Object.entries(typeDef.fields).map(([fieldName, type]) => {
|
|
402
600
|
const factory = (fieldArgs, extras) => {
|
|
403
601
|
const wrap = (value) => require_schema.wrapByKey(extras?.alias ?? fieldName, value);
|
|
602
|
+
const directives = extras?.directives ?? [];
|
|
404
603
|
if (type.kind === "object") {
|
|
405
604
|
const factoryReturn = ((nest) => {
|
|
406
605
|
const nestedFields = withFieldPath(appendToPath(getCurrentFieldPath(), {
|
|
@@ -413,7 +612,7 @@ const createFieldFactoriesInner = (schema, typeName) => {
|
|
|
413
612
|
field: fieldName,
|
|
414
613
|
type,
|
|
415
614
|
args: fieldArgs ?? {},
|
|
416
|
-
directives
|
|
615
|
+
directives,
|
|
417
616
|
object: nestedFields,
|
|
418
617
|
union: null
|
|
419
618
|
});
|
|
@@ -435,7 +634,7 @@ const createFieldFactoriesInner = (schema, typeName) => {
|
|
|
435
634
|
field: fieldName,
|
|
436
635
|
type,
|
|
437
636
|
args: fieldArgs ?? {},
|
|
438
|
-
directives
|
|
637
|
+
directives,
|
|
439
638
|
object: null,
|
|
440
639
|
union: nestedUnion
|
|
441
640
|
});
|
|
@@ -447,7 +646,7 @@ const createFieldFactoriesInner = (schema, typeName) => {
|
|
|
447
646
|
field: fieldName,
|
|
448
647
|
type,
|
|
449
648
|
args: fieldArgs ?? {},
|
|
450
|
-
directives
|
|
649
|
+
directives,
|
|
451
650
|
object: null,
|
|
452
651
|
union: null
|
|
453
652
|
});
|
|
@@ -509,6 +708,15 @@ const evaluateSync = (executor, context) => {
|
|
|
509
708
|
//#region packages/core/src/types/element/gql-element.ts
|
|
510
709
|
const GQL_ELEMENT_FACTORY = Symbol("GQL_ELEMENT_FACTORY");
|
|
511
710
|
const GQL_ELEMENT_CONTEXT = Symbol("GQL_ELEMENT_CONTEXT");
|
|
711
|
+
/**
|
|
712
|
+
* Abstract base class for all GraphQL elements (Fragment, Operation).
|
|
713
|
+
*
|
|
714
|
+
* Uses lazy evaluation with caching - definition is computed on first access.
|
|
715
|
+
* Subclasses should not be instantiated directly; use static `create` methods.
|
|
716
|
+
*
|
|
717
|
+
* @template TDefinition - The shape of the evaluated definition
|
|
718
|
+
* @template TInfer - Type inference metadata (access via `$infer`)
|
|
719
|
+
*/
|
|
512
720
|
var GqlElement = class GqlElement {
|
|
513
721
|
[GQL_ELEMENT_FACTORY];
|
|
514
722
|
[GQL_ELEMENT_CONTEXT] = null;
|
|
@@ -518,30 +726,55 @@ var GqlElement = class GqlElement {
|
|
|
518
726
|
throw new Error("This property is only for type meta. Do not access this property directly.");
|
|
519
727
|
} });
|
|
520
728
|
}
|
|
521
|
-
attach(
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
729
|
+
attach(attachmentOrAttachments) {
|
|
730
|
+
const attachments = Array.isArray(attachmentOrAttachments) ? attachmentOrAttachments : [attachmentOrAttachments];
|
|
731
|
+
for (const attachment of attachments) {
|
|
732
|
+
let cache = null;
|
|
733
|
+
const self = this;
|
|
734
|
+
Object.defineProperty(this, attachment.name, { get() {
|
|
735
|
+
if (cache) return cache;
|
|
736
|
+
GqlElement.evaluateInstantly(self);
|
|
737
|
+
return cache = attachment.createValue(self);
|
|
738
|
+
} });
|
|
739
|
+
}
|
|
528
740
|
return this;
|
|
529
741
|
}
|
|
742
|
+
/**
|
|
743
|
+
* Sets the canonical context for an element. Used by the builder.
|
|
744
|
+
* @internal
|
|
745
|
+
*/
|
|
530
746
|
static setContext(element, context) {
|
|
531
747
|
element[GQL_ELEMENT_CONTEXT] = context;
|
|
532
748
|
}
|
|
749
|
+
/**
|
|
750
|
+
* Gets the canonical context of an element, if set.
|
|
751
|
+
* @internal
|
|
752
|
+
*/
|
|
533
753
|
static getContext(element) {
|
|
534
754
|
return element[GQL_ELEMENT_CONTEXT];
|
|
535
755
|
}
|
|
756
|
+
/**
|
|
757
|
+
* Creates a generator for async evaluation. Used by the builder.
|
|
758
|
+
* @internal
|
|
759
|
+
*/
|
|
536
760
|
static createEvaluationGenerator(element) {
|
|
537
761
|
return createEvaluationGenerator(element[GQL_ELEMENT_FACTORY], element[GQL_ELEMENT_CONTEXT]);
|
|
538
762
|
}
|
|
539
763
|
static evaluateInstantly(element) {
|
|
540
764
|
return evaluateSync(element[GQL_ELEMENT_FACTORY], element[GQL_ELEMENT_CONTEXT]);
|
|
541
765
|
}
|
|
766
|
+
/**
|
|
767
|
+
* Forces synchronous evaluation. Throws if async operation is needed.
|
|
768
|
+
* @internal
|
|
769
|
+
*/
|
|
542
770
|
static evaluateSync(element) {
|
|
543
771
|
GqlElement.evaluateInstantly(element);
|
|
544
772
|
}
|
|
773
|
+
/**
|
|
774
|
+
* Evaluates and returns the element's definition.
|
|
775
|
+
* Throws if async operation is needed.
|
|
776
|
+
* @internal
|
|
777
|
+
*/
|
|
545
778
|
static get(element) {
|
|
546
779
|
return GqlElement.evaluateInstantly(element);
|
|
547
780
|
}
|
|
@@ -549,16 +782,37 @@ var GqlElement = class GqlElement {
|
|
|
549
782
|
|
|
550
783
|
//#endregion
|
|
551
784
|
//#region packages/core/src/types/element/fragment.ts
|
|
785
|
+
/**
|
|
786
|
+
* Represents a reusable GraphQL field selection on a specific type.
|
|
787
|
+
*
|
|
788
|
+
* Fragments are created via `gql(({ fragment }) => fragment.TypeName({ ... }))`.
|
|
789
|
+
* Use `spread()` to include the fragment's fields in an operation.
|
|
790
|
+
*
|
|
791
|
+
* @template TTypeName - The GraphQL type this fragment selects from
|
|
792
|
+
* @template TVariables - Variables required when spreading
|
|
793
|
+
* @template TFields - The selected fields structure
|
|
794
|
+
* @template TOutput - Inferred output type from selected fields
|
|
795
|
+
*/
|
|
552
796
|
var Fragment = class Fragment extends GqlElement {
|
|
553
797
|
constructor(define$1) {
|
|
554
798
|
super(define$1);
|
|
555
799
|
}
|
|
800
|
+
/** The GraphQL type name this fragment selects from. */
|
|
556
801
|
get typename() {
|
|
557
802
|
return GqlElement.get(this).typename;
|
|
558
803
|
}
|
|
804
|
+
/**
|
|
805
|
+
* Spreads this fragment's fields into a parent selection.
|
|
806
|
+
* Pass variables if the fragment defines any.
|
|
807
|
+
*/
|
|
559
808
|
get spread() {
|
|
560
809
|
return GqlElement.get(this).spread;
|
|
561
810
|
}
|
|
811
|
+
/**
|
|
812
|
+
* Creates a new Fragment instance.
|
|
813
|
+
* Prefer using the `gql(({ fragment }) => ...)` API instead.
|
|
814
|
+
* @internal
|
|
815
|
+
*/
|
|
562
816
|
static create(define$1) {
|
|
563
817
|
return new Fragment(define$1);
|
|
564
818
|
}
|
|
@@ -566,28 +820,55 @@ var Fragment = class Fragment extends GqlElement {
|
|
|
566
820
|
|
|
567
821
|
//#endregion
|
|
568
822
|
//#region packages/core/src/types/element/operation.ts
|
|
823
|
+
/**
|
|
824
|
+
* Represents a GraphQL operation (query, mutation, or subscription).
|
|
825
|
+
*
|
|
826
|
+
* Operations are created via `gql(({ query }) => query.operation({ ... }))`.
|
|
827
|
+
* Produces a TypedDocumentNode for type-safe execution with GraphQL clients.
|
|
828
|
+
*
|
|
829
|
+
* @template TOperationType - 'query' | 'mutation' | 'subscription'
|
|
830
|
+
* @template TOperationName - The unique operation name
|
|
831
|
+
* @template TVariableNames - Tuple of variable names
|
|
832
|
+
* @template TVariables - Variable types for the operation
|
|
833
|
+
* @template TFields - Selected fields structure
|
|
834
|
+
* @template TData - Inferred response data type
|
|
835
|
+
*/
|
|
569
836
|
var Operation = class Operation extends GqlElement {
|
|
570
837
|
constructor(define$1) {
|
|
571
838
|
super(define$1);
|
|
572
839
|
}
|
|
840
|
+
/** The operation type: 'query', 'mutation', or 'subscription'. */
|
|
573
841
|
get operationType() {
|
|
574
842
|
return GqlElement.get(this).operationType;
|
|
575
843
|
}
|
|
844
|
+
/** The unique name of this operation. */
|
|
576
845
|
get operationName() {
|
|
577
846
|
return GqlElement.get(this).operationName;
|
|
578
847
|
}
|
|
848
|
+
/** List of variable names defined for this operation. */
|
|
579
849
|
get variableNames() {
|
|
580
850
|
return GqlElement.get(this).variableNames;
|
|
581
851
|
}
|
|
852
|
+
/**
|
|
853
|
+
* Returns the field selections. Used for document reconstruction.
|
|
854
|
+
* @internal
|
|
855
|
+
*/
|
|
582
856
|
get documentSource() {
|
|
583
857
|
return GqlElement.get(this).documentSource;
|
|
584
858
|
}
|
|
859
|
+
/** The TypedDocumentNode for use with GraphQL clients. */
|
|
585
860
|
get document() {
|
|
586
861
|
return GqlElement.get(this).document;
|
|
587
862
|
}
|
|
863
|
+
/** Custom metadata attached to this operation, if any. */
|
|
588
864
|
get metadata() {
|
|
589
865
|
return GqlElement.get(this).metadata;
|
|
590
866
|
}
|
|
867
|
+
/**
|
|
868
|
+
* Creates a new Operation instance.
|
|
869
|
+
* Prefer using the `gql(({ query }) => ...)` API instead.
|
|
870
|
+
* @internal
|
|
871
|
+
*/
|
|
591
872
|
static create(define$1) {
|
|
592
873
|
return new Operation(define$1);
|
|
593
874
|
}
|
|
@@ -631,6 +912,21 @@ const recordFragmentUsage = (record) => {
|
|
|
631
912
|
|
|
632
913
|
//#endregion
|
|
633
914
|
//#region packages/core/src/composer/input.ts
|
|
915
|
+
/**
|
|
916
|
+
* Utilities for creating variable assignments and references.
|
|
917
|
+
* @module
|
|
918
|
+
*/
|
|
919
|
+
/**
|
|
920
|
+
* Creates variable assignments from provided values.
|
|
921
|
+
*
|
|
922
|
+
* Maps variable definitions to VarRefs. If a value is provided,
|
|
923
|
+
* wraps it as a nested-value VarRef. If not provided, creates
|
|
924
|
+
* an undefined VarRef (field will be omitted).
|
|
925
|
+
*
|
|
926
|
+
* Used when spreading fragments with partial variable values.
|
|
927
|
+
*
|
|
928
|
+
* @internal
|
|
929
|
+
*/
|
|
634
930
|
const createVarAssignments = (definitions, providedValues) => {
|
|
635
931
|
return mapValues(definitions, (_, key) => {
|
|
636
932
|
const varName = key;
|
|
@@ -640,10 +936,34 @@ const createVarAssignments = (definitions, providedValues) => {
|
|
|
640
936
|
return createVarRefFromNestedValue(provided);
|
|
641
937
|
});
|
|
642
938
|
};
|
|
939
|
+
/**
|
|
940
|
+
* Creates variable references from variable definitions.
|
|
941
|
+
*
|
|
942
|
+
* Maps each variable definition to a VarRef pointing to that variable.
|
|
943
|
+
* Used in operation builders to create the `$` context object.
|
|
944
|
+
*
|
|
945
|
+
* @internal
|
|
946
|
+
*/
|
|
643
947
|
const createVarRefs = (definitions) => mapValues(definitions, (_, name) => createVarRefFromVariable(name));
|
|
644
948
|
|
|
645
949
|
//#endregion
|
|
646
950
|
//#region packages/core/src/composer/fragment.ts
|
|
951
|
+
/**
|
|
952
|
+
* Fragment composer factory for creating reusable field selections.
|
|
953
|
+
* @module
|
|
954
|
+
*/
|
|
955
|
+
/**
|
|
956
|
+
* Creates fragment builder functions for all object types in the schema.
|
|
957
|
+
*
|
|
958
|
+
* Returns an object with a builder for each type (e.g., `fragment.User`, `fragment.Post`).
|
|
959
|
+
* Each builder creates a `Fragment` that can be spread into operations.
|
|
960
|
+
*
|
|
961
|
+
* @param schema - The GraphQL schema definition
|
|
962
|
+
* @param _adapter - Optional metadata adapter (for fragment metadata)
|
|
963
|
+
* @returns Object mapping type names to fragment builder functions
|
|
964
|
+
*
|
|
965
|
+
* @internal Used by `createGqlElementComposer`
|
|
966
|
+
*/
|
|
647
967
|
const createGqlFragmentComposers = (schema, _adapter) => {
|
|
648
968
|
const createFragmentComposer = (typename) => {
|
|
649
969
|
return (options) => {
|
|
@@ -683,6 +1003,25 @@ const defaultMetadataAdapter = createDefaultAdapter();
|
|
|
683
1003
|
|
|
684
1004
|
//#endregion
|
|
685
1005
|
//#region packages/core/src/composer/operation.ts
|
|
1006
|
+
/**
|
|
1007
|
+
* Operation composer factory for creating typed GraphQL operations.
|
|
1008
|
+
* @module
|
|
1009
|
+
*/
|
|
1010
|
+
/**
|
|
1011
|
+
* Creates a factory for composing GraphQL operations.
|
|
1012
|
+
*
|
|
1013
|
+
* Returns a curried function: first select operation type (query/mutation/subscription),
|
|
1014
|
+
* then define the operation with name, variables, and fields.
|
|
1015
|
+
*
|
|
1016
|
+
* Handles metadata aggregation from fragments (sync or async) and builds
|
|
1017
|
+
* the TypedDocumentNode automatically.
|
|
1018
|
+
*
|
|
1019
|
+
* @param schema - The GraphQL schema definition
|
|
1020
|
+
* @param adapter - Optional metadata adapter for custom metadata handling
|
|
1021
|
+
* @returns Operation type selector function
|
|
1022
|
+
*
|
|
1023
|
+
* @internal Used by `createGqlElementComposer`
|
|
1024
|
+
*/
|
|
686
1025
|
const createOperationComposerFactory = (schema, adapter) => {
|
|
687
1026
|
const resolvedAdapter = adapter ?? defaultMetadataAdapter;
|
|
688
1027
|
return (operationType) => {
|
|
@@ -744,7 +1083,9 @@ const createOperationComposerFactory = (schema, adapter) => {
|
|
|
744
1083
|
//#region packages/core/src/composer/var-ref-tools.ts
|
|
745
1084
|
/**
|
|
746
1085
|
* Recursively checks if a NestedValue contains any VarRef.
|
|
1086
|
+
*
|
|
747
1087
|
* Used by getVarRefValue to determine if it's safe to return as ConstValue.
|
|
1088
|
+
* @internal
|
|
748
1089
|
*/
|
|
749
1090
|
const hasVarRefInside = (value) => {
|
|
750
1091
|
if (value instanceof VarRef) return true;
|
|
@@ -856,6 +1197,18 @@ const getValueAt = (varRef, selector) => {
|
|
|
856
1197
|
if (hasVarRefInside(inner.varInner.value)) throw new Error(`Value at path [${inner.segments.join(".")}] contains nested VarRef`);
|
|
857
1198
|
return inner.varInner.value;
|
|
858
1199
|
};
|
|
1200
|
+
/**
|
|
1201
|
+
* Gets the full path to a variable within a nested structure.
|
|
1202
|
+
*
|
|
1203
|
+
* Returns path segments starting with `$variableName` followed by
|
|
1204
|
+
* property accesses within that variable's value.
|
|
1205
|
+
*
|
|
1206
|
+
* @example
|
|
1207
|
+
* ```typescript
|
|
1208
|
+
* getVariablePath($.filter, p => p.user.id)
|
|
1209
|
+
* // Returns: ["$filter", "user", "id"]
|
|
1210
|
+
* ```
|
|
1211
|
+
*/
|
|
859
1212
|
const getVariablePath = (varRef, selector) => {
|
|
860
1213
|
const inner = getSelectableProxyInner(selector(createSelectableProxy({
|
|
861
1214
|
varInner: VarRef.getInner(varRef),
|
|
@@ -908,7 +1261,16 @@ const createVarMethodFactory = () => {
|
|
|
908
1261
|
};
|
|
909
1262
|
};
|
|
910
1263
|
/**
|
|
911
|
-
* Creates a variable builder
|
|
1264
|
+
* Creates a variable builder using injected input type methods.
|
|
1265
|
+
*
|
|
1266
|
+
* The returned builder provides type-safe variable definition methods
|
|
1267
|
+
* for all input types in the schema. Also includes utilities for
|
|
1268
|
+
* extracting variable names and values from VarRefs.
|
|
1269
|
+
*
|
|
1270
|
+
* @param inputTypeMethods - Methods for each input type (from codegen)
|
|
1271
|
+
* @returns Variable builder with methods for all input types
|
|
1272
|
+
*
|
|
1273
|
+
* @internal Used by `createGqlElementComposer`
|
|
912
1274
|
*/
|
|
913
1275
|
const createVarBuilder = (inputTypeMethods) => {
|
|
914
1276
|
const varBuilder = (varName) => {
|
|
@@ -933,12 +1295,38 @@ const createVarBuilder = (inputTypeMethods) => {
|
|
|
933
1295
|
/**
|
|
934
1296
|
* Creates a GQL element composer for a given schema.
|
|
935
1297
|
*
|
|
936
|
-
*
|
|
937
|
-
*
|
|
938
|
-
*
|
|
1298
|
+
* This is the main entry point for defining GraphQL operations and fragments.
|
|
1299
|
+
* The returned function provides a context with:
|
|
1300
|
+
* - `fragment`: Builders for each object type
|
|
1301
|
+
* - `query/mutation/subscription`: Operation builders
|
|
1302
|
+
* - `$var`: Variable definition helpers
|
|
1303
|
+
* - `$dir`: Field directive helpers (@skip, @include)
|
|
1304
|
+
* - `$colocate`: Fragment colocation utilities
|
|
1305
|
+
*
|
|
1306
|
+
* @param schema - The GraphQL schema definition
|
|
1307
|
+
* @param options - Configuration including input type methods and optional adapter
|
|
1308
|
+
* @returns Element composer function
|
|
1309
|
+
*
|
|
1310
|
+
* @example
|
|
1311
|
+
* ```typescript
|
|
1312
|
+
* const gql = createGqlElementComposer(schema, { inputTypeMethods });
|
|
1313
|
+
*
|
|
1314
|
+
* const GetUser = gql(({ query, $var, $dir }) =>
|
|
1315
|
+
* query.operation({
|
|
1316
|
+
* name: "GetUser",
|
|
1317
|
+
* variables: { showEmail: $var("showEmail").Boolean("!") },
|
|
1318
|
+
* fields: ({ f, $ }) => ({
|
|
1319
|
+
* ...f.user({ id: "1" })(({ f }) => ({
|
|
1320
|
+
* ...f.name(),
|
|
1321
|
+
* ...f.email({}, { directives: [$dir.skip({ if: $.showEmail })] }),
|
|
1322
|
+
* })),
|
|
1323
|
+
* }),
|
|
1324
|
+
* })
|
|
1325
|
+
* );
|
|
1326
|
+
* ```
|
|
939
1327
|
*/
|
|
940
1328
|
const createGqlElementComposer = (schema, options) => {
|
|
941
|
-
const { adapter, inputTypeMethods } = options;
|
|
1329
|
+
const { adapter, inputTypeMethods, directiveMethods } = options;
|
|
942
1330
|
const helpers = adapter?.helpers;
|
|
943
1331
|
const metadataAdapter = adapter?.metadata;
|
|
944
1332
|
const fragment = createGqlFragmentComposers(schema, metadataAdapter);
|
|
@@ -949,6 +1337,7 @@ const createGqlElementComposer = (schema, options) => {
|
|
|
949
1337
|
mutation: { operation: createOperationComposer("mutation") },
|
|
950
1338
|
subscription: { operation: createOperationComposer("subscription") },
|
|
951
1339
|
$var: createVarBuilder(inputTypeMethods),
|
|
1340
|
+
$dir: directiveMethods ?? createStandardDirectives(),
|
|
952
1341
|
$colocate: createColocateHelper(),
|
|
953
1342
|
...helpers ?? {}
|
|
954
1343
|
};
|
|
@@ -968,10 +1357,13 @@ exports.buildOperationTypeNode = buildOperationTypeNode;
|
|
|
968
1357
|
exports.buildWithTypeModifier = buildWithTypeModifier;
|
|
969
1358
|
exports.createColocateHelper = createColocateHelper;
|
|
970
1359
|
exports.createDefaultAdapter = createDefaultAdapter;
|
|
1360
|
+
exports.createDirectiveBuilder = createDirectiveBuilder;
|
|
1361
|
+
exports.createDirectiveMethod = createDirectiveMethod;
|
|
971
1362
|
exports.createFieldFactories = createFieldFactories;
|
|
972
1363
|
exports.createGqlElementComposer = createGqlElementComposer;
|
|
973
1364
|
exports.createGqlFragmentComposers = createGqlFragmentComposers;
|
|
974
1365
|
exports.createOperationComposerFactory = createOperationComposerFactory;
|
|
1366
|
+
exports.createStandardDirectives = createStandardDirectives;
|
|
975
1367
|
exports.createVarAssignments = createVarAssignments;
|
|
976
1368
|
exports.createVarBuilder = createVarBuilder;
|
|
977
1369
|
exports.createVarMethod = createVarMethod;
|
|
@@ -982,6 +1374,7 @@ exports.define = require_schema.define;
|
|
|
982
1374
|
exports.defineOperationRoots = require_schema.defineOperationRoots;
|
|
983
1375
|
exports.defineScalar = require_schema.defineScalar;
|
|
984
1376
|
exports.getCurrentFieldPath = getCurrentFieldPath;
|
|
1377
|
+
exports.isDirectiveRef = isDirectiveRef;
|
|
985
1378
|
exports.isListType = isListType;
|
|
986
1379
|
exports.recordFragmentUsage = recordFragmentUsage;
|
|
987
1380
|
exports.unsafeInputType = require_schema.unsafeInputType;
|