@soda-gql/core 0.0.9 → 0.2.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/README.md +247 -0
- package/dist/adapter.cjs +35 -0
- package/dist/adapter.cjs.map +1 -0
- package/dist/adapter.d.cts +35 -0
- package/dist/adapter.d.cts.map +1 -0
- package/dist/adapter.d.ts +35 -0
- package/dist/adapter.d.ts.map +1 -0
- package/dist/adapter.js +34 -0
- package/dist/adapter.js.map +1 -0
- package/dist/index-B-erotAZ.d.cts +657 -0
- package/dist/index-B-erotAZ.d.cts.map +1 -0
- package/dist/index-Djr9A4KL.d.ts +657 -0
- package/dist/index-Djr9A4KL.d.ts.map +1 -0
- package/dist/index.cjs +450 -348
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +88 -2
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +88 -2
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +420 -323
- package/dist/index.js.map +1 -1
- package/dist/runtime.cjs +85 -0
- package/dist/runtime.cjs.map +1 -0
- package/dist/runtime.d.cts +40 -0
- package/dist/runtime.d.cts.map +1 -0
- package/dist/runtime.d.ts +40 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +82 -0
- package/dist/runtime.js.map +1 -0
- package/dist/schema-Bip7o0g3.cjs +128 -0
- package/dist/schema-Bip7o0g3.cjs.map +1 -0
- package/dist/schema-BygZwEX8.d.ts +494 -0
- package/dist/schema-BygZwEX8.d.ts.map +1 -0
- package/dist/schema-D9wIW5Dl.js +86 -0
- package/dist/schema-D9wIW5Dl.js.map +1 -0
- package/dist/schema-DRkKucYe.d.cts +494 -0
- package/dist/schema-DRkKucYe.d.cts.map +1 -0
- package/dist/schema-builder-8zadflz-.d.cts +40 -0
- package/dist/schema-builder-8zadflz-.d.cts.map +1 -0
- package/dist/schema-builder-vwQtCGYI.d.ts +40 -0
- package/dist/schema-builder-vwQtCGYI.d.ts.map +1 -0
- package/package.json +34 -7
- package/dist/index-CFNJ_Aa6.d.ts +0 -1185
- package/dist/index-CFNJ_Aa6.d.ts.map +0 -1
- package/dist/index-DHh8XRal.d.cts +0 -1185
- package/dist/index-DHh8XRal.d.cts.map +0 -1
- package/dist/runtime/index.cjs +0 -105
- package/dist/runtime/index.d.cts +0 -71
- package/dist/runtime/index.d.cts.map +0 -1
- package/dist/runtime/index.d.ts +0 -71
- package/dist/runtime/index.d.ts.map +0 -1
- package/dist/runtime/index.js +0 -102
- package/dist/runtime/index.js.map +0 -1
- package/dist/slice-DlVY4UJG.cjs +0 -333
- package/dist/slice-ua5mSfhV.js +0 -256
- package/dist/slice-ua5mSfhV.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
|
-
import { a as
|
|
1
|
+
import { a as unsafeOutputType, i as unsafeInputType, n as defineOperationRoots, o as parseModifiedTypeName, r as defineScalar, s as wrapByKey, t as define } from "./schema-D9wIW5Dl.js";
|
|
2
2
|
import { Kind, OperationTypeNode } from "graphql";
|
|
3
3
|
|
|
4
|
+
//#region packages/core/src/types/type-foundation/var-ref.ts
|
|
5
|
+
var VarRef = class {
|
|
6
|
+
constructor(inner) {
|
|
7
|
+
this.inner = inner;
|
|
8
|
+
}
|
|
9
|
+
static getInner(varRef) {
|
|
10
|
+
return varRef.inner;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
const isVarRef = (value) => {
|
|
14
|
+
return typeof value === "object" && value !== null && value instanceof VarRef;
|
|
15
|
+
};
|
|
16
|
+
const createVarRefFromVariable = (name) => {
|
|
17
|
+
return new VarRef({
|
|
18
|
+
type: "variable",
|
|
19
|
+
name
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
const createVarRefFromConstValue = (value) => {
|
|
23
|
+
return new VarRef({
|
|
24
|
+
type: "const-value",
|
|
25
|
+
value
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
const getVarRefInner = (varRef) => {
|
|
29
|
+
return VarRef.getInner(varRef);
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Get the variable name from a VarRef.
|
|
33
|
+
* Throws if the VarRef contains a const-value instead of a variable reference.
|
|
34
|
+
*/
|
|
35
|
+
const getVarRefName = (varRef) => {
|
|
36
|
+
const inner = VarRef.getInner(varRef);
|
|
37
|
+
if (inner.type !== "variable") throw new Error("Expected variable reference, got const-value");
|
|
38
|
+
return inner.name;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Get the const value from a VarRef.
|
|
42
|
+
* Throws if the VarRef contains a variable reference instead of a const-value.
|
|
43
|
+
*/
|
|
44
|
+
const getVarRefValue = (varRef) => {
|
|
45
|
+
const inner = VarRef.getInner(varRef);
|
|
46
|
+
if (inner.type !== "const-value") throw new Error("Expected const-value, got variable reference");
|
|
47
|
+
return inner.value;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
//#endregion
|
|
4
51
|
//#region packages/core/src/composer/build-document.ts
|
|
5
52
|
const buildArgumentValue = (value) => {
|
|
6
53
|
if (value === void 0) return null;
|
|
@@ -235,6 +282,161 @@ const buildDocument = (options) => {
|
|
|
235
282
|
};
|
|
236
283
|
};
|
|
237
284
|
|
|
285
|
+
//#endregion
|
|
286
|
+
//#region packages/core/src/composer/colocate.ts
|
|
287
|
+
/**
|
|
288
|
+
* Creates a $colocate helper function for fragment colocation.
|
|
289
|
+
*
|
|
290
|
+
* $colocate takes an object of { label: fields } and applies prefix-based
|
|
291
|
+
* aliasing to each entry. This mirrors the structure of createExecutionResultParser
|
|
292
|
+
* from @soda-gql/colocation-tools.
|
|
293
|
+
*
|
|
294
|
+
* @example
|
|
295
|
+
* ```typescript
|
|
296
|
+
* // In operation definition
|
|
297
|
+
* query.operation({ name: "GetData" }, ({ f, $ }) => [
|
|
298
|
+
* $colocate({
|
|
299
|
+
* userCard: userCardFragment.embed({ userId: $.userId }),
|
|
300
|
+
* posts: postsFragment.embed({ userId: $.userId }),
|
|
301
|
+
* }),
|
|
302
|
+
* ]);
|
|
303
|
+
*
|
|
304
|
+
* // In parser definition (same labels)
|
|
305
|
+
* createExecutionResultParser({
|
|
306
|
+
* userCard: userCardProjection,
|
|
307
|
+
* posts: postsProjection,
|
|
308
|
+
* });
|
|
309
|
+
* ```
|
|
310
|
+
*/
|
|
311
|
+
const createColocateHelper = () => {
|
|
312
|
+
/**
|
|
313
|
+
* Colocate multiple field selections with labeled prefixes.
|
|
314
|
+
*
|
|
315
|
+
* @param entries - Object mapping labels to field selections
|
|
316
|
+
* @returns Merged object of all prefixed field entries
|
|
317
|
+
*/
|
|
318
|
+
const $colocate = (entries) => Object.fromEntries(Object.entries(entries).flatMap(([label, fields]) => Object.entries(fields).map(([key, value]) => [`${label}_${key}`, value])));
|
|
319
|
+
return $colocate;
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
//#endregion
|
|
323
|
+
//#region packages/core/src/composer/field-path-context.ts
|
|
324
|
+
/**
|
|
325
|
+
* Shared mutable container for field path context.
|
|
326
|
+
* Only synchronous access is supported.
|
|
327
|
+
*/
|
|
328
|
+
const fieldPathContext = { current: null };
|
|
329
|
+
/**
|
|
330
|
+
* Get the current field path.
|
|
331
|
+
* Returns null if not in a field building context.
|
|
332
|
+
*
|
|
333
|
+
* @example
|
|
334
|
+
* ```typescript
|
|
335
|
+
* import { getCurrentFieldPath } from '@soda-gql/core';
|
|
336
|
+
*
|
|
337
|
+
* // Inside a field builder or model embed:
|
|
338
|
+
* const path = getCurrentFieldPath();
|
|
339
|
+
* console.log(path?.full); // "$.user.posts[].author"
|
|
340
|
+
* ```
|
|
341
|
+
*/
|
|
342
|
+
const getCurrentFieldPath = () => fieldPathContext.current;
|
|
343
|
+
/**
|
|
344
|
+
* Run a function with a specific field path context.
|
|
345
|
+
* Restores the previous path after the function completes.
|
|
346
|
+
*
|
|
347
|
+
* @internal
|
|
348
|
+
*/
|
|
349
|
+
const withFieldPath = (path, fn) => {
|
|
350
|
+
const previousPath = fieldPathContext.current;
|
|
351
|
+
fieldPathContext.current = path;
|
|
352
|
+
try {
|
|
353
|
+
return fn();
|
|
354
|
+
} finally {
|
|
355
|
+
fieldPathContext.current = previousPath;
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
/**
|
|
359
|
+
* Append a new segment to the current path.
|
|
360
|
+
*
|
|
361
|
+
* @internal
|
|
362
|
+
*/
|
|
363
|
+
const appendToPath = (parent, segment) => {
|
|
364
|
+
const listSuffix = segment.isList ? "[]" : "";
|
|
365
|
+
const newSegment = {
|
|
366
|
+
field: segment.field,
|
|
367
|
+
parent: segment.parentType,
|
|
368
|
+
isList: segment.isList
|
|
369
|
+
};
|
|
370
|
+
if (!parent) return {
|
|
371
|
+
full: `$.${segment.field}${listSuffix}`,
|
|
372
|
+
segments: [newSegment]
|
|
373
|
+
};
|
|
374
|
+
return {
|
|
375
|
+
full: `${parent.full}.${segment.field}${listSuffix}`,
|
|
376
|
+
segments: [...parent.segments, newSegment]
|
|
377
|
+
};
|
|
378
|
+
};
|
|
379
|
+
/**
|
|
380
|
+
* Check if a type specifier represents a list type.
|
|
381
|
+
* Matches patterns like "Type:![]!", "Type:![]?", "Type:?[]!", etc.
|
|
382
|
+
*
|
|
383
|
+
* @internal
|
|
384
|
+
*/
|
|
385
|
+
const isListType = (typeString) => {
|
|
386
|
+
return typeString.includes("[]");
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
//#endregion
|
|
390
|
+
//#region packages/core/src/types/element/fields-builder.ts
|
|
391
|
+
const mergeFields = (fields) => Object.assign({}, ...fields);
|
|
392
|
+
|
|
393
|
+
//#endregion
|
|
394
|
+
//#region packages/core/src/types/element/lazy-evaluator.ts
|
|
395
|
+
/**
|
|
396
|
+
* Creates a lazy evaluator with caching, async support, and dependency ordering.
|
|
397
|
+
*
|
|
398
|
+
* @param define - Factory function that produces the value
|
|
399
|
+
* @param getDeps - Optional function returning dependencies to evaluate first
|
|
400
|
+
* @param createDepGenerator - Function to create evaluation generator for a dependency
|
|
401
|
+
* @returns An executor generator function
|
|
402
|
+
*/
|
|
403
|
+
const createLazyEvaluator = (define$1, getDeps, createDepGenerator) => {
|
|
404
|
+
let cache = null;
|
|
405
|
+
let promise = null;
|
|
406
|
+
return function* execute(context) {
|
|
407
|
+
if (cache) return cache.value;
|
|
408
|
+
if (promise) {
|
|
409
|
+
yield promise;
|
|
410
|
+
return cache.value;
|
|
411
|
+
}
|
|
412
|
+
if (getDeps) for (const dep of getDeps()) yield* createDepGenerator(dep);
|
|
413
|
+
const defined = define$1(context);
|
|
414
|
+
if (!(defined instanceof Promise)) return (cache = { value: defined }).value;
|
|
415
|
+
promise = defined.then((value) => {
|
|
416
|
+
cache = { value };
|
|
417
|
+
promise = null;
|
|
418
|
+
});
|
|
419
|
+
yield promise;
|
|
420
|
+
return cache.value;
|
|
421
|
+
};
|
|
422
|
+
};
|
|
423
|
+
/**
|
|
424
|
+
* Creates an evaluation generator from an executor.
|
|
425
|
+
* Wraps the executor's generator and discards its return value.
|
|
426
|
+
*/
|
|
427
|
+
function* createEvaluationGenerator(executor, context) {
|
|
428
|
+
yield* executor(context);
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* Executes the evaluator synchronously.
|
|
432
|
+
* Throws if async operation is encountered.
|
|
433
|
+
*/
|
|
434
|
+
const evaluateSync = (executor, context) => {
|
|
435
|
+
const result = executor(context).next();
|
|
436
|
+
if (!result.done) throw new Error("Async operation is not supported in sync evaluation.");
|
|
437
|
+
return result.value;
|
|
438
|
+
};
|
|
439
|
+
|
|
238
440
|
//#endregion
|
|
239
441
|
//#region packages/core/src/types/element/gql-element.ts
|
|
240
442
|
const GQL_ELEMENT_FACTORY = Symbol("GQL_ELEMENT_FACTORY");
|
|
@@ -242,66 +444,58 @@ const GQL_ELEMENT_CONTEXT = Symbol("GQL_ELEMENT_CONTEXT");
|
|
|
242
444
|
var GqlElement = class GqlElement {
|
|
243
445
|
[GQL_ELEMENT_FACTORY];
|
|
244
446
|
[GQL_ELEMENT_CONTEXT] = null;
|
|
245
|
-
constructor(define$1) {
|
|
246
|
-
|
|
247
|
-
this[GQL_ELEMENT_FACTORY] = (context) => {
|
|
248
|
-
if (cache) return cache.value;
|
|
249
|
-
const value = define$1(context);
|
|
250
|
-
cache = { value };
|
|
251
|
-
return value;
|
|
252
|
-
};
|
|
447
|
+
constructor(define$1, getDeps) {
|
|
448
|
+
this[GQL_ELEMENT_FACTORY] = createLazyEvaluator(define$1, getDeps, GqlElement.createEvaluationGenerator);
|
|
253
449
|
Object.defineProperty(this, "$infer", { get() {
|
|
254
450
|
throw new Error("This property is only for type meta. Do not access this property directly.");
|
|
255
451
|
} });
|
|
256
452
|
}
|
|
453
|
+
attach(attachment) {
|
|
454
|
+
let cache = null;
|
|
455
|
+
Object.defineProperty(this, attachment.name, { get() {
|
|
456
|
+
if (cache) return cache;
|
|
457
|
+
GqlElement.evaluateInstantly(this);
|
|
458
|
+
return cache = attachment.createValue(this);
|
|
459
|
+
} });
|
|
460
|
+
return this;
|
|
461
|
+
}
|
|
257
462
|
static setContext(element, context) {
|
|
258
463
|
element[GQL_ELEMENT_CONTEXT] = context;
|
|
259
464
|
}
|
|
260
|
-
static
|
|
261
|
-
|
|
465
|
+
static createEvaluationGenerator(element) {
|
|
466
|
+
return createEvaluationGenerator(element[GQL_ELEMENT_FACTORY], element[GQL_ELEMENT_CONTEXT]);
|
|
467
|
+
}
|
|
468
|
+
static evaluateInstantly(element) {
|
|
469
|
+
return evaluateSync(element[GQL_ELEMENT_FACTORY], element[GQL_ELEMENT_CONTEXT]);
|
|
470
|
+
}
|
|
471
|
+
static evaluateSync(element) {
|
|
472
|
+
GqlElement.evaluateInstantly(element);
|
|
262
473
|
}
|
|
263
474
|
static get(element) {
|
|
264
|
-
|
|
265
|
-
return element[GQL_ELEMENT_FACTORY](context);
|
|
475
|
+
return GqlElement.evaluateInstantly(element);
|
|
266
476
|
}
|
|
267
477
|
};
|
|
268
478
|
|
|
269
479
|
//#endregion
|
|
270
|
-
//#region packages/core/src/types/element/
|
|
271
|
-
var
|
|
480
|
+
//#region packages/core/src/types/element/fragment.ts
|
|
481
|
+
var Fragment = class Fragment extends GqlElement {
|
|
272
482
|
constructor(define$1) {
|
|
273
483
|
super(define$1);
|
|
274
484
|
}
|
|
275
|
-
get
|
|
276
|
-
return GqlElement.get(this).
|
|
277
|
-
}
|
|
278
|
-
get operationName() {
|
|
279
|
-
return GqlElement.get(this).operationName;
|
|
280
|
-
}
|
|
281
|
-
get variableNames() {
|
|
282
|
-
return GqlElement.get(this).variableNames;
|
|
283
|
-
}
|
|
284
|
-
get projectionPathGraph() {
|
|
285
|
-
return GqlElement.get(this).projectionPathGraph;
|
|
286
|
-
}
|
|
287
|
-
get document() {
|
|
288
|
-
return GqlElement.get(this).document;
|
|
485
|
+
get typename() {
|
|
486
|
+
return GqlElement.get(this).typename;
|
|
289
487
|
}
|
|
290
|
-
get
|
|
291
|
-
return GqlElement.get(this).
|
|
488
|
+
get embed() {
|
|
489
|
+
return GqlElement.get(this).embed;
|
|
292
490
|
}
|
|
293
491
|
static create(define$1) {
|
|
294
|
-
return new
|
|
492
|
+
return new Fragment(define$1);
|
|
295
493
|
}
|
|
296
494
|
};
|
|
297
495
|
|
|
298
496
|
//#endregion
|
|
299
|
-
//#region packages/core/src/types/element/
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
//#endregion
|
|
303
|
-
//#region packages/core/src/types/element/inline-operation.ts
|
|
304
|
-
var InlineOperation = class InlineOperation extends GqlElement {
|
|
497
|
+
//#region packages/core/src/types/element/operation.ts
|
|
498
|
+
var Operation = class Operation extends GqlElement {
|
|
305
499
|
constructor(define$1) {
|
|
306
500
|
super(define$1);
|
|
307
501
|
}
|
|
@@ -320,115 +514,18 @@ var InlineOperation = class InlineOperation extends GqlElement {
|
|
|
320
514
|
get document() {
|
|
321
515
|
return GqlElement.get(this).document;
|
|
322
516
|
}
|
|
323
|
-
|
|
324
|
-
return
|
|
325
|
-
}
|
|
326
|
-
};
|
|
327
|
-
|
|
328
|
-
//#endregion
|
|
329
|
-
//#region packages/core/src/types/element/model.ts
|
|
330
|
-
var Model = class Model extends GqlElement {
|
|
331
|
-
constructor(define$1) {
|
|
332
|
-
super(define$1);
|
|
333
|
-
}
|
|
334
|
-
get typename() {
|
|
335
|
-
return GqlElement.get(this).typename;
|
|
336
|
-
}
|
|
337
|
-
get fragment() {
|
|
338
|
-
return GqlElement.get(this).fragment;
|
|
339
|
-
}
|
|
340
|
-
get normalize() {
|
|
341
|
-
return GqlElement.get(this).normalize;
|
|
342
|
-
}
|
|
343
|
-
static create(define$1) {
|
|
344
|
-
return new Model(define$1);
|
|
345
|
-
}
|
|
346
|
-
};
|
|
347
|
-
|
|
348
|
-
//#endregion
|
|
349
|
-
//#region packages/core/src/types/element/slice.ts
|
|
350
|
-
var Slice = class Slice extends GqlElement {
|
|
351
|
-
constructor(define$1) {
|
|
352
|
-
super(define$1);
|
|
353
|
-
}
|
|
354
|
-
get operationType() {
|
|
355
|
-
return GqlElement.get(this).operationType;
|
|
356
|
-
}
|
|
357
|
-
get embed() {
|
|
358
|
-
return GqlElement.get(this).embed;
|
|
517
|
+
get metadata() {
|
|
518
|
+
return GqlElement.get(this).metadata;
|
|
359
519
|
}
|
|
360
520
|
static create(define$1) {
|
|
361
|
-
return new
|
|
521
|
+
return new Operation(define$1);
|
|
362
522
|
}
|
|
363
523
|
};
|
|
364
524
|
|
|
365
525
|
//#endregion
|
|
366
|
-
//#region packages/core/src/
|
|
367
|
-
function
|
|
368
|
-
|
|
369
|
-
if (segment) (acc[segment] || (acc[segment] = [])).push({
|
|
370
|
-
label,
|
|
371
|
-
raw,
|
|
372
|
-
segments
|
|
373
|
-
});
|
|
374
|
-
return acc;
|
|
375
|
-
}, {});
|
|
376
|
-
return {
|
|
377
|
-
matches: paths.map(({ label, raw, segments }) => ({
|
|
378
|
-
label,
|
|
379
|
-
path: raw,
|
|
380
|
-
exact: segments.length === 0
|
|
381
|
-
})),
|
|
382
|
-
children: mapValues(intermediate, (paths$1) => createPathGraph(paths$1))
|
|
383
|
-
};
|
|
384
|
-
}
|
|
385
|
-
function createPathGraphFromSliceEntries(fragments) {
|
|
386
|
-
return createPathGraph(Object.entries(fragments).flatMap(([label, slice]) => Array.from(new Map(slice.projection.paths.map(({ full: raw, segments }) => {
|
|
387
|
-
const [first, ...rest] = segments;
|
|
388
|
-
return [raw, {
|
|
389
|
-
label,
|
|
390
|
-
raw,
|
|
391
|
-
segments: [`${label}_${first}`, ...rest]
|
|
392
|
-
}];
|
|
393
|
-
})).values())));
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
//#endregion
|
|
397
|
-
//#region packages/core/src/composer/composed-operation.ts
|
|
398
|
-
const createComposedOperationComposerFactory = () => {
|
|
399
|
-
return (operationType) => {
|
|
400
|
-
return (options, builder) => {
|
|
401
|
-
return ComposedOperation.create(() => {
|
|
402
|
-
const { operationName } = options;
|
|
403
|
-
const variables = mergeVarDefinitions(options.variables ?? []);
|
|
404
|
-
const fragments = builder({ $: createVarRefs(variables) });
|
|
405
|
-
const fields = Object.fromEntries(Object.entries(fragments).flatMap(([label, { getFields: fields$1 }]) => Object.entries(fields$1).map(([key, reference]) => [`${label}_${key}`, reference])));
|
|
406
|
-
const projectionPathGraph = createPathGraphFromSliceEntries(fragments);
|
|
407
|
-
return {
|
|
408
|
-
operationType,
|
|
409
|
-
operationName,
|
|
410
|
-
variableNames: Object.keys(variables),
|
|
411
|
-
projectionPathGraph,
|
|
412
|
-
document: buildDocument({
|
|
413
|
-
operationName,
|
|
414
|
-
operationType,
|
|
415
|
-
variables,
|
|
416
|
-
fields
|
|
417
|
-
}),
|
|
418
|
-
parse: createExecutionResultParser({
|
|
419
|
-
fragments,
|
|
420
|
-
projectionPathGraph
|
|
421
|
-
})
|
|
422
|
-
};
|
|
423
|
-
});
|
|
424
|
-
};
|
|
425
|
-
};
|
|
426
|
-
};
|
|
427
|
-
|
|
428
|
-
//#endregion
|
|
429
|
-
//#region packages/core/src/utils/wrap-by-key.ts
|
|
430
|
-
function wrapByKey(name, value) {
|
|
431
|
-
return { [name]: value };
|
|
526
|
+
//#region packages/core/src/utils/map-values.ts
|
|
527
|
+
function mapValues(obj, fn) {
|
|
528
|
+
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, fn(value, key)]));
|
|
432
529
|
}
|
|
433
530
|
|
|
434
531
|
//#endregion
|
|
@@ -456,30 +553,44 @@ const createFieldFactoriesInner = (schema, typeName) => {
|
|
|
456
553
|
const factory = (fieldArgs, extras) => {
|
|
457
554
|
const wrap = (value) => wrapByKey(extras?.alias ?? fieldName, value);
|
|
458
555
|
if (type.kind === "object") {
|
|
459
|
-
const factoryReturn = ((nest) =>
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
556
|
+
const factoryReturn = ((nest) => {
|
|
557
|
+
const nestedFields = withFieldPath(appendToPath(getCurrentFieldPath(), {
|
|
558
|
+
field: fieldName,
|
|
559
|
+
parentType: typeName,
|
|
560
|
+
isList: isListType(type.modifier)
|
|
561
|
+
}), () => mergeFields(nest({ f: createFieldFactories(schema, type.name) })));
|
|
562
|
+
return wrap({
|
|
563
|
+
parent: typeName,
|
|
564
|
+
field: fieldName,
|
|
565
|
+
type,
|
|
566
|
+
args: fieldArgs ?? {},
|
|
567
|
+
directives: extras?.directives ?? {},
|
|
568
|
+
object: nestedFields,
|
|
569
|
+
union: null
|
|
570
|
+
});
|
|
571
|
+
});
|
|
468
572
|
return factoryReturn;
|
|
469
573
|
}
|
|
470
574
|
if (type.kind === "union") {
|
|
471
|
-
const factoryReturn = ((nest) =>
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
object: null,
|
|
478
|
-
union: mapValues(nest, (builder, memberName) => {
|
|
575
|
+
const factoryReturn = ((nest) => {
|
|
576
|
+
const nestedUnion = withFieldPath(appendToPath(getCurrentFieldPath(), {
|
|
577
|
+
field: fieldName,
|
|
578
|
+
parentType: typeName,
|
|
579
|
+
isList: isListType(type.modifier)
|
|
580
|
+
}), () => mapValues(nest, (builder, memberName) => {
|
|
479
581
|
if (!builder) throw new Error(`Builder is undefined for member name: ${memberName}`);
|
|
480
582
|
return mergeFields(builder({ f: createFieldFactories(schema, memberName) }));
|
|
481
|
-
})
|
|
482
|
-
|
|
583
|
+
}));
|
|
584
|
+
return wrap({
|
|
585
|
+
parent: typeName,
|
|
586
|
+
field: fieldName,
|
|
587
|
+
type,
|
|
588
|
+
args: fieldArgs ?? {},
|
|
589
|
+
directives: extras?.directives ?? {},
|
|
590
|
+
object: null,
|
|
591
|
+
union: nestedUnion
|
|
592
|
+
});
|
|
593
|
+
});
|
|
483
594
|
return factoryReturn;
|
|
484
595
|
}
|
|
485
596
|
if (type.kind === "scalar" || type.kind === "enum" || type.kind === "typename") return wrap({
|
|
@@ -499,100 +610,157 @@ const createFieldFactoriesInner = (schema, typeName) => {
|
|
|
499
610
|
};
|
|
500
611
|
|
|
501
612
|
//#endregion
|
|
502
|
-
//#region packages/core/src/composer/
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
operationName,
|
|
523
|
-
operationType,
|
|
524
|
-
variables,
|
|
525
|
-
fields
|
|
526
|
-
})
|
|
527
|
-
};
|
|
528
|
-
});
|
|
613
|
+
//#region packages/core/src/composer/fragment-usage-context.ts
|
|
614
|
+
/**
|
|
615
|
+
* Shared mutable container for collecting fragment usages.
|
|
616
|
+
* Only synchronous access is supported.
|
|
617
|
+
*/
|
|
618
|
+
const fragmentUsageContext = { current: null };
|
|
619
|
+
/**
|
|
620
|
+
* Run a function with fragment usage collection enabled.
|
|
621
|
+
* Returns both the function result and collected fragment usages.
|
|
622
|
+
*
|
|
623
|
+
* @internal
|
|
624
|
+
*/
|
|
625
|
+
const withFragmentUsageCollection = (fn) => {
|
|
626
|
+
const previousCollector = fragmentUsageContext.current;
|
|
627
|
+
const usages = [];
|
|
628
|
+
fragmentUsageContext.current = usages;
|
|
629
|
+
try {
|
|
630
|
+
return {
|
|
631
|
+
result: fn(),
|
|
632
|
+
usages
|
|
529
633
|
};
|
|
530
|
-
}
|
|
634
|
+
} finally {
|
|
635
|
+
fragmentUsageContext.current = previousCollector;
|
|
636
|
+
}
|
|
637
|
+
};
|
|
638
|
+
/**
|
|
639
|
+
* Record a fragment usage. Called when fragment.embed() is invoked.
|
|
640
|
+
* No-op if not in a collection context.
|
|
641
|
+
*
|
|
642
|
+
* @internal
|
|
643
|
+
*/
|
|
644
|
+
const recordFragmentUsage = (record) => {
|
|
645
|
+
if (fragmentUsageContext.current) fragmentUsageContext.current.push(record);
|
|
646
|
+
};
|
|
647
|
+
|
|
648
|
+
//#endregion
|
|
649
|
+
//#region packages/core/src/composer/input.ts
|
|
650
|
+
const mergeVarDefinitions = (definitions) => Object.assign({}, ...definitions);
|
|
651
|
+
const createVarAssignments = (definitions, providedValues) => {
|
|
652
|
+
return mapValues(definitions, (_definition, key) => {
|
|
653
|
+
const varName = key;
|
|
654
|
+
if (!providedValues || providedValues[varName] === void 0) return createVarRefFromConstValue(void 0);
|
|
655
|
+
const provided = providedValues[varName];
|
|
656
|
+
if (isVarRef(provided)) return provided;
|
|
657
|
+
return createVarRefFromConstValue(provided);
|
|
658
|
+
});
|
|
531
659
|
};
|
|
660
|
+
const createVarRefs = (definitions) => mapValues(definitions, (_ref, name) => createVarRefFromVariable(name));
|
|
532
661
|
|
|
533
662
|
//#endregion
|
|
534
|
-
//#region packages/core/src/composer/
|
|
535
|
-
const
|
|
536
|
-
const
|
|
537
|
-
return (options, builder
|
|
663
|
+
//#region packages/core/src/composer/fragment.ts
|
|
664
|
+
const createGqlFragmentComposers = (schema, _adapter) => {
|
|
665
|
+
const createFragmentComposer = (typename) => {
|
|
666
|
+
return (options, builder) => {
|
|
538
667
|
const varDefinitions = mergeVarDefinitions(options.variables ?? []);
|
|
539
|
-
|
|
668
|
+
const { metadata } = options;
|
|
669
|
+
return Fragment.create(() => ({
|
|
540
670
|
typename,
|
|
541
|
-
|
|
671
|
+
embed: (variables) => {
|
|
672
|
+
const f = createFieldFactories(schema, typename);
|
|
673
|
+
const $ = createVarAssignments(varDefinitions, variables);
|
|
674
|
+
recordFragmentUsage({
|
|
675
|
+
metadataBuilder: metadata ? () => metadata({ $ }) : null,
|
|
676
|
+
path: getCurrentFieldPath()
|
|
677
|
+
});
|
|
542
678
|
return mergeFields(builder({
|
|
543
|
-
f
|
|
544
|
-
|
|
679
|
+
f,
|
|
680
|
+
$
|
|
545
681
|
}));
|
|
546
|
-
}
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
});
|
|
682
|
+
}
|
|
683
|
+
}));
|
|
684
|
+
};
|
|
550
685
|
};
|
|
551
|
-
return mapValues(schema.object, (_, typename) =>
|
|
686
|
+
return mapValues(schema.object, (_, typename) => createFragmentComposer(typename));
|
|
552
687
|
};
|
|
553
688
|
|
|
554
689
|
//#endregion
|
|
555
|
-
//#region packages/core/src/
|
|
556
|
-
|
|
690
|
+
//#region packages/core/src/types/metadata/adapter.ts
|
|
691
|
+
/**
|
|
692
|
+
* Creates the default adapter instance.
|
|
693
|
+
* @internal
|
|
694
|
+
*/
|
|
695
|
+
const createDefaultAdapter = () => ({ aggregateFragmentMetadata: (fragments) => fragments.map((m) => m.metadata) });
|
|
696
|
+
/**
|
|
697
|
+
* The default adapter instance.
|
|
698
|
+
*/
|
|
699
|
+
const defaultMetadataAdapter = createDefaultAdapter();
|
|
700
|
+
|
|
701
|
+
//#endregion
|
|
702
|
+
//#region packages/core/src/composer/operation.ts
|
|
703
|
+
const createOperationComposerFactory = (schema, adapter) => {
|
|
704
|
+
const resolvedAdapter = adapter ?? defaultMetadataAdapter;
|
|
557
705
|
return (operationType) => {
|
|
558
706
|
const operationTypeName = schema.operations[operationType];
|
|
559
707
|
if (operationTypeName === null) throw new Error(`Operation type ${operationType} is not defined in schema roots`);
|
|
560
|
-
return (options, fieldBuilder
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
708
|
+
return (options, fieldBuilder) => {
|
|
709
|
+
return Operation.create(() => {
|
|
710
|
+
const { name: operationName } = options;
|
|
711
|
+
const variables = mergeVarDefinitions(options.variables ?? []);
|
|
712
|
+
const $ = createVarRefs(variables);
|
|
713
|
+
const f = createFieldFactories(schema, operationTypeName);
|
|
714
|
+
const { result: fields, usages: fragmentUsages } = withFragmentUsageCollection(() => mergeFields(fieldBuilder({
|
|
715
|
+
f,
|
|
716
|
+
$
|
|
717
|
+
})));
|
|
718
|
+
const document = buildDocument({
|
|
719
|
+
operationName,
|
|
720
|
+
operationType,
|
|
721
|
+
variables,
|
|
722
|
+
fields
|
|
723
|
+
});
|
|
724
|
+
const createDefinition = (metadata) => ({
|
|
725
|
+
operationType,
|
|
726
|
+
operationName,
|
|
727
|
+
variableNames: Object.keys(variables),
|
|
728
|
+
documentSource: () => fields,
|
|
729
|
+
document,
|
|
730
|
+
metadata
|
|
731
|
+
});
|
|
732
|
+
if (!fragmentUsages.some((u) => u.metadataBuilder) && !options.metadata) return createDefinition(void 0);
|
|
733
|
+
const fragmentMetadataResults = fragmentUsages.map((usage) => usage.metadataBuilder ? usage.metadataBuilder() : void 0);
|
|
734
|
+
const hasAsyncFragmentMetadata = fragmentMetadataResults.some((r) => r instanceof Promise);
|
|
735
|
+
const buildOperationMetadata = (resolvedFragmentMetadata) => {
|
|
736
|
+
const fragmentMetaInfos = fragmentUsages.map((usage, index) => ({
|
|
737
|
+
metadata: resolvedFragmentMetadata[index],
|
|
738
|
+
fieldPath: usage.path
|
|
569
739
|
}));
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
740
|
+
const aggregatedFragmentMetadata = resolvedAdapter.aggregateFragmentMetadata(fragmentMetaInfos);
|
|
741
|
+
const schemaLevel = resolvedAdapter.schemaLevel;
|
|
742
|
+
return options.metadata?.({
|
|
743
|
+
$,
|
|
744
|
+
document,
|
|
745
|
+
fragmentMetadata: aggregatedFragmentMetadata,
|
|
746
|
+
schemaLevel
|
|
747
|
+
});
|
|
748
|
+
};
|
|
749
|
+
if (hasAsyncFragmentMetadata) return Promise.all(fragmentMetadataResults).then(async (resolvedFragmentMetadata) => {
|
|
750
|
+
return createDefinition(await buildOperationMetadata(resolvedFragmentMetadata));
|
|
751
|
+
});
|
|
752
|
+
const operationMetadataResult = buildOperationMetadata(fragmentMetadataResults);
|
|
753
|
+
if (operationMetadataResult instanceof Promise) return operationMetadataResult.then(createDefinition);
|
|
754
|
+
return createDefinition(operationMetadataResult);
|
|
755
|
+
});
|
|
756
|
+
};
|
|
578
757
|
};
|
|
579
758
|
};
|
|
580
759
|
|
|
581
|
-
//#endregion
|
|
582
|
-
//#region packages/core/src/types/type-foundation/modified-type-name.ts
|
|
583
|
-
function parseModifiedTypeName(nameAndModifier) {
|
|
584
|
-
if (typeof nameAndModifier !== "string") throw new Error(`Invalid modified type name: ${nameAndModifier}`);
|
|
585
|
-
const [name, modifier] = nameAndModifier.split(":");
|
|
586
|
-
return {
|
|
587
|
-
name,
|
|
588
|
-
modifier
|
|
589
|
-
};
|
|
590
|
-
}
|
|
591
|
-
|
|
592
760
|
//#endregion
|
|
593
761
|
//#region packages/core/src/composer/var-builder.ts
|
|
594
762
|
const createVarBuilder = (schema) => {
|
|
595
|
-
const
|
|
763
|
+
const varBuilder = (varName) => {
|
|
596
764
|
const createVarSpecifierBuilder = (kind) => {
|
|
597
765
|
return (type, extras) => wrapByKey(varName, {
|
|
598
766
|
kind,
|
|
@@ -611,106 +779,35 @@ const createVarBuilder = (schema) => {
|
|
|
611
779
|
}
|
|
612
780
|
};
|
|
613
781
|
};
|
|
614
|
-
|
|
782
|
+
varBuilder.getName = getVarRefName;
|
|
783
|
+
varBuilder.getValue = getVarRefValue;
|
|
784
|
+
varBuilder.getInner = getVarRefInner;
|
|
785
|
+
return varBuilder;
|
|
615
786
|
};
|
|
616
787
|
|
|
617
788
|
//#endregion
|
|
618
789
|
//#region packages/core/src/composer/gql-composer.ts
|
|
619
|
-
const createGqlElementComposer = (schema) => {
|
|
620
|
-
const
|
|
621
|
-
const
|
|
622
|
-
const
|
|
623
|
-
const
|
|
790
|
+
const createGqlElementComposer = (schema, options = {}) => {
|
|
791
|
+
const { adapter } = options;
|
|
792
|
+
const helpers = adapter?.helpers;
|
|
793
|
+
const metadataAdapter = adapter?.metadata;
|
|
794
|
+
const fragment = createGqlFragmentComposers(schema, metadataAdapter);
|
|
795
|
+
const createOperationComposer = createOperationComposerFactory(schema, metadataAdapter);
|
|
624
796
|
const composers = {
|
|
625
|
-
|
|
626
|
-
query: {
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
inline: createInlineOperationComposer("mutation")
|
|
635
|
-
},
|
|
636
|
-
subscription: {
|
|
637
|
-
slice: createSliceComposer("subscription"),
|
|
638
|
-
composed: createComposedOperationFactory("subscription"),
|
|
639
|
-
inline: createInlineOperationComposer("subscription")
|
|
640
|
-
}
|
|
797
|
+
fragment,
|
|
798
|
+
query: { operation: createOperationComposer("query") },
|
|
799
|
+
mutation: { operation: createOperationComposer("mutation") },
|
|
800
|
+
subscription: { operation: createOperationComposer("subscription") }
|
|
801
|
+
};
|
|
802
|
+
const helper = {
|
|
803
|
+
$var: createVarBuilder(schema),
|
|
804
|
+
$colocate: createColocateHelper(),
|
|
805
|
+
...helpers ?? {}
|
|
641
806
|
};
|
|
642
|
-
const helper = { ...createVarBuilder(schema) };
|
|
643
807
|
const elementComposer = (composeElement) => composeElement(composers, helper);
|
|
644
808
|
return elementComposer;
|
|
645
809
|
};
|
|
646
810
|
|
|
647
811
|
//#endregion
|
|
648
|
-
|
|
649
|
-
const withTypeMeta = (target) => {
|
|
650
|
-
if (process.env.NODE_ENV !== "production") {
|
|
651
|
-
if (typeof target !== "object" && typeof target !== "function" || target === null) throw new Error("Target must be an object or a function.");
|
|
652
|
-
Object.defineProperty(target, "$type", { get() {
|
|
653
|
-
throw new Error("This property is only for type inference. Do not access this property directly.");
|
|
654
|
-
} });
|
|
655
|
-
}
|
|
656
|
-
return target;
|
|
657
|
-
};
|
|
658
|
-
|
|
659
|
-
//#endregion
|
|
660
|
-
//#region packages/core/src/schema/type-specifier-builder.ts
|
|
661
|
-
const createUnsafeInputTypeSpecifierFactory = (kind) => {
|
|
662
|
-
return (type, extras) => ({
|
|
663
|
-
kind,
|
|
664
|
-
...parseModifiedTypeName(type),
|
|
665
|
-
defaultValue: extras.default ? { default: extras.default() } : null
|
|
666
|
-
});
|
|
667
|
-
};
|
|
668
|
-
const unsafeInputType = {
|
|
669
|
-
scalar: createUnsafeInputTypeSpecifierFactory("scalar"),
|
|
670
|
-
enum: createUnsafeInputTypeSpecifierFactory("enum"),
|
|
671
|
-
input: createUnsafeInputTypeSpecifierFactory("input")
|
|
672
|
-
};
|
|
673
|
-
const createUnsafeOutputTypeSpecifierFactory = (kind) => {
|
|
674
|
-
return (type, extras) => ({
|
|
675
|
-
kind,
|
|
676
|
-
...parseModifiedTypeName(type),
|
|
677
|
-
arguments: extras.arguments ?? {}
|
|
678
|
-
});
|
|
679
|
-
};
|
|
680
|
-
const unsafeOutputType = {
|
|
681
|
-
scalar: createUnsafeOutputTypeSpecifierFactory("scalar"),
|
|
682
|
-
enum: createUnsafeOutputTypeSpecifierFactory("enum"),
|
|
683
|
-
object: createUnsafeOutputTypeSpecifierFactory("object"),
|
|
684
|
-
union: createUnsafeOutputTypeSpecifierFactory("union"),
|
|
685
|
-
typename: createUnsafeOutputTypeSpecifierFactory("typename")
|
|
686
|
-
};
|
|
687
|
-
|
|
688
|
-
//#endregion
|
|
689
|
-
//#region packages/core/src/schema/schema-builder.ts
|
|
690
|
-
const defineScalar = (name) => wrapByKey(name, withTypeMeta({ name }));
|
|
691
|
-
const define = (name) => ({
|
|
692
|
-
enum: (values) => withTypeMeta({
|
|
693
|
-
name,
|
|
694
|
-
values
|
|
695
|
-
}),
|
|
696
|
-
input: (fields) => ({
|
|
697
|
-
name,
|
|
698
|
-
fields
|
|
699
|
-
}),
|
|
700
|
-
object: (fields) => ({
|
|
701
|
-
name,
|
|
702
|
-
fields: {
|
|
703
|
-
__typename: unsafeOutputType.typename(`${name}:!`, {}),
|
|
704
|
-
...fields
|
|
705
|
-
}
|
|
706
|
-
}),
|
|
707
|
-
union: (types) => ({
|
|
708
|
-
name,
|
|
709
|
-
types
|
|
710
|
-
})
|
|
711
|
-
});
|
|
712
|
-
const defineOperationRoots = (operationRoots) => operationRoots;
|
|
713
|
-
|
|
714
|
-
//#endregion
|
|
715
|
-
export { ComposedOperation, GqlElement, InlineOperation, Model, Projection, Slice, SlicedExecutionResultEmpty, SlicedExecutionResultError, SlicedExecutionResultSuccess, buildArgumentValue, buildConstValueNode, buildDocument, buildOperationTypeNode, buildWithTypeModifier, createComposedOperationComposerFactory, createFieldFactories, createGqlElementComposer, createGqlModelComposers, createPathGraphFromSliceEntries, createSliceComposerFactory, createVarAssignments, createVarBuilder, createVarRefs, define, defineOperationRoots, defineScalar, mergeFields, mergeVarDefinitions, unsafeInputType, unsafeOutputType };
|
|
812
|
+
export { Fragment, GqlElement, Operation, appendToPath, buildArgumentValue, buildConstValueNode, buildDocument, buildOperationTypeNode, buildWithTypeModifier, createColocateHelper, createDefaultAdapter, createFieldFactories, createGqlElementComposer, createGqlFragmentComposers, createOperationComposerFactory, createVarAssignments, createVarBuilder, createVarRefs, defaultMetadataAdapter, define, defineOperationRoots, defineScalar, getCurrentFieldPath, getVarRefInner, getVarRefName, getVarRefValue, isListType, mergeFields, mergeVarDefinitions, recordFragmentUsage, unsafeInputType, unsafeOutputType, withFieldPath, withFragmentUsageCollection };
|
|
716
813
|
//# sourceMappingURL=index.js.map
|