@envelop/core 1.8.0-alpha-2354554.0 → 2.0.0-alpha-2c9f1ce.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/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()) {
@@ -477,23 +485,16 @@ function createEnvelopOrchestrator(plugins) {
477
485
  const afterCalls = [];
478
486
  try {
479
487
  let context = orchestratorCtx ? { ...initialContext, ...orchestratorCtx } : initialContext;
480
- let isBreakingContextBuilding = false;
481
488
  for (const onContext of beforeCallbacks.context) {
482
489
  const afterHookResult = await onContext({
483
490
  context,
484
491
  extendContext: extension => {
485
492
  context = { ...context, ...extension };
486
493
  },
487
- breakContextBuilding: () => {
488
- isBreakingContextBuilding = true;
489
- },
490
494
  });
491
495
  if (typeof afterHookResult === 'function') {
492
496
  afterCalls.push(afterHookResult);
493
497
  }
494
- if (isBreakingContextBuilding === true) {
495
- break;
496
- }
497
498
  }
498
499
  for (const afterCb of afterCalls) {
499
500
  afterCb({
@@ -519,101 +520,100 @@ function createEnvelopOrchestrator(plugins) {
519
520
  }
520
521
  }
521
522
  : 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);
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
+ }
548
548
  }
549
549
  }
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
- }
550
+ if (onResolversHandlers.length) {
551
+ context[resolversHooksSymbol] = onResolversHandlers;
577
552
  }
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
- }
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
596
558
  });
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
- });
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
+ }
608
576
  }
609
- throw error;
610
- });
611
- }
612
- return result;
613
- });
614
- 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
615
616
  ? makeExecute(async (args) => {
616
- const onResolversHandlers = [];
617
617
  let executeFn = graphql.execute;
618
618
  let result;
619
619
  const afterCalls = [];
@@ -649,9 +649,6 @@ function createEnvelopOrchestrator(plugins) {
649
649
  if (after.onExecuteDone) {
650
650
  afterCalls.push(after.onExecuteDone);
651
651
  }
652
- if (after.onResolverCalled) {
653
- onResolversHandlers.push(after.onResolverCalled);
654
- }
655
652
  }
656
653
  }
657
654
  if (onResolversHandlers.length) {
@@ -1004,12 +1001,12 @@ const useTiming = (rawOptions) => {
1004
1001
  onExecuteDone: () => {
1005
1002
  options.onExecutionMeasurement(args, deltaFrom(executeStartTime));
1006
1003
  },
1007
- onResolverCalled: ({ info }) => {
1008
- const resolverStartTime = process.hrtime();
1009
- return () => {
1010
- options.onResolverMeasurement(info, deltaFrom(resolverStartTime));
1011
- };
1012
- },
1004
+ };
1005
+ };
1006
+ result.onResolverCalled = ({ info }) => {
1007
+ const resolverStartTime = process.hrtime();
1008
+ return () => {
1009
+ options.onResolverMeasurement(info, deltaFrom(resolverStartTime));
1013
1010
  };
1014
1011
  };
1015
1012
  }
@@ -1038,12 +1035,12 @@ const useTiming = (rawOptions) => {
1038
1035
  onSubscribeResult: () => {
1039
1036
  options.onSubscriptionMeasurement && options.onSubscriptionMeasurement(args, deltaFrom(subscribeStartTime));
1040
1037
  },
1041
- onResolverCalled: ({ info }) => {
1042
- const resolverStartTime = process.hrtime();
1043
- return () => {
1044
- options.onResolverMeasurement && options.onResolverMeasurement(info, deltaFrom(resolverStartTime));
1045
- };
1046
- },
1038
+ };
1039
+ };
1040
+ result.onResolverCalled = ({ info }) => {
1041
+ const resolverStartTime = process.hrtime();
1042
+ return () => {
1043
+ options.onResolverMeasurement && options.onResolverMeasurement(info, deltaFrom(resolverStartTime));
1047
1044
  };
1048
1045
  };
1049
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()) {
@@ -473,23 +481,16 @@ function createEnvelopOrchestrator(plugins) {
473
481
  const afterCalls = [];
474
482
  try {
475
483
  let context = orchestratorCtx ? { ...initialContext, ...orchestratorCtx } : initialContext;
476
- let isBreakingContextBuilding = false;
477
484
  for (const onContext of beforeCallbacks.context) {
478
485
  const afterHookResult = await onContext({
479
486
  context,
480
487
  extendContext: extension => {
481
488
  context = { ...context, ...extension };
482
489
  },
483
- breakContextBuilding: () => {
484
- isBreakingContextBuilding = true;
485
- },
486
490
  });
487
491
  if (typeof afterHookResult === 'function') {
488
492
  afterCalls.push(afterHookResult);
489
493
  }
490
- if (isBreakingContextBuilding === true) {
491
- break;
492
- }
493
494
  }
494
495
  for (const afterCb of afterCalls) {
495
496
  afterCb({
@@ -515,101 +516,100 @@ function createEnvelopOrchestrator(plugins) {
515
516
  }
516
517
  }
517
518
  : 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);
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
+ }
544
544
  }
545
545
  }
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
- }
546
+ if (onResolversHandlers.length) {
547
+ context[resolversHooksSymbol] = onResolversHandlers;
573
548
  }
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
- }
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
592
554
  });
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
- });
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
+ }
604
572
  }
605
- throw error;
606
- });
607
- }
608
- return result;
609
- });
610
- 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
611
612
  ? makeExecute(async (args) => {
612
- const onResolversHandlers = [];
613
613
  let executeFn = execute;
614
614
  let result;
615
615
  const afterCalls = [];
@@ -645,9 +645,6 @@ function createEnvelopOrchestrator(plugins) {
645
645
  if (after.onExecuteDone) {
646
646
  afterCalls.push(after.onExecuteDone);
647
647
  }
648
- if (after.onResolverCalled) {
649
- onResolversHandlers.push(after.onResolverCalled);
650
- }
651
648
  }
652
649
  }
653
650
  if (onResolversHandlers.length) {
@@ -1000,12 +997,12 @@ const useTiming = (rawOptions) => {
1000
997
  onExecuteDone: () => {
1001
998
  options.onExecutionMeasurement(args, deltaFrom(executeStartTime));
1002
999
  },
1003
- onResolverCalled: ({ info }) => {
1004
- const resolverStartTime = process.hrtime();
1005
- return () => {
1006
- options.onResolverMeasurement(info, deltaFrom(resolverStartTime));
1007
- };
1008
- },
1000
+ };
1001
+ };
1002
+ result.onResolverCalled = ({ info }) => {
1003
+ const resolverStartTime = process.hrtime();
1004
+ return () => {
1005
+ options.onResolverMeasurement(info, deltaFrom(resolverStartTime));
1009
1006
  };
1010
1007
  };
1011
1008
  }
@@ -1034,12 +1031,12 @@ const useTiming = (rawOptions) => {
1034
1031
  onSubscribeResult: () => {
1035
1032
  options.onSubscriptionMeasurement && options.onSubscriptionMeasurement(args, deltaFrom(subscribeStartTime));
1036
1033
  },
1037
- onResolverCalled: ({ info }) => {
1038
- const resolverStartTime = process.hrtime();
1039
- return () => {
1040
- options.onResolverMeasurement && options.onResolverMeasurement(info, deltaFrom(resolverStartTime));
1041
- };
1042
- },
1034
+ };
1035
+ };
1036
+ result.onResolverCalled = ({ info }) => {
1037
+ const resolverStartTime = process.hrtime();
1038
+ return () => {
1039
+ options.onResolverMeasurement && options.onResolverMeasurement(info, deltaFrom(resolverStartTime));
1043
1040
  };
1044
1041
  };
1045
1042
  }
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@envelop/core",
3
- "version": "1.8.0-alpha-2354554.0",
3
+ "version": "2.0.0-alpha-2c9f1ce.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-2354554.0"
9
+ "@envelop/types": "1.5.1"
10
10
  },
11
11
  "repository": {
12
12
  "type": "git",
@@ -1,6 +0,0 @@
1
- import type { Plugin } from '@envelop/types';
2
- /**
3
- * In case a GraphQL operation only contains introspection fields the context building can be skipped completely.
4
- * With this plugin any further context extensions will be skipped.
5
- */
6
- export declare const useImmediateIntrospection: () => Plugin;