@soda-gql/core 0.0.8 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +312 -0
- package/dist/{index-CFNJ_Aa6.d.ts → index-DYwkqPzd.d.ts} +124 -294
- package/dist/index-DYwkqPzd.d.ts.map +1 -0
- package/dist/index-Db9ogofS.d.ts +365 -0
- package/dist/index-Db9ogofS.d.ts.map +1 -0
- package/dist/{index-DHh8XRal.d.cts → index-Dth0NSJt.d.cts} +124 -294
- package/dist/index-Dth0NSJt.d.cts.map +1 -0
- package/dist/index-zGZ61WLt.d.cts +365 -0
- package/dist/index-zGZ61WLt.d.cts.map +1 -0
- package/dist/index.cjs +132 -44
- package/dist/index.d.cts +3 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.js +129 -45
- package/dist/index.js.map +1 -1
- package/dist/merge-CeMx09is.js +74 -0
- package/dist/merge-CeMx09is.js.map +1 -0
- package/dist/merge-ZxKV1syS.cjs +85 -0
- package/dist/metadata/index.cjs +62 -0
- package/dist/metadata/index.d.cts +71 -0
- package/dist/metadata/index.d.cts.map +1 -0
- package/dist/metadata/index.d.ts +71 -0
- package/dist/metadata/index.d.ts.map +1 -0
- package/dist/metadata/index.js +59 -0
- package/dist/metadata/index.js.map +1 -0
- package/dist/runtime/index.cjs +5 -3
- package/dist/runtime/index.d.cts +2 -1
- package/dist/runtime/index.d.cts.map +1 -1
- package/dist/runtime/index.d.ts +2 -1
- package/dist/runtime/index.d.ts.map +1 -1
- package/dist/runtime/index.js +5 -3
- package/dist/runtime/index.js.map +1 -1
- package/dist/{slice-ua5mSfhV.js → slice-BuSNc8vw.js} +27 -5
- package/dist/slice-BuSNc8vw.js.map +1 -0
- package/dist/{slice-DlVY4UJG.cjs → slice-C-FIQK-f.cjs} +43 -3
- package/package.json +10 -3
- package/dist/index-CFNJ_Aa6.d.ts.map +0 -1
- package/dist/index-DHh8XRal.d.cts.map +0 -1
- package/dist/slice-ua5mSfhV.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { a as createVarRefs, c as createExecutionResultParser, d as SlicedExecutionResultSuccess, f as Projection, i as createVarAssignments, l as SlicedExecutionResultEmpty, n as handleProjectionBuilder, o as mergeVarDefinitions, p as VarRef, s as mapValues, u as SlicedExecutionResultError } from "./slice-
|
|
1
|
+
import { a as createVarRefs, c as createExecutionResultParser, d as SlicedExecutionResultSuccess, f as Projection, g as getVarRefValue, h as getVarRefName, i as createVarAssignments, l as SlicedExecutionResultEmpty, m as getVarRefInner, n as handleProjectionBuilder, o as mergeVarDefinitions, p as VarRef, s as mapValues, u as SlicedExecutionResultError } from "./slice-BuSNc8vw.js";
|
|
2
|
+
import { t as defaultMergeSliceMetadata } from "./merge-CeMx09is.js";
|
|
2
3
|
import { Kind, OperationTypeNode } from "graphql";
|
|
3
4
|
|
|
4
5
|
//#region packages/core/src/composer/build-document.ts
|
|
@@ -242,13 +243,24 @@ const GQL_ELEMENT_CONTEXT = Symbol("GQL_ELEMENT_CONTEXT");
|
|
|
242
243
|
var GqlElement = class GqlElement {
|
|
243
244
|
[GQL_ELEMENT_FACTORY];
|
|
244
245
|
[GQL_ELEMENT_CONTEXT] = null;
|
|
245
|
-
constructor(define$1) {
|
|
246
|
+
constructor(define$1, getDeps) {
|
|
246
247
|
let cache = null;
|
|
247
|
-
|
|
248
|
+
let promise = null;
|
|
249
|
+
this[GQL_ELEMENT_FACTORY] = function* execute(context) {
|
|
248
250
|
if (cache) return cache.value;
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
251
|
+
if (promise) {
|
|
252
|
+
yield promise;
|
|
253
|
+
return cache.value;
|
|
254
|
+
}
|
|
255
|
+
if (getDeps) for (const dep of getDeps()) yield* GqlElement.createEvaluationGenerator(dep);
|
|
256
|
+
const defined = define$1(context);
|
|
257
|
+
if (!(defined instanceof Promise)) return (cache = { value: defined }).value;
|
|
258
|
+
promise = defined.then((value) => {
|
|
259
|
+
cache = { value };
|
|
260
|
+
promise = null;
|
|
261
|
+
});
|
|
262
|
+
yield promise;
|
|
263
|
+
return cache.value;
|
|
252
264
|
};
|
|
253
265
|
Object.defineProperty(this, "$infer", { get() {
|
|
254
266
|
throw new Error("This property is only for type meta. Do not access this property directly.");
|
|
@@ -257,12 +269,21 @@ var GqlElement = class GqlElement {
|
|
|
257
269
|
static setContext(element, context) {
|
|
258
270
|
element[GQL_ELEMENT_CONTEXT] = context;
|
|
259
271
|
}
|
|
260
|
-
static
|
|
261
|
-
|
|
272
|
+
static *createEvaluationGenerator(element) {
|
|
273
|
+
const context = element[GQL_ELEMENT_CONTEXT];
|
|
274
|
+
yield* element[GQL_ELEMENT_FACTORY](context);
|
|
262
275
|
}
|
|
263
|
-
static
|
|
276
|
+
static evaluateInstantly(element) {
|
|
264
277
|
const context = element[GQL_ELEMENT_CONTEXT];
|
|
265
|
-
|
|
278
|
+
const result = element[GQL_ELEMENT_FACTORY](context).next();
|
|
279
|
+
if (!result.done) throw new Error("Async operation is not supported in sync evaluation.");
|
|
280
|
+
return result.value;
|
|
281
|
+
}
|
|
282
|
+
static evaluateSync(element) {
|
|
283
|
+
GqlElement.evaluateInstantly(element);
|
|
284
|
+
}
|
|
285
|
+
static get(element) {
|
|
286
|
+
return GqlElement.evaluateInstantly(element);
|
|
266
287
|
}
|
|
267
288
|
};
|
|
268
289
|
|
|
@@ -290,6 +311,9 @@ var ComposedOperation = class ComposedOperation extends GqlElement {
|
|
|
290
311
|
get parse() {
|
|
291
312
|
return GqlElement.get(this).parse;
|
|
292
313
|
}
|
|
314
|
+
get metadata() {
|
|
315
|
+
return GqlElement.get(this).metadata;
|
|
316
|
+
}
|
|
293
317
|
static create(define$1) {
|
|
294
318
|
return new ComposedOperation(define$1);
|
|
295
319
|
}
|
|
@@ -320,6 +344,9 @@ var InlineOperation = class InlineOperation extends GqlElement {
|
|
|
320
344
|
get document() {
|
|
321
345
|
return GqlElement.get(this).document;
|
|
322
346
|
}
|
|
347
|
+
get metadata() {
|
|
348
|
+
return GqlElement.get(this).metadata;
|
|
349
|
+
}
|
|
323
350
|
static create(define$1) {
|
|
324
351
|
return new InlineOperation(define$1);
|
|
325
352
|
}
|
|
@@ -395,31 +422,48 @@ function createPathGraphFromSliceEntries(fragments) {
|
|
|
395
422
|
|
|
396
423
|
//#endregion
|
|
397
424
|
//#region packages/core/src/composer/composed-operation.ts
|
|
398
|
-
const createComposedOperationComposerFactory = () => {
|
|
425
|
+
const createComposedOperationComposerFactory = (options = {}) => {
|
|
426
|
+
const { metadataAdapter } = options;
|
|
399
427
|
return (operationType) => {
|
|
400
|
-
return (options, builder) => {
|
|
428
|
+
return (options$1, builder) => {
|
|
401
429
|
return ComposedOperation.create(() => {
|
|
402
|
-
const { operationName } = options;
|
|
403
|
-
const variables = mergeVarDefinitions(options.variables ?? []);
|
|
404
|
-
const
|
|
430
|
+
const { operationName } = options$1;
|
|
431
|
+
const variables = mergeVarDefinitions(options$1.variables ?? []);
|
|
432
|
+
const $ = createVarRefs(variables);
|
|
433
|
+
const fragments = builder({ $ });
|
|
405
434
|
const fields = Object.fromEntries(Object.entries(fragments).flatMap(([label, { getFields: fields$1 }]) => Object.entries(fields$1).map(([key, reference]) => [`${label}_${key}`, reference])));
|
|
406
435
|
const projectionPathGraph = createPathGraphFromSliceEntries(fragments);
|
|
407
|
-
|
|
408
|
-
operationType,
|
|
436
|
+
const document = buildDocument({
|
|
409
437
|
operationName,
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
438
|
+
operationType,
|
|
439
|
+
variables,
|
|
440
|
+
fields
|
|
441
|
+
});
|
|
442
|
+
const operationMetadataResult = options$1.metadata?.({
|
|
443
|
+
$,
|
|
444
|
+
document
|
|
445
|
+
});
|
|
446
|
+
const sliceMetadataResults = Object.values(fragments).map((fragment) => fragment.metadata).filter((m) => m != null);
|
|
447
|
+
const mergeSliceMetadata = metadataAdapter?.mergeSliceMetadata ?? defaultMergeSliceMetadata;
|
|
448
|
+
const createDefinition = (operationMetadata, sliceMetadataList) => {
|
|
449
|
+
const metadata = mergeSliceMetadata(operationMetadata ?? {}, sliceMetadataList);
|
|
450
|
+
return {
|
|
414
451
|
operationType,
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
452
|
+
operationName,
|
|
453
|
+
variableNames: Object.keys(variables),
|
|
454
|
+
projectionPathGraph,
|
|
455
|
+
document,
|
|
456
|
+
parse: createExecutionResultParser({
|
|
457
|
+
fragments,
|
|
458
|
+
projectionPathGraph
|
|
459
|
+
}),
|
|
460
|
+
metadata: Object.keys(metadata).length > 0 ? metadata : void 0
|
|
461
|
+
};
|
|
422
462
|
};
|
|
463
|
+
const hasAsyncOperationMetadata = operationMetadataResult instanceof Promise;
|
|
464
|
+
const hasAsyncSliceMetadata = sliceMetadataResults.some((m) => m instanceof Promise);
|
|
465
|
+
if (hasAsyncOperationMetadata || hasAsyncSliceMetadata) return Promise.all([hasAsyncOperationMetadata ? operationMetadataResult : Promise.resolve(operationMetadataResult), Promise.all(sliceMetadataResults.map((m) => m instanceof Promise ? m : Promise.resolve(m)))]).then(([operationMetadata, sliceMetadataList]) => createDefinition(operationMetadata, sliceMetadataList));
|
|
466
|
+
return createDefinition(operationMetadataResult, sliceMetadataResults);
|
|
423
467
|
});
|
|
424
468
|
};
|
|
425
469
|
};
|
|
@@ -513,18 +557,26 @@ const createInlineOperationComposerFactory = (schema) => {
|
|
|
513
557
|
f: createFieldFactories(schema, operationTypeName),
|
|
514
558
|
$
|
|
515
559
|
}));
|
|
516
|
-
|
|
560
|
+
const document = buildDocument({
|
|
561
|
+
operationName,
|
|
562
|
+
operationType,
|
|
563
|
+
variables,
|
|
564
|
+
fields
|
|
565
|
+
});
|
|
566
|
+
const metadataResult = options.metadata?.({
|
|
567
|
+
$,
|
|
568
|
+
document
|
|
569
|
+
});
|
|
570
|
+
const createDefinition = (metadata) => ({
|
|
517
571
|
operationType,
|
|
518
572
|
operationName,
|
|
519
573
|
variableNames: Object.keys(variables),
|
|
520
574
|
documentSource: () => fields,
|
|
521
|
-
document
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
})
|
|
527
|
-
};
|
|
575
|
+
document,
|
|
576
|
+
metadata
|
|
577
|
+
});
|
|
578
|
+
if (metadataResult instanceof Promise) return metadataResult.then(createDefinition);
|
|
579
|
+
return createDefinition(metadataResult);
|
|
528
580
|
});
|
|
529
581
|
};
|
|
530
582
|
};
|
|
@@ -560,17 +612,21 @@ const createSliceComposerFactory = (schema) => {
|
|
|
560
612
|
return (options, fieldBuilder, projectionBuilder) => Slice.create(() => {
|
|
561
613
|
const varDefinitions = mergeVarDefinitions(options.variables ?? []);
|
|
562
614
|
const projection = handleProjectionBuilder(projectionBuilder);
|
|
615
|
+
const { metadata: metadataOption } = options;
|
|
563
616
|
return {
|
|
564
617
|
operationType,
|
|
565
618
|
embed: (variables) => {
|
|
619
|
+
const f = createFieldFactories(schema, operationTypeName);
|
|
620
|
+
const $ = createVarAssignments(varDefinitions, variables);
|
|
566
621
|
const fields = mergeFields(fieldBuilder({
|
|
567
|
-
f
|
|
568
|
-
|
|
622
|
+
f,
|
|
623
|
+
$
|
|
569
624
|
}));
|
|
570
625
|
return {
|
|
571
626
|
variables,
|
|
572
627
|
getFields: () => fields,
|
|
573
|
-
projection
|
|
628
|
+
projection,
|
|
629
|
+
metadata: metadataOption?.({ $ })
|
|
574
630
|
};
|
|
575
631
|
}
|
|
576
632
|
};
|
|
@@ -592,7 +648,7 @@ function parseModifiedTypeName(nameAndModifier) {
|
|
|
592
648
|
//#endregion
|
|
593
649
|
//#region packages/core/src/composer/var-builder.ts
|
|
594
650
|
const createVarBuilder = (schema) => {
|
|
595
|
-
const
|
|
651
|
+
const varBuilder = (varName) => {
|
|
596
652
|
const createVarSpecifierBuilder = (kind) => {
|
|
597
653
|
return (type, extras) => wrapByKey(varName, {
|
|
598
654
|
kind,
|
|
@@ -611,15 +667,20 @@ const createVarBuilder = (schema) => {
|
|
|
611
667
|
}
|
|
612
668
|
};
|
|
613
669
|
};
|
|
614
|
-
return { $
|
|
670
|
+
return { $var: Object.assign(varBuilder, {
|
|
671
|
+
getName: getVarRefName,
|
|
672
|
+
getValue: getVarRefValue,
|
|
673
|
+
getInner: getVarRefInner
|
|
674
|
+
}) };
|
|
615
675
|
};
|
|
616
676
|
|
|
617
677
|
//#endregion
|
|
618
678
|
//#region packages/core/src/composer/gql-composer.ts
|
|
619
|
-
const createGqlElementComposer = (schema) => {
|
|
679
|
+
const createGqlElementComposer = (schema, options = {}) => {
|
|
680
|
+
const { metadataAdapter, helpers } = options;
|
|
620
681
|
const model = createGqlModelComposers(schema);
|
|
621
682
|
const createSliceComposer = createSliceComposerFactory(schema);
|
|
622
|
-
const createComposedOperationFactory = createComposedOperationComposerFactory();
|
|
683
|
+
const createComposedOperationFactory = createComposedOperationComposerFactory({ metadataAdapter });
|
|
623
684
|
const createInlineOperationComposer = createInlineOperationComposerFactory(schema);
|
|
624
685
|
const composers = {
|
|
625
686
|
model,
|
|
@@ -639,11 +700,34 @@ const createGqlElementComposer = (schema) => {
|
|
|
639
700
|
inline: createInlineOperationComposer("subscription")
|
|
640
701
|
}
|
|
641
702
|
};
|
|
642
|
-
const helper = {
|
|
703
|
+
const helper = {
|
|
704
|
+
...createVarBuilder(schema),
|
|
705
|
+
...helpers ?? {}
|
|
706
|
+
};
|
|
643
707
|
const elementComposer = (composeElement) => composeElement(composers, helper);
|
|
644
708
|
return elementComposer;
|
|
645
709
|
};
|
|
646
710
|
|
|
711
|
+
//#endregion
|
|
712
|
+
//#region packages/core/src/composer/helpers.ts
|
|
713
|
+
/**
|
|
714
|
+
* Helper function for defining typed helpers with better inference.
|
|
715
|
+
* Use this when you need explicit typing or when type inference needs assistance.
|
|
716
|
+
*
|
|
717
|
+
* @example
|
|
718
|
+
* ```typescript
|
|
719
|
+
* const gql = createGqlElementComposer(schema, {
|
|
720
|
+
* helpers: defineHelpers({
|
|
721
|
+
* auth: {
|
|
722
|
+
* requiresLogin: () => ({ requiresAuth: true }),
|
|
723
|
+
* adminOnly: () => ({ requiresAuth: true, role: 'admin' }),
|
|
724
|
+
* },
|
|
725
|
+
* }),
|
|
726
|
+
* });
|
|
727
|
+
* ```
|
|
728
|
+
*/
|
|
729
|
+
const defineHelpers = (helpers) => helpers;
|
|
730
|
+
|
|
647
731
|
//#endregion
|
|
648
732
|
//#region packages/core/src/utils/type-meta.ts
|
|
649
733
|
const withTypeMeta = (target) => {
|
|
@@ -712,5 +796,5 @@ const define = (name) => ({
|
|
|
712
796
|
const defineOperationRoots = (operationRoots) => operationRoots;
|
|
713
797
|
|
|
714
798
|
//#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 };
|
|
799
|
+
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, defineHelpers, defineOperationRoots, defineScalar, getVarRefInner, getVarRefName, getVarRefValue, mergeFields, mergeVarDefinitions, unsafeInputType, unsafeOutputType };
|
|
716
800
|
//# sourceMappingURL=index.js.map
|