2
2
defaultFieldResolver ,
3
3
DocumentNode ,
4
4
execute ,
5
+ subscribe ,
5
6
GraphQLError ,
6
7
GraphQLFieldResolver ,
7
8
GraphQLSchema ,
@@ -18,16 +19,14 @@ import {
18
19
AfterResolverPayload ,
19
20
Envelop ,
20
21
ExecuteFunction ,
21
- OnExecuteSubscriptionEventHandler ,
22
22
OnResolverCalledHooks ,
23
23
Plugin ,
24
24
SubscribeFunction ,
25
25
} from '@envelop/types' ;
26
- import { subscribe } from './subscribe' ;
27
26
import { makeSubscribe , makeExecute } from './util' ;
28
27
29
28
const trackedSchemaSymbol = Symbol ( 'TRACKED_SCHEMA' ) ;
30
- const resolversHooksSymbol = Symbol ( 'RESOLVERS_HOOKS' ) ;
29
+ export const resolversHooksSymbol = Symbol ( 'RESOLVERS_HOOKS' ) ;
31
30
32
31
export function envelop ( { plugins } : { plugins : Plugin [ ] } ) : Envelop {
33
32
let schema : GraphQLSchema | undefined | null = null ;
@@ -220,7 +219,6 @@ export function envelop({ plugins }: { plugins: Plugin[] }): Envelop {
220
219
result : AsyncIterableIterator < ExecutionResult > | ExecutionResult ;
221
220
setResult : ( newResult : AsyncIterableIterator < ExecutionResult > | ExecutionResult ) => void ;
222
221
} ) => void ) [ ] = [ ] ;
223
- const beforeExecuteSubscriptionHandlers : OnExecuteSubscriptionEventHandler [ ] = [ ] ;
224
222
let context = args . contextValue ;
225
223
226
224
for ( const onSubscribe of onSubscribeCbs ) {
@@ -251,29 +249,42 @@ export function envelop({ plugins }: { plugins: Plugin[] }): Envelop {
251
249
if ( after . onResolverCalled ) {
252
250
onResolversHandlers . push ( after . onResolverCalled ) ;
253
251
}
254
- if ( after . onExecuteSubscriptionEvent ) {
255
- beforeExecuteSubscriptionHandlers . push ( after . onExecuteSubscriptionEvent ) ;
256
- }
257
252
}
258
253
}
259
254
260
255
if ( onResolversHandlers . length ) {
261
256
context [ resolversHooksSymbol ] = onResolversHandlers ;
262
257
}
263
258
264
- const subscribeExecute = beforeExecuteSubscriptionHandlers . length
259
+ let result = await subscribeFn ( {
260
+ ...args ,
261
+ contextValue : context ,
262
+ } ) ;
263
+
264
+ for ( const afterCb of afterCalls ) {
265
+ afterCb ( {
266
+ result,
267
+ setResult : newResult => {
268
+ result = newResult ;
269
+ } ,
270
+ } ) ;
271
+ }
272
+
273
+ return result ;
274
+ } ) ;
275
+
276
+ const customExecute = (
277
+ onExecuteCbs . length
265
278
? makeExecute ( async args => {
266
279
const onResolversHandlers : OnResolverCalledHooks [ ] = [ ] ;
267
280
let executeFn : ExecuteFunction = execute as ExecuteFunction ;
268
281
let result : ExecutionResult ;
269
282
270
- const afterCalls : ( ( options : {
271
- result : ExecutionResult ;
272
- setResult : ( newResult : ExecutionResult ) => void ;
273
- } ) => void ) [ ] = [ ] ;
283
+ const afterCalls : ( ( options : { result : ExecutionResult ; setResult : ( newResult : ExecutionResult ) => void } ) => void ) [ ] =
284
+ [ ] ;
274
285
let context = args . contextValue ;
275
286
276
- for ( const onExecute of beforeExecuteSubscriptionHandlers ) {
287
+ for ( const onExecute of onExecuteCbs ) {
277
288
let stopCalled = false ;
278
289
279
290
const after = onExecute ( {
@@ -336,102 +347,8 @@ export function envelop({ plugins }: { plugins: Plugin[] }): Envelop {
336
347
337
348
return result ;
338
349
} )
339
- : ( ( args . execute ?? execute ) as ExecuteFunction ) ;
340
-
341
- let result = await subscribeFn ( {
342
- ...args ,
343
- contextValue : context ,
344
- execute : subscribeExecute ,
345
- } ) ;
346
-
347
- for ( const afterCb of afterCalls ) {
348
- afterCb ( {
349
- result,
350
- setResult : newResult => {
351
- result = newResult ;
352
- } ,
353
- } ) ;
354
- }
355
-
356
- return result ;
357
- } ) ;
358
-
359
- const customExecute = ( onExecuteCbs . length
360
- ? makeExecute ( async args => {
361
- const onResolversHandlers : OnResolverCalledHooks [ ] = [ ] ;
362
- let executeFn : ExecuteFunction = execute as ExecuteFunction ;
363
- let result : ExecutionResult ;
364
-
365
- const afterCalls : ( ( options : {
366
- result : ExecutionResult ;
367
- setResult : ( newResult : ExecutionResult ) => void ;
368
- } ) => void ) [ ] = [ ] ;
369
- let context = args . contextValue ;
370
-
371
- for ( const onExecute of onExecuteCbs ) {
372
- let stopCalled = false ;
373
-
374
- const after = onExecute ( {
375
- executeFn,
376
- setExecuteFn : newExecuteFn => {
377
- executeFn = newExecuteFn ;
378
- } ,
379
- setResultAndStopExecution : stopResult => {
380
- stopCalled = true ;
381
- result = stopResult ;
382
- } ,
383
- extendContext : extension => {
384
- if ( typeof extension === 'object' ) {
385
- context = {
386
- ...( context || { } ) ,
387
- ...extension ,
388
- } ;
389
- } else {
390
- throw new Error (
391
- `Invalid context extension provided! Expected "object", got: "${ JSON . stringify (
392
- extension
393
- ) } " (${ typeof extension } )`
394
- ) ;
395
- }
396
- } ,
397
- args,
398
- } ) ;
399
-
400
- if ( stopCalled ) {
401
- return result ! ;
402
- }
403
-
404
- if ( after ) {
405
- if ( after . onExecuteDone ) {
406
- afterCalls . push ( after . onExecuteDone ) ;
407
- }
408
- if ( after . onResolverCalled ) {
409
- onResolversHandlers . push ( after . onResolverCalled ) ;
410
- }
411
- }
412
- }
413
-
414
- if ( onResolversHandlers . length ) {
415
- context [ resolversHooksSymbol ] = onResolversHandlers ;
416
- }
417
-
418
- result = await executeFn ( {
419
- ...args ,
420
- contextValue : context ,
421
- } ) ;
422
-
423
- for ( const afterCb of afterCalls ) {
424
- afterCb ( {
425
- result,
426
- setResult : newResult => {
427
- result = newResult ;
428
- } ,
429
- } ) ;
430
- }
431
-
432
- return result ;
433
- } )
434
- : execute ) as ExecuteFunction ;
350
+ : execute
351
+ ) as ExecuteFunction ;
435
352
436
353
function prepareSchema ( ) {
437
354
if ( ! schema || schema [ trackedSchemaSymbol ] ) {
0 commit comments