@envelop/core 1.7.0 → 2.0.0-alpha-dc1e98d.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 +1 -1
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()) {
@@ -512,101 +520,100 @@ function createEnvelopOrchestrator(plugins) {
512
520
  }
513
521
  }
514
522
  : initialContext => orchestratorCtx => orchestratorCtx ? { ...initialContext, ...orchestratorCtx } : initialContext;
515
- const customSubscribe = makeSubscribe(async (args) => {
516
- const onResolversHandlers = [];
517
- let subscribeFn = graphql.subscribe;
518
- const afterCalls = [];
519
- const subscribeErrorHandlers = [];
520
- let context = args.contextValue || {};
521
- for (const onSubscribe of beforeCallbacks.subscribe) {
522
- const after = await onSubscribe({
523
- subscribeFn,
524
- setSubscribeFn: newSubscribeFn => {
525
- subscribeFn = newSubscribeFn;
526
- },
527
- extendContext: extension => {
528
- context = { ...context, ...extension };
529
- },
530
- args: args,
531
- });
532
- if (after) {
533
- if (after.onSubscribeResult) {
534
- afterCalls.push(after.onSubscribeResult);
535
- }
536
- if (after.onSubscribeError) {
537
- subscribeErrorHandlers.push(after.onSubscribeError);
538
- }
539
- if (after.onResolverCalled) {
540
- onResolversHandlers.push(after.onResolverCalled);
523
+ const useCustomSubscribe = beforeCallbacks.subscribe.length || onResolversHandlers.length;
524
+ const customSubscribe = useCustomSubscribe
525
+ ? makeSubscribe(async (args) => {
526
+ let subscribeFn = graphql.subscribe;
527
+ const afterCalls = [];
528
+ const subscribeErrorHandlers = [];
529
+ let context = args.contextValue || {};
530
+ for (const onSubscribe of beforeCallbacks.subscribe) {
531
+ const after = await onSubscribe({
532
+ subscribeFn,
533
+ setSubscribeFn: newSubscribeFn => {
534
+ subscribeFn = newSubscribeFn;
535
+ },
536
+ extendContext: extension => {
537
+ context = { ...context, ...extension };
538
+ },
539
+ args: args,
540
+ });
541
+ if (after) {
542
+ if (after.onSubscribeResult) {
543
+ afterCalls.push(after.onSubscribeResult);
544
+ }
545
+ if (after.onSubscribeError) {
546
+ subscribeErrorHandlers.push(after.onSubscribeError);
547
+ }
541
548
  }
542
549
  }
543
- }
544
- if (onResolversHandlers.length) {
545
- context[resolversHooksSymbol] = onResolversHandlers;
546
- }
547
- let result = await subscribeFn({
548
- ...args,
549
- contextValue: context,
550
- // Casted for GraphQL.js 15 compatibility
551
- // Can be removed once we drop support for GraphQL.js 15
552
- });
553
- const onNextHandler = [];
554
- const onEndHandler = [];
555
- for (const afterCb of afterCalls) {
556
- const hookResult = afterCb({
557
- args: args,
558
- result,
559
- setResult: newResult => {
560
- result = newResult;
561
- },
562
- });
563
- if (hookResult) {
564
- if (hookResult.onNext) {
565
- onNextHandler.push(hookResult.onNext);
566
- }
567
- if (hookResult.onEnd) {
568
- onEndHandler.push(hookResult.onEnd);
569
- }
550
+ if (onResolversHandlers.length) {
551
+ context[resolversHooksSymbol] = onResolversHandlers;
570
552
  }
571
- }
572
- if (onNextHandler.length && isAsyncIterable(result)) {
573
- result = mapAsyncIterator(result, async (result) => {
574
- for (const onNext of onNextHandler) {
575
- await onNext({
576
- args: args,
577
- result,
578
- setResult: newResult => (result = newResult),
579
- });
580
- }
581
- return result;
582
- });
583
- }
584
- if (onEndHandler.length && isAsyncIterable(result)) {
585
- result = finalAsyncIterator(result, () => {
586
- for (const onEnd of onEndHandler) {
587
- onEnd();
588
- }
553
+ let result = await subscribeFn({
554
+ ...args,
555
+ contextValue: context,
556
+ // Casted for GraphQL.js 15 compatibility
557
+ // Can be removed once we drop support for GraphQL.js 15
589
558
  });
590
- }
591
- if (subscribeErrorHandlers.length && isAsyncIterable(result)) {
592
- result = errorAsyncIterator(result, err => {
593
- let error = err;
594
- for (const handler of subscribeErrorHandlers) {
595
- handler({
596
- error,
597
- setError: err => {
598
- error = err;
599
- },
600
- });
559
+ const onNextHandler = [];
560
+ const onEndHandler = [];
561
+ for (const afterCb of afterCalls) {
562
+ const hookResult = afterCb({
563
+ args: args,
564
+ result,
565
+ setResult: newResult => {
566
+ result = newResult;
567
+ },
568
+ });
569
+ if (hookResult) {
570
+ if (hookResult.onNext) {
571
+ onNextHandler.push(hookResult.onNext);
572
+ }
573
+ if (hookResult.onEnd) {
574
+ onEndHandler.push(hookResult.onEnd);
575
+ }
601
576
  }
602
- throw error;
603
- });
604
- }
605
- return result;
606
- });
607
- const customExecute = beforeCallbacks.execute.length
577
+ }
578
+ if (onNextHandler.length && isAsyncIterable(result)) {
579
+ result = mapAsyncIterator(result, async (result) => {
580
+ for (const onNext of onNextHandler) {
581
+ await onNext({
582
+ args: args,
583
+ result,
584
+ setResult: newResult => (result = newResult),
585
+ });
586
+ }
587
+ return result;
588
+ });
589
+ }
590
+ if (onEndHandler.length && isAsyncIterable(result)) {
591
+ result = finalAsyncIterator(result, () => {
592
+ for (const onEnd of onEndHandler) {
593
+ onEnd();
594
+ }
595
+ });
596
+ }
597
+ if (subscribeErrorHandlers.length && isAsyncIterable(result)) {
598
+ result = errorAsyncIterator(result, err => {
599
+ let error = err;
600
+ for (const handler of subscribeErrorHandlers) {
601
+ handler({
602
+ error,
603
+ setError: err => {
604
+ error = err;
605
+ },
606
+ });
607
+ }
608
+ throw error;
609
+ });
610
+ }
611
+ return result;
612
+ })
613
+ : makeSubscribe(graphql.subscribe);
614
+ const useCustomExecute = beforeCallbacks.execute.length || onResolversHandlers.length;
615
+ const customExecute = useCustomExecute
608
616
  ? makeExecute(async (args) => {
609
- const onResolversHandlers = [];
610
617
  let executeFn = graphql.execute;
611
618
  let result;
612
619
  const afterCalls = [];
@@ -642,9 +649,6 @@ function createEnvelopOrchestrator(plugins) {
642
649
  if (after.onExecuteDone) {
643
650
  afterCalls.push(after.onExecuteDone);
644
651
  }
645
- if (after.onResolverCalled) {
646
- onResolversHandlers.push(after.onResolverCalled);
647
- }
648
652
  }
649
653
  }
650
654
  if (onResolversHandlers.length) {
@@ -997,12 +1001,12 @@ const useTiming = (rawOptions) => {
997
1001
  onExecuteDone: () => {
998
1002
  options.onExecutionMeasurement(args, deltaFrom(executeStartTime));
999
1003
  },
1000
- onResolverCalled: ({ info }) => {
1001
- const resolverStartTime = process.hrtime();
1002
- return () => {
1003
- options.onResolverMeasurement(info, deltaFrom(resolverStartTime));
1004
- };
1005
- },
1004
+ };
1005
+ };
1006
+ result.onResolverCalled = ({ info }) => {
1007
+ const resolverStartTime = process.hrtime();
1008
+ return () => {
1009
+ options.onResolverMeasurement(info, deltaFrom(resolverStartTime));
1006
1010
  };
1007
1011
  };
1008
1012
  }
@@ -1031,12 +1035,12 @@ const useTiming = (rawOptions) => {
1031
1035
  onSubscribeResult: () => {
1032
1036
  options.onSubscriptionMeasurement && options.onSubscriptionMeasurement(args, deltaFrom(subscribeStartTime));
1033
1037
  },
1034
- onResolverCalled: ({ info }) => {
1035
- const resolverStartTime = process.hrtime();
1036
- return () => {
1037
- options.onResolverMeasurement && options.onResolverMeasurement(info, deltaFrom(resolverStartTime));
1038
- };
1039
- },
1038
+ };
1039
+ };
1040
+ result.onResolverCalled = ({ info }) => {
1041
+ const resolverStartTime = process.hrtime();
1042
+ return () => {
1043
+ options.onResolverMeasurement && options.onResolverMeasurement(info, deltaFrom(resolverStartTime));
1040
1044
  };
1041
1045
  };
1042
1046
  }
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()) {
@@ -508,101 +516,100 @@ function createEnvelopOrchestrator(plugins) {
508
516
  }
509
517
  }
510
518
  : initialContext => orchestratorCtx => orchestratorCtx ? { ...initialContext, ...orchestratorCtx } : initialContext;
511
- const customSubscribe = makeSubscribe(async (args) => {
512
- const onResolversHandlers = [];
513
- let subscribeFn = subscribe;
514
- const afterCalls = [];
515
- const subscribeErrorHandlers = [];
516
- let context = args.contextValue || {};
517
- for (const onSubscribe of beforeCallbacks.subscribe) {
518
- const after = await onSubscribe({
519
- subscribeFn,
520
- setSubscribeFn: newSubscribeFn => {
521
- subscribeFn = newSubscribeFn;
522
- },
523
- extendContext: extension => {
524
- context = { ...context, ...extension };
525
- },
526
- args: args,
527
- });
528
- if (after) {
529
- if (after.onSubscribeResult) {
530
- afterCalls.push(after.onSubscribeResult);
531
- }
532
- if (after.onSubscribeError) {
533
- subscribeErrorHandlers.push(after.onSubscribeError);
534
- }
535
- if (after.onResolverCalled) {
536
- onResolversHandlers.push(after.onResolverCalled);
519
+ const useCustomSubscribe = beforeCallbacks.subscribe.length || onResolversHandlers.length;
520
+ const customSubscribe = useCustomSubscribe
521
+ ? makeSubscribe(async (args) => {
522
+ let subscribeFn = subscribe;
523
+ const afterCalls = [];
524
+ const subscribeErrorHandlers = [];
525
+ let context = args.contextValue || {};
526
+ for (const onSubscribe of beforeCallbacks.subscribe) {
527
+ const after = await onSubscribe({
528
+ subscribeFn,
529
+ setSubscribeFn: newSubscribeFn => {
530
+ subscribeFn = newSubscribeFn;
531
+ },
532
+ extendContext: extension => {
533
+ context = { ...context, ...extension };
534
+ },
535
+ args: args,
536
+ });
537
+ if (after) {
538
+ if (after.onSubscribeResult) {
539
+ afterCalls.push(after.onSubscribeResult);
540
+ }
541
+ if (after.onSubscribeError) {
542
+ subscribeErrorHandlers.push(after.onSubscribeError);
543
+ }
537
544
  }
538
545
  }
539
- }
540
- if (onResolversHandlers.length) {
541
- context[resolversHooksSymbol] = onResolversHandlers;
542
- }
543
- let result = await subscribeFn({
544
- ...args,
545
- contextValue: context,
546
- // Casted for GraphQL.js 15 compatibility
547
- // Can be removed once we drop support for GraphQL.js 15
548
- });
549
- const onNextHandler = [];
550
- const onEndHandler = [];
551
- for (const afterCb of afterCalls) {
552
- const hookResult = afterCb({
553
- args: args,
554
- result,
555
- setResult: newResult => {
556
- result = newResult;
557
- },
558
- });
559
- if (hookResult) {
560
- if (hookResult.onNext) {
561
- onNextHandler.push(hookResult.onNext);
562
- }
563
- if (hookResult.onEnd) {
564
- onEndHandler.push(hookResult.onEnd);
565
- }
546
+ if (onResolversHandlers.length) {
547
+ context[resolversHooksSymbol] = onResolversHandlers;
566
548
  }
567
- }
568
- if (onNextHandler.length && isAsyncIterable(result)) {
569
- result = mapAsyncIterator(result, async (result) => {
570
- for (const onNext of onNextHandler) {
571
- await onNext({
572
- args: args,
573
- result,
574
- setResult: newResult => (result = newResult),
575
- });
576
- }
577
- return result;
578
- });
579
- }
580
- if (onEndHandler.length && isAsyncIterable(result)) {
581
- result = finalAsyncIterator(result, () => {
582
- for (const onEnd of onEndHandler) {
583
- onEnd();
584
- }
549
+ let result = await subscribeFn({
550
+ ...args,
551
+ contextValue: context,
552
+ // Casted for GraphQL.js 15 compatibility
553
+ // Can be removed once we drop support for GraphQL.js 15
585
554
  });
586
- }
587
- if (subscribeErrorHandlers.length && isAsyncIterable(result)) {
588
- result = errorAsyncIterator(result, err => {
589
- let error = err;
590
- for (const handler of subscribeErrorHandlers) {
591
- handler({
592
- error,
593
- setError: err => {
594
- error = err;
595
- },
596
- });
555
+ const onNextHandler = [];
556
+ const onEndHandler = [];
557
+ for (const afterCb of afterCalls) {
558
+ const hookResult = afterCb({
559
+ args: args,
560
+ result,
561
+ setResult: newResult => {
562
+ result = newResult;
563
+ },
564
+ });
565
+ if (hookResult) {
566
+ if (hookResult.onNext) {
567
+ onNextHandler.push(hookResult.onNext);
568
+ }
569
+ if (hookResult.onEnd) {
570
+ onEndHandler.push(hookResult.onEnd);
571
+ }
597
572
  }
598
- throw error;
599
- });
600
- }
601
- return result;
602
- });
603
- const customExecute = beforeCallbacks.execute.length
573
+ }
574
+ if (onNextHandler.length && isAsyncIterable(result)) {
575
+ result = mapAsyncIterator(result, async (result) => {
576
+ for (const onNext of onNextHandler) {
577
+ await onNext({
578
+ args: args,
579
+ result,
580
+ setResult: newResult => (result = newResult),
581
+ });
582
+ }
583
+ return result;
584
+ });
585
+ }
586
+ if (onEndHandler.length && isAsyncIterable(result)) {
587
+ result = finalAsyncIterator(result, () => {
588
+ for (const onEnd of onEndHandler) {
589
+ onEnd();
590
+ }
591
+ });
592
+ }
593
+ if (subscribeErrorHandlers.length && isAsyncIterable(result)) {
594
+ result = errorAsyncIterator(result, err => {
595
+ let error = err;
596
+ for (const handler of subscribeErrorHandlers) {
597
+ handler({
598
+ error,
599
+ setError: err => {
600
+ error = err;
601
+ },
602
+ });
603
+ }
604
+ throw error;
605
+ });
606
+ }
607
+ return result;
608
+ })
609
+ : makeSubscribe(subscribe);
610
+ const useCustomExecute = beforeCallbacks.execute.length || onResolversHandlers.length;
611
+ const customExecute = useCustomExecute
604
612
  ? makeExecute(async (args) => {
605
- const onResolversHandlers = [];
606
613
  let executeFn = execute;
607
614
  let result;
608
615
  const afterCalls = [];
@@ -638,9 +645,6 @@ function createEnvelopOrchestrator(plugins) {
638
645
  if (after.onExecuteDone) {
639
646
  afterCalls.push(after.onExecuteDone);
640
647
  }
641
- if (after.onResolverCalled) {
642
- onResolversHandlers.push(after.onResolverCalled);
643
- }
644
648
  }
645
649
  }
646
650
  if (onResolversHandlers.length) {
@@ -993,12 +997,12 @@ const useTiming = (rawOptions) => {
993
997
  onExecuteDone: () => {
994
998
  options.onExecutionMeasurement(args, deltaFrom(executeStartTime));
995
999
  },
996
- onResolverCalled: ({ info }) => {
997
- const resolverStartTime = process.hrtime();
998
- return () => {
999
- options.onResolverMeasurement(info, deltaFrom(resolverStartTime));
1000
- };
1001
- },
1000
+ };
1001
+ };
1002
+ result.onResolverCalled = ({ info }) => {
1003
+ const resolverStartTime = process.hrtime();
1004
+ return () => {
1005
+ options.onResolverMeasurement(info, deltaFrom(resolverStartTime));
1002
1006
  };
1003
1007
  };
1004
1008
  }
@@ -1027,12 +1031,12 @@ const useTiming = (rawOptions) => {
1027
1031
  onSubscribeResult: () => {
1028
1032
  options.onSubscriptionMeasurement && options.onSubscriptionMeasurement(args, deltaFrom(subscribeStartTime));
1029
1033
  },
1030
- onResolverCalled: ({ info }) => {
1031
- const resolverStartTime = process.hrtime();
1032
- return () => {
1033
- options.onResolverMeasurement && options.onResolverMeasurement(info, deltaFrom(resolverStartTime));
1034
- };
1035
- },
1034
+ };
1035
+ };
1036
+ result.onResolverCalled = ({ info }) => {
1037
+ const resolverStartTime = process.hrtime();
1038
+ return () => {
1039
+ options.onResolverMeasurement && options.onResolverMeasurement(info, deltaFrom(resolverStartTime));
1036
1040
  };
1037
1041
  };
1038
1042
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@envelop/core",
3
- "version": "1.7.0",
3
+ "version": "2.0.0-alpha-dc1e98d.0",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
6
  "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"