@soda-gql/core 0.2.0 → 0.3.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 +60 -48
- 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-Djr9A4KL.d.ts → index-DH3lMepk.d.cts} +133 -221
- package/dist/index-DH3lMepk.d.cts.map +1 -0
- package/dist/{index-B-erotAZ.d.cts → index-WU6aMZjg.d.ts} +133 -221
- package/dist/index-WU6aMZjg.d.ts.map +1 -0
- package/dist/index.cjs +127 -126
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +13 -4
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +13 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +127 -125
- package/dist/index.js.map +1 -1
- package/dist/runtime.d.cts +2 -2
- package/dist/runtime.d.ts +2 -2
- package/dist/{schema-BygZwEX8.d.ts → schema-BElqa12z.d.cts} +86 -60
- package/dist/schema-BElqa12z.d.cts.map +1 -0
- package/dist/{schema-D9wIW5Dl.js → schema-BbCrsNkQ.js} +2 -2
- package/dist/{schema-D9wIW5Dl.js.map → schema-BbCrsNkQ.js.map} +1 -1
- package/dist/{schema-DRkKucYe.d.cts → schema-C7q047S0.d.ts} +86 -60
- package/dist/schema-C7q047S0.d.ts.map +1 -0
- package/dist/{schema-Bip7o0g3.cjs → schema-DuWaRhdp.cjs} +1 -7
- package/dist/{schema-Bip7o0g3.cjs.map → schema-DuWaRhdp.cjs.map} +1 -1
- package/dist/{schema-builder-8zadflz-.d.cts → schema-builder-DDfulXP3.d.cts} +2 -2
- package/dist/{schema-builder-8zadflz-.d.cts.map → schema-builder-DDfulXP3.d.cts.map} +1 -1
- package/dist/{schema-builder-vwQtCGYI.d.ts → schema-builder-DfyTaFMt.d.ts} +2 -2
- package/dist/{schema-builder-vwQtCGYI.d.ts.map → schema-builder-DfyTaFMt.d.ts.map} +1 -1
- package/package.json +1 -1
- package/dist/index-B-erotAZ.d.cts.map +0 -1
- package/dist/index-Djr9A4KL.d.ts.map +0 -1
- package/dist/schema-BygZwEX8.d.ts.map +0 -1
- package/dist/schema-DRkKucYe.d.cts.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as unsafeOutputType, i as unsafeInputType, n as defineOperationRoots, o as
|
|
1
|
+
import { a as unsafeOutputType, i as unsafeInputType, n as defineOperationRoots, o as wrapByKey, r as defineScalar, t as define } from "./schema-BbCrsNkQ.js";
|
|
2
2
|
import { Kind, OperationTypeNode } from "graphql";
|
|
3
3
|
|
|
4
4
|
//#region packages/core/src/types/type-foundation/var-ref.ts
|
|
@@ -294,12 +294,13 @@ const buildDocument = (options) => {
|
|
|
294
294
|
* @example
|
|
295
295
|
* ```typescript
|
|
296
296
|
* // In operation definition
|
|
297
|
-
* query.operation({
|
|
298
|
-
*
|
|
297
|
+
* query.operation({
|
|
298
|
+
* name: "GetData",
|
|
299
|
+
* fields: ({ $ }) => $colocate({
|
|
299
300
|
* userCard: userCardFragment.embed({ userId: $.userId }),
|
|
300
301
|
* posts: postsFragment.embed({ userId: $.userId }),
|
|
301
302
|
* }),
|
|
302
|
-
*
|
|
303
|
+
* });
|
|
303
304
|
*
|
|
304
305
|
* // In parser definition (same labels)
|
|
305
306
|
* createExecutionResultParser({
|
|
@@ -387,8 +388,91 @@ const isListType = (typeString) => {
|
|
|
387
388
|
};
|
|
388
389
|
|
|
389
390
|
//#endregion
|
|
390
|
-
//#region packages/core/src/
|
|
391
|
-
|
|
391
|
+
//#region packages/core/src/utils/map-values.ts
|
|
392
|
+
function mapValues(obj, fn) {
|
|
393
|
+
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, fn(value, key)]));
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
//#endregion
|
|
397
|
+
//#region packages/core/src/composer/fields-builder.ts
|
|
398
|
+
const cacheMapBySchema = /* @__PURE__ */ new WeakMap();
|
|
399
|
+
const ensureCacheMapBySchema = (schema) => {
|
|
400
|
+
const cachedCacheMap = cacheMapBySchema.get(schema);
|
|
401
|
+
if (cachedCacheMap) return cachedCacheMap;
|
|
402
|
+
const cacheMap = /* @__PURE__ */ new Map();
|
|
403
|
+
cacheMapBySchema.set(schema, cacheMap);
|
|
404
|
+
return cacheMap;
|
|
405
|
+
};
|
|
406
|
+
const createFieldFactories = (schema, typeName) => {
|
|
407
|
+
const cacheMap = ensureCacheMapBySchema(schema);
|
|
408
|
+
const cached = cacheMap.get(typeName);
|
|
409
|
+
if (cached) return cached;
|
|
410
|
+
const factories = createFieldFactoriesInner(schema, typeName);
|
|
411
|
+
cacheMap.set(typeName, factories);
|
|
412
|
+
return factories;
|
|
413
|
+
};
|
|
414
|
+
const createFieldFactoriesInner = (schema, typeName) => {
|
|
415
|
+
const typeDef = schema.object[typeName];
|
|
416
|
+
if (!typeDef) throw new Error(`Type ${typeName} is not defined in schema objects`);
|
|
417
|
+
const entries = Object.entries(typeDef.fields).map(([fieldName, type]) => {
|
|
418
|
+
const factory = (fieldArgs, extras) => {
|
|
419
|
+
const wrap = (value) => wrapByKey(extras?.alias ?? fieldName, value);
|
|
420
|
+
if (type.kind === "object") {
|
|
421
|
+
const factoryReturn = ((nest) => {
|
|
422
|
+
const nestedFields = withFieldPath(appendToPath(getCurrentFieldPath(), {
|
|
423
|
+
field: fieldName,
|
|
424
|
+
parentType: typeName,
|
|
425
|
+
isList: isListType(type.modifier)
|
|
426
|
+
}), () => nest({ f: createFieldFactories(schema, type.name) }));
|
|
427
|
+
return wrap({
|
|
428
|
+
parent: typeName,
|
|
429
|
+
field: fieldName,
|
|
430
|
+
type,
|
|
431
|
+
args: fieldArgs ?? {},
|
|
432
|
+
directives: extras?.directives ?? {},
|
|
433
|
+
object: nestedFields,
|
|
434
|
+
union: null
|
|
435
|
+
});
|
|
436
|
+
});
|
|
437
|
+
return factoryReturn;
|
|
438
|
+
}
|
|
439
|
+
if (type.kind === "union") {
|
|
440
|
+
const factoryReturn = ((nest) => {
|
|
441
|
+
const nestedUnion = withFieldPath(appendToPath(getCurrentFieldPath(), {
|
|
442
|
+
field: fieldName,
|
|
443
|
+
parentType: typeName,
|
|
444
|
+
isList: isListType(type.modifier)
|
|
445
|
+
}), () => mapValues(nest, (builder, memberName) => {
|
|
446
|
+
if (!builder) throw new Error(`Builder is undefined for member name: ${memberName}`);
|
|
447
|
+
return builder({ f: createFieldFactories(schema, memberName) });
|
|
448
|
+
}));
|
|
449
|
+
return wrap({
|
|
450
|
+
parent: typeName,
|
|
451
|
+
field: fieldName,
|
|
452
|
+
type,
|
|
453
|
+
args: fieldArgs ?? {},
|
|
454
|
+
directives: extras?.directives ?? {},
|
|
455
|
+
object: null,
|
|
456
|
+
union: nestedUnion
|
|
457
|
+
});
|
|
458
|
+
});
|
|
459
|
+
return factoryReturn;
|
|
460
|
+
}
|
|
461
|
+
if (type.kind === "scalar" || type.kind === "enum" || type.kind === "typename") return wrap({
|
|
462
|
+
parent: typeName,
|
|
463
|
+
field: fieldName,
|
|
464
|
+
type,
|
|
465
|
+
args: fieldArgs ?? {},
|
|
466
|
+
directives: extras?.directives ?? {},
|
|
467
|
+
object: null,
|
|
468
|
+
union: null
|
|
469
|
+
});
|
|
470
|
+
throw new Error(`Unsupported field type: ${type}`);
|
|
471
|
+
};
|
|
472
|
+
return [fieldName, factory];
|
|
473
|
+
});
|
|
474
|
+
return Object.fromEntries(entries);
|
|
475
|
+
};
|
|
392
476
|
|
|
393
477
|
//#endregion
|
|
394
478
|
//#region packages/core/src/types/element/lazy-evaluator.ts
|
|
@@ -522,93 +606,6 @@ var Operation = class Operation extends GqlElement {
|
|
|
522
606
|
}
|
|
523
607
|
};
|
|
524
608
|
|
|
525
|
-
//#endregion
|
|
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)]));
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
//#endregion
|
|
532
|
-
//#region packages/core/src/composer/fields-builder.ts
|
|
533
|
-
const cacheMapBySchema = /* @__PURE__ */ new WeakMap();
|
|
534
|
-
const ensureCacheMapBySchema = (schema) => {
|
|
535
|
-
const cachedCacheMap = cacheMapBySchema.get(schema);
|
|
536
|
-
if (cachedCacheMap) return cachedCacheMap;
|
|
537
|
-
const cacheMap = /* @__PURE__ */ new Map();
|
|
538
|
-
cacheMapBySchema.set(schema, cacheMap);
|
|
539
|
-
return cacheMap;
|
|
540
|
-
};
|
|
541
|
-
const createFieldFactories = (schema, typeName) => {
|
|
542
|
-
const cacheMap = ensureCacheMapBySchema(schema);
|
|
543
|
-
const cached = cacheMap.get(typeName);
|
|
544
|
-
if (cached) return cached;
|
|
545
|
-
const factories = createFieldFactoriesInner(schema, typeName);
|
|
546
|
-
cacheMap.set(typeName, factories);
|
|
547
|
-
return factories;
|
|
548
|
-
};
|
|
549
|
-
const createFieldFactoriesInner = (schema, typeName) => {
|
|
550
|
-
const typeDef = schema.object[typeName];
|
|
551
|
-
if (!typeDef) throw new Error(`Type ${typeName} is not defined in schema objects`);
|
|
552
|
-
const entries = Object.entries(typeDef.fields).map(([fieldName, type]) => {
|
|
553
|
-
const factory = (fieldArgs, extras) => {
|
|
554
|
-
const wrap = (value) => wrapByKey(extras?.alias ?? fieldName, value);
|
|
555
|
-
if (type.kind === "object") {
|
|
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
|
-
});
|
|
572
|
-
return factoryReturn;
|
|
573
|
-
}
|
|
574
|
-
if (type.kind === "union") {
|
|
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) => {
|
|
581
|
-
if (!builder) throw new Error(`Builder is undefined for member name: ${memberName}`);
|
|
582
|
-
return mergeFields(builder({ f: createFieldFactories(schema, memberName) }));
|
|
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
|
-
});
|
|
594
|
-
return factoryReturn;
|
|
595
|
-
}
|
|
596
|
-
if (type.kind === "scalar" || type.kind === "enum" || type.kind === "typename") return wrap({
|
|
597
|
-
parent: typeName,
|
|
598
|
-
field: fieldName,
|
|
599
|
-
type,
|
|
600
|
-
args: fieldArgs ?? {},
|
|
601
|
-
directives: extras?.directives ?? {},
|
|
602
|
-
object: null,
|
|
603
|
-
union: null
|
|
604
|
-
});
|
|
605
|
-
throw new Error(`Unsupported field type: ${type}`);
|
|
606
|
-
};
|
|
607
|
-
return [fieldName, factory];
|
|
608
|
-
});
|
|
609
|
-
return Object.fromEntries(entries);
|
|
610
|
-
};
|
|
611
|
-
|
|
612
609
|
//#endregion
|
|
613
610
|
//#region packages/core/src/composer/fragment-usage-context.ts
|
|
614
611
|
/**
|
|
@@ -647,7 +644,6 @@ const recordFragmentUsage = (record) => {
|
|
|
647
644
|
|
|
648
645
|
//#endregion
|
|
649
646
|
//#region packages/core/src/composer/input.ts
|
|
650
|
-
const mergeVarDefinitions = (definitions) => Object.assign({}, ...definitions);
|
|
651
647
|
const createVarAssignments = (definitions, providedValues) => {
|
|
652
648
|
return mapValues(definitions, (_definition, key) => {
|
|
653
649
|
const varName = key;
|
|
@@ -663,9 +659,9 @@ const createVarRefs = (definitions) => mapValues(definitions, (_ref, name) => cr
|
|
|
663
659
|
//#region packages/core/src/composer/fragment.ts
|
|
664
660
|
const createGqlFragmentComposers = (schema, _adapter) => {
|
|
665
661
|
const createFragmentComposer = (typename) => {
|
|
666
|
-
return (options
|
|
667
|
-
const varDefinitions =
|
|
668
|
-
const { metadata } = options;
|
|
662
|
+
return (options) => {
|
|
663
|
+
const varDefinitions = options.variables ?? {};
|
|
664
|
+
const { metadata, fields } = options;
|
|
669
665
|
return Fragment.create(() => ({
|
|
670
666
|
typename,
|
|
671
667
|
embed: (variables) => {
|
|
@@ -675,10 +671,10 @@ const createGqlFragmentComposers = (schema, _adapter) => {
|
|
|
675
671
|
metadataBuilder: metadata ? () => metadata({ $ }) : null,
|
|
676
672
|
path: getCurrentFieldPath()
|
|
677
673
|
});
|
|
678
|
-
return
|
|
674
|
+
return fields({
|
|
679
675
|
f,
|
|
680
676
|
$
|
|
681
|
-
})
|
|
677
|
+
});
|
|
682
678
|
}
|
|
683
679
|
}));
|
|
684
680
|
};
|
|
@@ -705,16 +701,16 @@ const createOperationComposerFactory = (schema, adapter) => {
|
|
|
705
701
|
return (operationType) => {
|
|
706
702
|
const operationTypeName = schema.operations[operationType];
|
|
707
703
|
if (operationTypeName === null) throw new Error(`Operation type ${operationType} is not defined in schema roots`);
|
|
708
|
-
return (options
|
|
704
|
+
return (options) => {
|
|
709
705
|
return Operation.create(() => {
|
|
710
706
|
const { name: operationName } = options;
|
|
711
|
-
const variables =
|
|
707
|
+
const variables = options.variables ?? {};
|
|
712
708
|
const $ = createVarRefs(variables);
|
|
713
709
|
const f = createFieldFactories(schema, operationTypeName);
|
|
714
|
-
const { result: fields, usages: fragmentUsages } = withFragmentUsageCollection(() =>
|
|
710
|
+
const { result: fields, usages: fragmentUsages } = withFragmentUsageCollection(() => options.fields({
|
|
715
711
|
f,
|
|
716
712
|
$
|
|
717
|
-
}))
|
|
713
|
+
}));
|
|
718
714
|
const document = buildDocument({
|
|
719
715
|
operationName,
|
|
720
716
|
operationType,
|
|
@@ -759,25 +755,31 @@ const createOperationComposerFactory = (schema, adapter) => {
|
|
|
759
755
|
|
|
760
756
|
//#endregion
|
|
761
757
|
//#region packages/core/src/composer/var-builder.ts
|
|
762
|
-
|
|
758
|
+
/**
|
|
759
|
+
* Creates a variable method for a specific input type.
|
|
760
|
+
* This is used by codegen to generate type-specific variable methods.
|
|
761
|
+
*/
|
|
762
|
+
const createVarMethod = (kind, typeName) => {
|
|
763
|
+
return (modifier, extras) => ({
|
|
764
|
+
kind,
|
|
765
|
+
name: typeName,
|
|
766
|
+
modifier,
|
|
767
|
+
defaultValue: extras?.default ? { default: extras.default() } : null,
|
|
768
|
+
directives: extras?.directives ?? {}
|
|
769
|
+
});
|
|
770
|
+
};
|
|
771
|
+
/**
|
|
772
|
+
* Creates a variable builder that uses injected input type methods.
|
|
773
|
+
*/
|
|
774
|
+
const createVarBuilder = (inputTypeMethods) => {
|
|
763
775
|
const varBuilder = (varName) => {
|
|
764
|
-
const
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
return {
|
|
772
|
-
scalar: createVarSpecifierBuilder("scalar"),
|
|
773
|
-
enum: createVarSpecifierBuilder("enum"),
|
|
774
|
-
input: createVarSpecifierBuilder("input"),
|
|
775
|
-
byField: (typeName, fieldName, argName) => {
|
|
776
|
-
const argTypeRef = schema.object[typeName]?.fields[fieldName]?.arguments[argName];
|
|
777
|
-
if (!argTypeRef) throw new Error(`Argument ${argName} not found in field ${fieldName} of type ${typeName}`);
|
|
778
|
-
return { ...argTypeRef };
|
|
779
|
-
}
|
|
780
|
-
};
|
|
776
|
+
const wrappedMethods = {};
|
|
777
|
+
for (const [typeName, method] of Object.entries(inputTypeMethods)) Object.defineProperty(wrappedMethods, typeName, {
|
|
778
|
+
value: ((modifier, extras) => wrapByKey(varName, method(modifier, extras))),
|
|
779
|
+
writable: false,
|
|
780
|
+
configurable: true
|
|
781
|
+
});
|
|
782
|
+
return wrappedMethods;
|
|
781
783
|
};
|
|
782
784
|
varBuilder.getName = getVarRefName;
|
|
783
785
|
varBuilder.getValue = getVarRefValue;
|
|
@@ -787,8 +789,8 @@ const createVarBuilder = (schema) => {
|
|
|
787
789
|
|
|
788
790
|
//#endregion
|
|
789
791
|
//#region packages/core/src/composer/gql-composer.ts
|
|
790
|
-
const createGqlElementComposer = (schema, options
|
|
791
|
-
const { adapter } = options;
|
|
792
|
+
const createGqlElementComposer = (schema, options) => {
|
|
793
|
+
const { adapter, inputTypeMethods } = options;
|
|
792
794
|
const helpers = adapter?.helpers;
|
|
793
795
|
const metadataAdapter = adapter?.metadata;
|
|
794
796
|
const fragment = createGqlFragmentComposers(schema, metadataAdapter);
|
|
@@ -800,7 +802,7 @@ const createGqlElementComposer = (schema, options = {}) => {
|
|
|
800
802
|
subscription: { operation: createOperationComposer("subscription") }
|
|
801
803
|
};
|
|
802
804
|
const helper = {
|
|
803
|
-
$var: createVarBuilder(
|
|
805
|
+
$var: createVarBuilder(inputTypeMethods),
|
|
804
806
|
$colocate: createColocateHelper(),
|
|
805
807
|
...helpers ?? {}
|
|
806
808
|
};
|
|
@@ -809,5 +811,5 @@ const createGqlElementComposer = (schema, options = {}) => {
|
|
|
809
811
|
};
|
|
810
812
|
|
|
811
813
|
//#endregion
|
|
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,
|
|
814
|
+
export { Fragment, GqlElement, Operation, appendToPath, buildArgumentValue, buildConstValueNode, buildDocument, buildOperationTypeNode, buildWithTypeModifier, createColocateHelper, createDefaultAdapter, createFieldFactories, createGqlElementComposer, createGqlFragmentComposers, createOperationComposerFactory, createVarAssignments, createVarBuilder, createVarMethod, createVarRefs, defaultMetadataAdapter, define, defineOperationRoots, defineScalar, getCurrentFieldPath, getVarRefInner, getVarRefName, getVarRefValue, isListType, recordFragmentUsage, unsafeInputType, unsafeOutputType, withFieldPath, withFragmentUsageCollection };
|
|
813
815
|
//# sourceMappingURL=index.js.map
|