@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.
Files changed (36) hide show
  1. package/README.md +60 -48
  2. package/dist/adapter.cjs +1 -1
  3. package/dist/adapter.d.cts +2 -2
  4. package/dist/adapter.d.ts +2 -2
  5. package/dist/adapter.js +1 -1
  6. package/dist/{index-Djr9A4KL.d.ts → index-DH3lMepk.d.cts} +133 -221
  7. package/dist/index-DH3lMepk.d.cts.map +1 -0
  8. package/dist/{index-B-erotAZ.d.cts → index-WU6aMZjg.d.ts} +133 -221
  9. package/dist/index-WU6aMZjg.d.ts.map +1 -0
  10. package/dist/index.cjs +127 -126
  11. package/dist/index.cjs.map +1 -1
  12. package/dist/index.d.cts +13 -4
  13. package/dist/index.d.cts.map +1 -1
  14. package/dist/index.d.ts +13 -4
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +127 -125
  17. package/dist/index.js.map +1 -1
  18. package/dist/runtime.d.cts +2 -2
  19. package/dist/runtime.d.ts +2 -2
  20. package/dist/{schema-BygZwEX8.d.ts → schema-BElqa12z.d.cts} +86 -60
  21. package/dist/schema-BElqa12z.d.cts.map +1 -0
  22. package/dist/{schema-D9wIW5Dl.js → schema-BbCrsNkQ.js} +2 -2
  23. package/dist/{schema-D9wIW5Dl.js.map → schema-BbCrsNkQ.js.map} +1 -1
  24. package/dist/{schema-DRkKucYe.d.cts → schema-C7q047S0.d.ts} +86 -60
  25. package/dist/schema-C7q047S0.d.ts.map +1 -0
  26. package/dist/{schema-Bip7o0g3.cjs → schema-DuWaRhdp.cjs} +1 -7
  27. package/dist/{schema-Bip7o0g3.cjs.map → schema-DuWaRhdp.cjs.map} +1 -1
  28. package/dist/{schema-builder-8zadflz-.d.cts → schema-builder-DDfulXP3.d.cts} +2 -2
  29. package/dist/{schema-builder-8zadflz-.d.cts.map → schema-builder-DDfulXP3.d.cts.map} +1 -1
  30. package/dist/{schema-builder-vwQtCGYI.d.ts → schema-builder-DfyTaFMt.d.ts} +2 -2
  31. package/dist/{schema-builder-vwQtCGYI.d.ts.map → schema-builder-DfyTaFMt.d.ts.map} +1 -1
  32. package/package.json +1 -1
  33. package/dist/index-B-erotAZ.d.cts.map +0 -1
  34. package/dist/index-Djr9A4KL.d.ts.map +0 -1
  35. package/dist/schema-BygZwEX8.d.ts.map +0 -1
  36. package/dist/schema-DRkKucYe.d.cts.map +0 -1
package/dist/index.cjs CHANGED
@@ -1,4 +1,4 @@
1
- const require_schema = require('./schema-Bip7o0g3.cjs');
1
+ const require_schema = require('./schema-DuWaRhdp.cjs');
2
2
  let graphql = require("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({ name: "GetData" }, ({ f, $ }) => [
298
- * $colocate({
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/types/element/fields-builder.ts
391
- const mergeFields = (fields) => Object.assign({}, ...fields);
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) => require_schema.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) => require_schema.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, builder) => {
667
- const varDefinitions = mergeVarDefinitions(options.variables ?? []);
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 mergeFields(builder({
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, fieldBuilder) => {
704
+ return (options) => {
709
705
  return Operation.create(() => {
710
706
  const { name: operationName } = options;
711
- const variables = mergeVarDefinitions(options.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(() => mergeFields(fieldBuilder({
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
- const createVarBuilder = (schema) => {
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 createVarSpecifierBuilder = (kind) => {
765
- return (type, extras) => require_schema.wrapByKey(varName, {
766
- kind,
767
- ...require_schema.parseModifiedTypeName(type),
768
- defaultValue: extras?.default ? { default: extras.default() } : null
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) => require_schema.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(schema),
805
+ $var: createVarBuilder(inputTypeMethods),
804
806
  $colocate: createColocateHelper(),
805
807
  ...helpers ?? {}
806
808
  };
@@ -826,6 +828,7 @@ exports.createGqlFragmentComposers = createGqlFragmentComposers;
826
828
  exports.createOperationComposerFactory = createOperationComposerFactory;
827
829
  exports.createVarAssignments = createVarAssignments;
828
830
  exports.createVarBuilder = createVarBuilder;
831
+ exports.createVarMethod = createVarMethod;
829
832
  exports.createVarRefs = createVarRefs;
830
833
  exports.defaultMetadataAdapter = defaultMetadataAdapter;
831
834
  exports.define = require_schema.define;
@@ -836,8 +839,6 @@ exports.getVarRefInner = getVarRefInner;
836
839
  exports.getVarRefName = getVarRefName;
837
840
  exports.getVarRefValue = getVarRefValue;
838
841
  exports.isListType = isListType;
839
- exports.mergeFields = mergeFields;
840
- exports.mergeVarDefinitions = mergeVarDefinitions;
841
842
  exports.recordFragmentUsage = recordFragmentUsage;
842
843
  exports.unsafeInputType = require_schema.unsafeInputType;
843
844
  exports.unsafeOutputType = require_schema.unsafeOutputType;