@gqloom/core 0.6.0 → 0.7.1

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/index.js CHANGED
@@ -192,7 +192,7 @@ function listSilk(origin) {
192
192
  [GET_GRAPHQL_TYPE]: () => {
193
193
  let originType = getGraphQLType(origin);
194
194
  if (originType instanceof GraphQLNonNull && originType.ofType instanceof GraphQLList) {
195
- originType = originType.ofType;
195
+ originType = originType.ofType.ofType;
196
196
  }
197
197
  if (originType instanceof GraphQLList) {
198
198
  originType = originType.ofType;
@@ -432,6 +432,9 @@ function pascalCase(str) {
432
432
  (word, index) => index === 0 ? word.charAt(0).toUpperCase() + word.slice(1) : word.charAt(0).toUpperCase() + word.slice(1)
433
433
  ).join("");
434
434
  }
435
+ function capitalize(str) {
436
+ return str.slice(0, 1).toUpperCase() + str.slice(1);
437
+ }
435
438
 
436
439
  // src/utils/error.ts
437
440
  function markErrorLocation(error, ...locations) {
@@ -461,6 +464,72 @@ function markLocation(message, ...locations) {
461
464
  return `[${combinedLocation}] ${newMessage}`;
462
465
  }
463
466
 
467
+ // src/utils/loader.ts
468
+ var EasyDataLoader = class _EasyDataLoader {
469
+ constructor(batchLoadFn) {
470
+ this.batchLoadFn = batchLoadFn;
471
+ this.queue = [];
472
+ this.cache = /* @__PURE__ */ new Map();
473
+ this.resolvers = /* @__PURE__ */ new Map();
474
+ }
475
+ queue;
476
+ cache;
477
+ resolvers;
478
+ load(key) {
479
+ const existing = this.cache.get(key);
480
+ if (existing) return existing;
481
+ const promise = new Promise((resolve, reject) => {
482
+ this.queue.push(key);
483
+ this.resolvers.set(key, [resolve, reject]);
484
+ this.nextTickBatchLoad();
485
+ });
486
+ this.cache.set(key, promise);
487
+ return promise;
488
+ }
489
+ clear() {
490
+ this.queue = [];
491
+ this.cache = /* @__PURE__ */ new Map();
492
+ this.resolvers = /* @__PURE__ */ new Map();
493
+ }
494
+ clearByKey(key) {
495
+ this.queue = this.queue.filter((k) => k !== key);
496
+ this.cache.delete(key);
497
+ this.resolvers.delete(key);
498
+ }
499
+ async executeBatchLoad() {
500
+ if (this.queue.length === 0) return;
501
+ const [keys, resolvers] = [this.queue, this.resolvers];
502
+ this.queue = [];
503
+ this.resolvers = /* @__PURE__ */ new Map();
504
+ try {
505
+ const list = await this.batchLoadFn(keys);
506
+ for (let i = 0; i < list.length; i++) {
507
+ const data = list[i];
508
+ const resolve = resolvers.get(keys[i])?.[0];
509
+ const reject = resolvers.get(keys[i])?.[1];
510
+ if (data instanceof Error) {
511
+ reject?.(data);
512
+ } else {
513
+ resolve?.(data);
514
+ }
515
+ }
516
+ } catch (error) {
517
+ for (const key of keys) {
518
+ const reject = resolvers.get(key)?.[1];
519
+ reject?.(error);
520
+ }
521
+ }
522
+ }
523
+ nextTickPromise;
524
+ nextTickBatchLoad() {
525
+ this.nextTickPromise ??= _EasyDataLoader.nextTick().then(() => this.executeBatchLoad()).finally(() => this.nextTickPromise = void 0);
526
+ return this.nextTickPromise;
527
+ }
528
+ static nextTick() {
529
+ return new Promise((resolve) => setTimeout(resolve));
530
+ }
531
+ };
532
+
464
533
  // src/resolver/input.ts
465
534
  import { GraphQLError } from "graphql";
466
535
  function createInputParser(schema, value) {
@@ -514,8 +583,168 @@ function getStandardValue(result) {
514
583
  else throw new GraphQLError("Invalid input");
515
584
  }
516
585
 
586
+ // src/resolver/resolver-chain-factory.ts
587
+ var BaseChainFactory = class _BaseChainFactory {
588
+ constructor(options) {
589
+ this.options = options;
590
+ }
591
+ static methods() {
592
+ return {
593
+ description: _BaseChainFactory.prototype.description,
594
+ deprecationReason: _BaseChainFactory.prototype.deprecationReason,
595
+ extensions: _BaseChainFactory.prototype.extensions
596
+ };
597
+ }
598
+ description(description) {
599
+ return this.clone({ description });
600
+ }
601
+ deprecationReason(deprecationReason) {
602
+ return this.clone({ deprecationReason });
603
+ }
604
+ extensions(extensions) {
605
+ return this.clone({ extensions });
606
+ }
607
+ use(...middlewares) {
608
+ return this.clone({
609
+ middlewares: [...this.options?.middlewares ?? [], ...middlewares]
610
+ });
611
+ }
612
+ };
613
+ var FieldChainFactory = class _FieldChainFactory extends BaseChainFactory {
614
+ static methods() {
615
+ return {
616
+ ...BaseChainFactory.methods(),
617
+ output: _FieldChainFactory.prototype.output,
618
+ input: _FieldChainFactory.prototype.input,
619
+ resolve: _FieldChainFactory.prototype.resolve,
620
+ clone: _FieldChainFactory.prototype.clone
621
+ };
622
+ }
623
+ clone(options) {
624
+ return new _FieldChainFactory({ ...this.options, ...options });
625
+ }
626
+ use(...middlewares) {
627
+ return super.use(...middlewares);
628
+ }
629
+ output(output) {
630
+ return new _FieldChainFactory({ ...this.options, output });
631
+ }
632
+ input(input) {
633
+ return new _FieldChainFactory({ ...this.options, input });
634
+ }
635
+ resolve(resolve) {
636
+ return createField(this.options?.output, {
637
+ ...this.options,
638
+ resolve
639
+ });
640
+ }
641
+ };
642
+ var QueryChainFactory = class _QueryChainFactory extends BaseChainFactory {
643
+ static methods() {
644
+ return {
645
+ ...BaseChainFactory.methods(),
646
+ output: _QueryChainFactory.prototype.output,
647
+ input: _QueryChainFactory.prototype.input,
648
+ resolve: _QueryChainFactory.prototype.resolve,
649
+ clone: _QueryChainFactory.prototype.clone
650
+ };
651
+ }
652
+ clone(options) {
653
+ return new _QueryChainFactory({ ...this.options, ...options });
654
+ }
655
+ use(...middlewares) {
656
+ return super.use(...middlewares);
657
+ }
658
+ output(output) {
659
+ return new _QueryChainFactory({ ...this.options, output });
660
+ }
661
+ input(input) {
662
+ return new _QueryChainFactory({ ...this.options, input });
663
+ }
664
+ resolve(resolve) {
665
+ return createQuery(this.options?.output, {
666
+ ...this.options,
667
+ resolve
668
+ });
669
+ }
670
+ };
671
+ var MutationChainFactory = class _MutationChainFactory extends BaseChainFactory {
672
+ static methods() {
673
+ return {
674
+ ...BaseChainFactory.methods(),
675
+ output: _MutationChainFactory.prototype.output,
676
+ input: _MutationChainFactory.prototype.input,
677
+ resolve: _MutationChainFactory.prototype.resolve,
678
+ clone: _MutationChainFactory.prototype.clone
679
+ };
680
+ }
681
+ clone(options) {
682
+ return new _MutationChainFactory({ ...this.options, ...options });
683
+ }
684
+ use(...middlewares) {
685
+ return super.use(...middlewares);
686
+ }
687
+ output(output) {
688
+ return new _MutationChainFactory({ ...this.options, output });
689
+ }
690
+ input(input) {
691
+ return new _MutationChainFactory({ ...this.options, input });
692
+ }
693
+ resolve(resolve) {
694
+ return createMutation(this.options?.output, {
695
+ ...this.options,
696
+ resolve
697
+ });
698
+ }
699
+ };
700
+ var SubscriptionChainFactory = class _SubscriptionChainFactory extends BaseChainFactory {
701
+ static methods() {
702
+ return {
703
+ ...BaseChainFactory.methods(),
704
+ output: _SubscriptionChainFactory.prototype.output,
705
+ input: _SubscriptionChainFactory.prototype.input,
706
+ subscribe: _SubscriptionChainFactory.prototype.subscribe,
707
+ clone: _SubscriptionChainFactory.prototype.clone
708
+ };
709
+ }
710
+ clone(options) {
711
+ return new _SubscriptionChainFactory({ ...this.options, ...options });
712
+ }
713
+ use(...middlewares) {
714
+ return super.use(...middlewares);
715
+ }
716
+ output(output) {
717
+ return new _SubscriptionChainFactory({ ...this.options, output });
718
+ }
719
+ input(input) {
720
+ return new _SubscriptionChainFactory({ ...this.options, input });
721
+ }
722
+ subscribe(subscribe) {
723
+ const options = this.options;
724
+ const subscription2 = createSubscription(options?.output, {
725
+ ...options,
726
+ subscribe
727
+ });
728
+ const subscriptionResolve = subscription2.resolve;
729
+ const resolve = (...args) => {
730
+ if (args.length === 1 && typeof args[0] === "function") {
731
+ return createSubscription(options?.output, {
732
+ ...options,
733
+ resolve: args[0],
734
+ subscribe
735
+ });
736
+ }
737
+ return subscriptionResolve(...args);
738
+ };
739
+ return Object.assign(subscription2, { resolve });
740
+ }
741
+ };
742
+
517
743
  // src/resolver/resolver.ts
518
- var query = (output, resolveOrOptions) => {
744
+ var createQuery = (output, resolveOrOptions) => {
745
+ if (resolveOrOptions == null) {
746
+ return new QueryChainFactory({ output });
747
+ }
519
748
  const options = getOperationOptions(resolveOrOptions);
520
749
  const type = "query";
521
750
  return {
@@ -533,7 +762,14 @@ var query = (output, resolveOrOptions) => {
533
762
  type
534
763
  };
535
764
  };
536
- var mutation = (output, resolveOrOptions) => {
765
+ var query = Object.assign(
766
+ createQuery,
767
+ QueryChainFactory.methods()
768
+ );
769
+ var createMutation = (output, resolveOrOptions) => {
770
+ if (resolveOrOptions == null) {
771
+ return new MutationChainFactory({ output });
772
+ }
537
773
  const options = getOperationOptions(resolveOrOptions);
538
774
  const type = "mutation";
539
775
  return {
@@ -551,7 +787,14 @@ var mutation = (output, resolveOrOptions) => {
551
787
  type
552
788
  };
553
789
  };
554
- var baseSilkField = (output, resolveOrOptions) => {
790
+ var mutation = Object.assign(
791
+ createMutation,
792
+ MutationChainFactory.methods()
793
+ );
794
+ var createField = (output, resolveOrOptions) => {
795
+ if (resolveOrOptions == null) {
796
+ return new FieldChainFactory({ output });
797
+ }
555
798
  const options = getOperationOptions(resolveOrOptions);
556
799
  const type = "field";
557
800
  return {
@@ -570,13 +813,15 @@ var baseSilkField = (output, resolveOrOptions) => {
570
813
  };
571
814
  };
572
815
  var field = Object.assign(
573
- baseSilkField,
574
- {
575
- hidden: FIELD_HIDDEN
576
- }
816
+ createField,
817
+ { hidden: FIELD_HIDDEN },
818
+ FieldChainFactory.methods()
577
819
  );
578
820
  var defaultSubscriptionResolve = (source) => source;
579
- var subscription = (output, subscribeOrOptions) => {
821
+ var createSubscription = (output, subscribeOrOptions) => {
822
+ if (subscribeOrOptions == null) {
823
+ return new SubscriptionChainFactory({ output });
824
+ }
580
825
  const options = getSubscriptionOptions(subscribeOrOptions);
581
826
  const type = "subscription";
582
827
  return {
@@ -598,6 +843,10 @@ var subscription = (output, subscribeOrOptions) => {
598
843
  type
599
844
  };
600
845
  };
846
+ var subscription = Object.assign(
847
+ createSubscription,
848
+ SubscriptionChainFactory.methods()
849
+ );
601
850
  var ResolverOptionsMap = /* @__PURE__ */ new WeakMap();
602
851
  function baseResolver(operations, options) {
603
852
  const record = {};
@@ -678,6 +927,9 @@ function createResolverFactory(toSilk) {
678
927
  }
679
928
  function createFieldFactory(toSilk, isSchema) {
680
929
  const baseFieldFunc = (output, resolveOrOptions) => {
930
+ if (resolveOrOptions == null) {
931
+ return new FieldChainFactory({ output: toSilk(output) });
932
+ }
681
933
  const options = getOperationOptions(
682
934
  resolveOrOptions
683
935
  );
@@ -686,12 +938,17 @@ function createFieldFactory(toSilk, isSchema) {
686
938
  input: toSilkInput(options.input, toSilk, isSchema)
687
939
  });
688
940
  };
689
- return Object.assign(baseFieldFunc, {
690
- hidden: FIELD_HIDDEN
691
- });
941
+ return Object.assign(
942
+ baseFieldFunc,
943
+ { hidden: FIELD_HIDDEN },
944
+ FieldChainFactory.methods()
945
+ );
692
946
  }
693
947
  function createQueryFactory(toSilk, isSchema) {
694
948
  return (output, resolveOrOptions) => {
949
+ if (resolveOrOptions == null) {
950
+ return new QueryChainFactory({ output: toSilk(output) });
951
+ }
695
952
  const options = getOperationOptions(resolveOrOptions);
696
953
  return query(toSilk(output), {
697
954
  ...options,
@@ -701,6 +958,9 @@ function createQueryFactory(toSilk, isSchema) {
701
958
  }
702
959
  function createMutationFactory(toSilk, isSchema) {
703
960
  return (output, resolveOrOptions) => {
961
+ if (resolveOrOptions == null) {
962
+ return new MutationChainFactory({ output: toSilk(output) });
963
+ }
704
964
  const options = getOperationOptions(resolveOrOptions);
705
965
  return mutation(toSilk(output), {
706
966
  ...options,
@@ -710,6 +970,9 @@ function createMutationFactory(toSilk, isSchema) {
710
970
  }
711
971
  function createSubscriptionFactory(toSilk, isSchema) {
712
972
  return (output, resolveOrOptions) => {
973
+ if (resolveOrOptions == null) {
974
+ return new SubscriptionChainFactory({ output: toSilk(output) });
975
+ }
713
976
  const options = getSubscriptionOptions(resolveOrOptions);
714
977
  return subscription(toSilk(output), {
715
978
  ...options,
@@ -1290,6 +1553,7 @@ function ensureInterfaceType(gqlType, interfaceConfig) {
1290
1553
  }
1291
1554
  export {
1292
1555
  ContextMemoization,
1556
+ EasyDataLoader,
1293
1557
  GraphQLSchemaLoom,
1294
1558
  LoomObjectType,
1295
1559
  OPERATION_OBJECT_NAMES,
@@ -1297,16 +1561,21 @@ export {
1297
1561
  symbols_exports as SYMBOLS,
1298
1562
  applyMiddlewares,
1299
1563
  baseResolver,
1564
+ capitalize,
1300
1565
  collectName,
1301
1566
  collectNames,
1302
1567
  compose,
1568
+ createField,
1303
1569
  createFieldFactory,
1304
1570
  createInputParser,
1305
1571
  createLoom,
1306
1572
  createMemoization,
1573
+ createMutation,
1307
1574
  createMutationFactory,
1575
+ createQuery,
1308
1576
  createQueryFactory,
1309
1577
  createResolverFactory,
1578
+ createSubscription,
1310
1579
  createSubscriptionFactory,
1311
1580
  deepMerge,
1312
1581
  defaultSubscriptionResolve,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gqloom/core",
3
- "version": "0.6.0",
3
+ "version": "0.7.1",
4
4
  "description": "Create GraphQL schema and resolvers with TypeScript.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -21,11 +21,9 @@
21
21
  }
22
22
  }
23
23
  },
24
- "scripts": {
25
- "build": "tsup --dts-resolve",
26
- "check:type": "tsc --noEmit"
27
- },
28
- "files": ["dist"],
24
+ "files": [
25
+ "dist"
26
+ ],
29
27
  "peerDependencies": {
30
28
  "graphql": ">= 16.8.0"
31
29
  },
@@ -51,5 +49,9 @@
51
49
  },
52
50
  "devDependencies": {
53
51
  "@standard-schema/spec": "1.0.0-beta.4"
52
+ },
53
+ "scripts": {
54
+ "build": "tsup --dts-resolve",
55
+ "check:type": "tsc --noEmit"
54
56
  }
55
- }
57
+ }