@envelop/core 1.8.0-alpha-f56d3a3.0 → 2.0.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 (3) hide show
  1. package/index.js +109 -105
  2. package/index.mjs +110 -106
  3. package/package.json +2 -2
package/index.js CHANGED
@@ -303,11 +303,19 @@ function errorAsyncIterator(source, onError) {
303
303
  function createEnvelopOrchestrator(plugins) {
304
304
  let schema = null;
305
305
  let initDone = false;
306
+ const onResolversHandlers = [];
307
+ for (const plugin of plugins) {
308
+ if (plugin.onResolverCalled) {
309
+ onResolversHandlers.push(plugin.onResolverCalled);
310
+ }
311
+ }
306
312
  // Define the initial method for replacing the GraphQL schema, this is needed in order
307
313
  // to allow setting the schema from the onPluginInit callback. We also need to make sure
308
314
  // here not to call the same plugin that initiated the schema switch.
309
315
  const replaceSchema = (newSchema, ignorePluginIndex = -1) => {
310
- prepareTracedSchema(newSchema);
316
+ if (onResolversHandlers.length) {
317
+ prepareTracedSchema(newSchema);
318
+ }
311
319
  schema = newSchema;
312
320
  if (initDone) {
313
321
  for (const [i, plugin] of plugins.entries()) {
@@ -519,101 +527,100 @@ function createEnvelopOrchestrator(plugins) {
519
527
  }
520
528
  }
521
529
  : initialContext => orchestratorCtx => orchestratorCtx ? { ...initialContext, ...orchestratorCtx } : initialContext;
522
- const customSubscribe = makeSubscribe(async (args) => {
523
- const onResolversHandlers = [];
524
- let subscribeFn = graphql.subscribe;
525
- const afterCalls = [];
526
- const subscribeErrorHandlers = [];
527
- let context = args.contextValue || {};
528
- for (const onSubscribe of beforeCallbacks.subscribe) {
529
- const after = await onSubscribe({
530
- subscribeFn,
531
- setSubscribeFn: newSubscribeFn => {
532
- subscribeFn = newSubscribeFn;
533
- },
534
- extendContext: extension => {
535
- context = { ...context, ...extension };
536
- },
537
- args: args,
538
- });
539
- if (after) {
540
- if (after.onSubscribeResult) {
541
- afterCalls.push(after.onSubscribeResult);
542
- }
543
- if (after.onSubscribeError) {
544
- subscribeErrorHandlers.push(after.onSubscribeError);
545
- }
546
- if (after.onResolverCalled) {
547
- onResolversHandlers.push(after.onResolverCalled);
530
+ const useCustomSubscribe = beforeCallbacks.subscribe.length || onResolversHandlers.length;
531
+ const customSubscribe = useCustomSubscribe
532
+ ? makeSubscribe(async (args) => {
533
+ let subscribeFn = graphql.subscribe;
534
+ const afterCalls = [];
535
+ const subscribeErrorHandlers = [];
536
+ let context = args.contextValue || {};
537
+ for (const onSubscribe of beforeCallbacks.subscribe) {
538
+ const after = await onSubscribe({
539
+ subscribeFn,
540
+ setSubscribeFn: newSubscribeFn => {
541
+ subscribeFn = newSubscribeFn;
542
+ },
543
+ extendContext: extension => {
544
+ context = { ...context, ...extension };
545
+ },
546
+ args: args,
547
+ });
548
+ if (after) {
549
+ if (after.onSubscribeResult) {
550
+ afterCalls.push(after.onSubscribeResult);
551
+ }
552
+ if (after.onSubscribeError) {
553
+ subscribeErrorHandlers.push(after.onSubscribeError);
554
+ }
548
555
  }
549
556
  }
550
- }
551
- if (onResolversHandlers.length) {
552
- context[resolversHooksSymbol] = onResolversHandlers;
553
- }
554
- let result = await subscribeFn({
555
- ...args,
556
- contextValue: context,
557
- // Casted for GraphQL.js 15 compatibility
558
- // Can be removed once we drop support for GraphQL.js 15
559
- });
560
- const onNextHandler = [];
561
- const onEndHandler = [];
562
- for (const afterCb of afterCalls) {
563
- const hookResult = afterCb({
564
- args: args,
565
- result,
566
- setResult: newResult => {
567
- result = newResult;
568
- },
569
- });
570
- if (hookResult) {
571
- if (hookResult.onNext) {
572
- onNextHandler.push(hookResult.onNext);
573
- }
574
- if (hookResult.onEnd) {
575
- onEndHandler.push(hookResult.onEnd);
576
- }
557
+ if (onResolversHandlers.length) {
558
+ context[resolversHooksSymbol] = onResolversHandlers;
577
559
  }
578
- }
579
- if (onNextHandler.length && isAsyncIterable(result)) {
580
- result = mapAsyncIterator(result, async (result) => {
581
- for (const onNext of onNextHandler) {
582
- await onNext({
583
- args: args,
584
- result,
585
- setResult: newResult => (result = newResult),
586
- });
587
- }
588
- return result;
589
- });
590
- }
591
- if (onEndHandler.length && isAsyncIterable(result)) {
592
- result = finalAsyncIterator(result, () => {
593
- for (const onEnd of onEndHandler) {
594
- onEnd();
595
- }
560
+ let result = await subscribeFn({
561
+ ...args,
562
+ contextValue: context,
563
+ // Casted for GraphQL.js 15 compatibility
564
+ // Can be removed once we drop support for GraphQL.js 15
596
565
  });
597
- }
598
- if (subscribeErrorHandlers.length && isAsyncIterable(result)) {
599
- result = errorAsyncIterator(result, err => {
600
- let error = err;
601
- for (const handler of subscribeErrorHandlers) {
602
- handler({
603
- error,
604
- setError: err => {
605
- error = err;
606
- },
607
- });
566
+ const onNextHandler = [];
567
+ const onEndHandler = [];
568
+ for (const afterCb of afterCalls) {
569
+ const hookResult = afterCb({
570
+ args: args,
571
+ result,
572
+ setResult: newResult => {
573
+ result = newResult;
574
+ },
575
+ });
576
+ if (hookResult) {
577
+ if (hookResult.onNext) {
578
+ onNextHandler.push(hookResult.onNext);
579
+ }
580
+ if (hookResult.onEnd) {
581
+ onEndHandler.push(hookResult.onEnd);
582
+ }
608
583
  }
609
- throw error;
610
- });
611
- }
612
- return result;
613
- });
614
- const customExecute = beforeCallbacks.execute.length
584
+ }
585
+ if (onNextHandler.length && isAsyncIterable(result)) {
586
+ result = mapAsyncIterator(result, async (result) => {
587
+ for (const onNext of onNextHandler) {
588
+ await onNext({
589
+ args: args,
590
+ result,
591
+ setResult: newResult => (result = newResult),
592
+ });
593
+ }
594
+ return result;
595
+ });
596
+ }
597
+ if (onEndHandler.length && isAsyncIterable(result)) {
598
+ result = finalAsyncIterator(result, () => {
599
+ for (const onEnd of onEndHandler) {
600
+ onEnd();
601
+ }
602
+ });
603
+ }
604
+ if (subscribeErrorHandlers.length && isAsyncIterable(result)) {
605
+ result = errorAsyncIterator(result, err => {
606
+ let error = err;
607
+ for (const handler of subscribeErrorHandlers) {
608
+ handler({
609
+ error,
610
+ setError: err => {
611
+ error = err;
612
+ },
613
+ });
614
+ }
615
+ throw error;
616
+ });
617
+ }
618
+ return result;
619
+ })
620
+ : makeSubscribe(graphql.subscribe);
621
+ const useCustomExecute = beforeCallbacks.execute.length || onResolversHandlers.length;
622
+ const customExecute = useCustomExecute
615
623
  ? makeExecute(async (args) => {
616
- const onResolversHandlers = [];
617
624
  let executeFn = graphql.execute;
618
625
  let result;
619
626
  const afterCalls = [];
@@ -649,9 +656,6 @@ function createEnvelopOrchestrator(plugins) {
649
656
  if (after.onExecuteDone) {
650
657
  afterCalls.push(after.onExecuteDone);
651
658
  }
652
- if (after.onResolverCalled) {
653
- onResolversHandlers.push(after.onResolverCalled);
654
- }
655
659
  }
656
660
  }
657
661
  if (onResolversHandlers.length) {
@@ -1004,12 +1008,12 @@ const useTiming = (rawOptions) => {
1004
1008
  onExecuteDone: () => {
1005
1009
  options.onExecutionMeasurement(args, deltaFrom(executeStartTime));
1006
1010
  },
1007
- onResolverCalled: ({ info }) => {
1008
- const resolverStartTime = process.hrtime();
1009
- return () => {
1010
- options.onResolverMeasurement(info, deltaFrom(resolverStartTime));
1011
- };
1012
- },
1011
+ };
1012
+ };
1013
+ result.onResolverCalled = ({ info }) => {
1014
+ const resolverStartTime = process.hrtime();
1015
+ return () => {
1016
+ options.onResolverMeasurement(info, deltaFrom(resolverStartTime));
1013
1017
  };
1014
1018
  };
1015
1019
  }
@@ -1038,12 +1042,12 @@ const useTiming = (rawOptions) => {
1038
1042
  onSubscribeResult: () => {
1039
1043
  options.onSubscriptionMeasurement && options.onSubscriptionMeasurement(args, deltaFrom(subscribeStartTime));
1040
1044
  },
1041
- onResolverCalled: ({ info }) => {
1042
- const resolverStartTime = process.hrtime();
1043
- return () => {
1044
- options.onResolverMeasurement && options.onResolverMeasurement(info, deltaFrom(resolverStartTime));
1045
- };
1046
- },
1045
+ };
1046
+ };
1047
+ result.onResolverCalled = ({ info }) => {
1048
+ const resolverStartTime = process.hrtime();
1049
+ return () => {
1050
+ options.onResolverMeasurement && options.onResolverMeasurement(info, deltaFrom(resolverStartTime));
1047
1051
  };
1048
1052
  };
1049
1053
  }
package/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  export * from '@envelop/types';
2
- import { isIntrospectionType, isObjectType, defaultFieldResolver, Kind, visit, BREAK, parse, specifiedRules, validate, execute, subscribe, GraphQLSchema, getOperationAST, GraphQLError } from 'graphql';
2
+ import { isIntrospectionType, isObjectType, defaultFieldResolver, Kind, visit, BREAK, parse, specifiedRules, validate, subscribe, execute, GraphQLSchema, getOperationAST, GraphQLError } from 'graphql';
3
3
 
4
4
  /**
5
5
  * This enum is used only internally in order to create nominal type for the disabled plugin
@@ -299,11 +299,19 @@ function errorAsyncIterator(source, onError) {
299
299
  function createEnvelopOrchestrator(plugins) {
300
300
  let schema = null;
301
301
  let initDone = false;
302
+ const onResolversHandlers = [];
303
+ for (const plugin of plugins) {
304
+ if (plugin.onResolverCalled) {
305
+ onResolversHandlers.push(plugin.onResolverCalled);
306
+ }
307
+ }
302
308
  // Define the initial method for replacing the GraphQL schema, this is needed in order
303
309
  // to allow setting the schema from the onPluginInit callback. We also need to make sure
304
310
  // here not to call the same plugin that initiated the schema switch.
305
311
  const replaceSchema = (newSchema, ignorePluginIndex = -1) => {
306
- prepareTracedSchema(newSchema);
312
+ if (onResolversHandlers.length) {
313
+ prepareTracedSchema(newSchema);
314
+ }
307
315
  schema = newSchema;
308
316
  if (initDone) {
309
317
  for (const [i, plugin] of plugins.entries()) {
@@ -515,101 +523,100 @@ function createEnvelopOrchestrator(plugins) {
515
523
  }
516
524
  }
517
525
  : initialContext => orchestratorCtx => orchestratorCtx ? { ...initialContext, ...orchestratorCtx } : initialContext;
518
- const customSubscribe = makeSubscribe(async (args) => {
519
- const onResolversHandlers = [];
520
- let subscribeFn = subscribe;
521
- const afterCalls = [];
522
- const subscribeErrorHandlers = [];
523
- let context = args.contextValue || {};
524
- for (const onSubscribe of beforeCallbacks.subscribe) {
525
- const after = await onSubscribe({
526
- subscribeFn,
527
- setSubscribeFn: newSubscribeFn => {
528
- subscribeFn = newSubscribeFn;
529
- },
530
- extendContext: extension => {
531
- context = { ...context, ...extension };
532
- },
533
- args: args,
534
- });
535
- if (after) {
536
- if (after.onSubscribeResult) {
537
- afterCalls.push(after.onSubscribeResult);
538
- }
539
- if (after.onSubscribeError) {
540
- subscribeErrorHandlers.push(after.onSubscribeError);
541
- }
542
- if (after.onResolverCalled) {
543
- onResolversHandlers.push(after.onResolverCalled);
526
+ const useCustomSubscribe = beforeCallbacks.subscribe.length || onResolversHandlers.length;
527
+ const customSubscribe = useCustomSubscribe
528
+ ? makeSubscribe(async (args) => {
529
+ let subscribeFn = subscribe;
530
+ const afterCalls = [];
531
+ const subscribeErrorHandlers = [];
532
+ let context = args.contextValue || {};
533
+ for (const onSubscribe of beforeCallbacks.subscribe) {
534
+ const after = await onSubscribe({
535
+ subscribeFn,
536
+ setSubscribeFn: newSubscribeFn => {
537
+ subscribeFn = newSubscribeFn;
538
+ },
539
+ extendContext: extension => {
540
+ context = { ...context, ...extension };
541
+ },
542
+ args: args,
543
+ });
544
+ if (after) {
545
+ if (after.onSubscribeResult) {
546
+ afterCalls.push(after.onSubscribeResult);
547
+ }
548
+ if (after.onSubscribeError) {
549
+ subscribeErrorHandlers.push(after.onSubscribeError);
550
+ }
544
551
  }
545
552
  }
546
- }
547
- if (onResolversHandlers.length) {
548
- context[resolversHooksSymbol] = onResolversHandlers;
549
- }
550
- let result = await subscribeFn({
551
- ...args,
552
- contextValue: context,
553
- // Casted for GraphQL.js 15 compatibility
554
- // Can be removed once we drop support for GraphQL.js 15
555
- });
556
- const onNextHandler = [];
557
- const onEndHandler = [];
558
- for (const afterCb of afterCalls) {
559
- const hookResult = afterCb({
560
- args: args,
561
- result,
562
- setResult: newResult => {
563
- result = newResult;
564
- },
565
- });
566
- if (hookResult) {
567
- if (hookResult.onNext) {
568
- onNextHandler.push(hookResult.onNext);
569
- }
570
- if (hookResult.onEnd) {
571
- onEndHandler.push(hookResult.onEnd);
572
- }
553
+ if (onResolversHandlers.length) {
554
+ context[resolversHooksSymbol] = onResolversHandlers;
573
555
  }
574
- }
575
- if (onNextHandler.length && isAsyncIterable(result)) {
576
- result = mapAsyncIterator(result, async (result) => {
577
- for (const onNext of onNextHandler) {
578
- await onNext({
579
- args: args,
580
- result,
581
- setResult: newResult => (result = newResult),
582
- });
583
- }
584
- return result;
585
- });
586
- }
587
- if (onEndHandler.length && isAsyncIterable(result)) {
588
- result = finalAsyncIterator(result, () => {
589
- for (const onEnd of onEndHandler) {
590
- onEnd();
591
- }
556
+ let result = await subscribeFn({
557
+ ...args,
558
+ contextValue: context,
559
+ // Casted for GraphQL.js 15 compatibility
560
+ // Can be removed once we drop support for GraphQL.js 15
592
561
  });
593
- }
594
- if (subscribeErrorHandlers.length && isAsyncIterable(result)) {
595
- result = errorAsyncIterator(result, err => {
596
- let error = err;
597
- for (const handler of subscribeErrorHandlers) {
598
- handler({
599
- error,
600
- setError: err => {
601
- error = err;
602
- },
603
- });
562
+ const onNextHandler = [];
563
+ const onEndHandler = [];
564
+ for (const afterCb of afterCalls) {
565
+ const hookResult = afterCb({
566
+ args: args,
567
+ result,
568
+ setResult: newResult => {
569
+ result = newResult;
570
+ },
571
+ });
572
+ if (hookResult) {
573
+ if (hookResult.onNext) {
574
+ onNextHandler.push(hookResult.onNext);
575
+ }
576
+ if (hookResult.onEnd) {
577
+ onEndHandler.push(hookResult.onEnd);
578
+ }
604
579
  }
605
- throw error;
606
- });
607
- }
608
- return result;
609
- });
610
- const customExecute = beforeCallbacks.execute.length
580
+ }
581
+ if (onNextHandler.length && isAsyncIterable(result)) {
582
+ result = mapAsyncIterator(result, async (result) => {
583
+ for (const onNext of onNextHandler) {
584
+ await onNext({
585
+ args: args,
586
+ result,
587
+ setResult: newResult => (result = newResult),
588
+ });
589
+ }
590
+ return result;
591
+ });
592
+ }
593
+ if (onEndHandler.length && isAsyncIterable(result)) {
594
+ result = finalAsyncIterator(result, () => {
595
+ for (const onEnd of onEndHandler) {
596
+ onEnd();
597
+ }
598
+ });
599
+ }
600
+ if (subscribeErrorHandlers.length && isAsyncIterable(result)) {
601
+ result = errorAsyncIterator(result, err => {
602
+ let error = err;
603
+ for (const handler of subscribeErrorHandlers) {
604
+ handler({
605
+ error,
606
+ setError: err => {
607
+ error = err;
608
+ },
609
+ });
610
+ }
611
+ throw error;
612
+ });
613
+ }
614
+ return result;
615
+ })
616
+ : makeSubscribe(subscribe);
617
+ const useCustomExecute = beforeCallbacks.execute.length || onResolversHandlers.length;
618
+ const customExecute = useCustomExecute
611
619
  ? makeExecute(async (args) => {
612
- const onResolversHandlers = [];
613
620
  let executeFn = execute;
614
621
  let result;
615
622
  const afterCalls = [];
@@ -645,9 +652,6 @@ function createEnvelopOrchestrator(plugins) {
645
652
  if (after.onExecuteDone) {
646
653
  afterCalls.push(after.onExecuteDone);
647
654
  }
648
- if (after.onResolverCalled) {
649
- onResolversHandlers.push(after.onResolverCalled);
650
- }
651
655
  }
652
656
  }
653
657
  if (onResolversHandlers.length) {
@@ -1000,12 +1004,12 @@ const useTiming = (rawOptions) => {
1000
1004
  onExecuteDone: () => {
1001
1005
  options.onExecutionMeasurement(args, deltaFrom(executeStartTime));
1002
1006
  },
1003
- onResolverCalled: ({ info }) => {
1004
- const resolverStartTime = process.hrtime();
1005
- return () => {
1006
- options.onResolverMeasurement(info, deltaFrom(resolverStartTime));
1007
- };
1008
- },
1007
+ };
1008
+ };
1009
+ result.onResolverCalled = ({ info }) => {
1010
+ const resolverStartTime = process.hrtime();
1011
+ return () => {
1012
+ options.onResolverMeasurement(info, deltaFrom(resolverStartTime));
1009
1013
  };
1010
1014
  };
1011
1015
  }
@@ -1034,12 +1038,12 @@ const useTiming = (rawOptions) => {
1034
1038
  onSubscribeResult: () => {
1035
1039
  options.onSubscriptionMeasurement && options.onSubscriptionMeasurement(args, deltaFrom(subscribeStartTime));
1036
1040
  },
1037
- onResolverCalled: ({ info }) => {
1038
- const resolverStartTime = process.hrtime();
1039
- return () => {
1040
- options.onResolverMeasurement && options.onResolverMeasurement(info, deltaFrom(resolverStartTime));
1041
- };
1042
- },
1041
+ };
1042
+ };
1043
+ result.onResolverCalled = ({ info }) => {
1044
+ const resolverStartTime = process.hrtime();
1045
+ return () => {
1046
+ options.onResolverMeasurement && options.onResolverMeasurement(info, deltaFrom(resolverStartTime));
1043
1047
  };
1044
1048
  };
1045
1049
  }
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@envelop/core",
3
- "version": "1.8.0-alpha-f56d3a3.0",
3
+ "version": "2.0.0",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
6
  "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
7
7
  },
8
8
  "dependencies": {
9
- "@envelop/types": "1.6.0-alpha-f56d3a3.0"
9
+ "@envelop/types": "2.0.0"
10
10
  },
11
11
  "repository": {
12
12
  "type": "git",