@@ -31,6 +31,9 @@ import {
31
31
ValidateFunction ,
32
32
ExecutionResult ,
33
33
PerformFunction ,
34
+ OnPerformHook ,
35
+ OnPerformHookResult ,
36
+ OnPerformDoneHook ,
34
37
} from '@envelop/types' ;
35
38
import {
36
39
errorAsyncIterator ,
@@ -118,15 +121,17 @@ export function createEnvelopOrchestrator<PluginsContext extends DefaultContext>
118
121
subscribe : [ ] as OnSubscribeHook < any > [ ] ,
119
122
execute : [ ] as OnExecuteHook < any > [ ] ,
120
123
context : [ ] as OnContextBuildingHook < any > [ ] ,
124
+ perform : [ ] as OnPerformHook < any > [ ] ,
121
125
} ;
122
126
123
- for ( const { onContextBuilding, onExecute, onParse, onSubscribe, onValidate, onEnveloped } of plugins ) {
127
+ for ( const { onContextBuilding, onExecute, onParse, onSubscribe, onValidate, onEnveloped, onPerform } of plugins ) {
124
128
onEnveloped && beforeCallbacks . init . push ( onEnveloped ) ;
125
129
onContextBuilding && beforeCallbacks . context . push ( onContextBuilding ) ;
126
130
onExecute && beforeCallbacks . execute . push ( onExecute ) ;
127
131
onParse && beforeCallbacks . parse . push ( onParse ) ;
128
132
onSubscribe && beforeCallbacks . subscribe . push ( onSubscribe ) ;
129
133
onValidate && beforeCallbacks . validate . push ( onValidate ) ;
134
+ onPerform && beforeCallbacks . perform . push ( onPerform ) ;
130
135
}
131
136
132
137
const init : EnvelopOrchestrator [ 'init' ] = initialContext => {
@@ -565,6 +570,24 @@ export function createEnvelopOrchestrator<PluginsContext extends DefaultContext>
565
570
const contextFactory = customContextFactory ( initialContext ) ;
566
571
567
572
return async ( params , contextExtension ) => {
573
+ const context = await contextFactory ( contextExtension ) ;
574
+
575
+ const doneFns : OnPerformDoneHook [ ] = [ ] ;
576
+ for ( const onPerform of beforeCallbacks . perform ) {
577
+ const result = await onPerform ( {
578
+ context,
579
+ extendContext : extension => {
580
+ Object . assign ( context , extension ) ;
581
+ } ,
582
+ params,
583
+ setParams : newParams => {
584
+ params = newParams ;
585
+ } ,
586
+ } ) ;
587
+
588
+ result ?. onPerformDone && doneFns . push ( result . onPerformDone ) ;
589
+ }
590
+
568
591
let document ;
569
592
try {
570
593
document = parse ( params . query ) ;
@@ -577,23 +600,33 @@ export function createEnvelopOrchestrator<PluginsContext extends DefaultContext>
577
600
return { errors : validationErrors } ;
578
601
}
579
602
580
- const context = await contextFactory ( contextExtension ) ;
581
-
603
+ let result ;
582
604
if ( isSubscriptionOperation ( document , params . operationName ) ) {
583
- return await customSubscribe ( {
605
+ result = await customSubscribe ( {
606
+ document,
607
+ schema,
608
+ variableValues : params . variables ,
609
+ contextValue : context ,
610
+ } ) ;
611
+ } else {
612
+ result = await customExecute ( {
584
613
document,
585
614
schema,
586
615
variableValues : params . variables ,
587
616
contextValue : context ,
588
617
} ) ;
589
618
}
590
619
591
- return await customExecute ( {
592
- document,
593
- schema,
594
- variableValues : params . variables ,
595
- contextValue : context ,
596
- } ) ;
620
+ for ( const doneFn of doneFns ) {
621
+ doneFn ( {
622
+ result,
623
+ setResult : newResult => {
624
+ result = newResult ;
625
+ } ,
626
+ } ) ;
627
+ }
628
+
629
+ return result ;
597
630
} ;
598
631
} ;
599
632
0 commit comments