1
1
import { ExecutionArgs , ExecutionResult , SubscriptionArgs } from 'graphql' ;
2
2
import { Plugin , YogaInitialContext , YogaServerInstance } from 'graphql-yoga' ;
3
3
import { useSofa as createSofaHandler } from 'sofa-api' ;
4
+ import { isPromise } from '@graphql-tools/utils' ;
4
5
import { SofaHandler } from './types.js' ;
5
6
6
7
type SofaHandlerConfig = Parameters < typeof createSofaHandler > [ 0 ] ;
@@ -22,7 +23,6 @@ export function useSofa(config: SofaPluginConfig): Plugin {
22
23
ReturnType < YogaServerInstance < Record < string , unknown > , Record < string , unknown > > [ 'getEnveloped' ] >
23
24
> ( ) ;
24
25
25
- const requestByContext = new WeakMap < YogaInitialContext , Request > ( ) ;
26
26
return {
27
27
onYogaInit ( { yoga } ) {
28
28
getEnveloped = yoga . getEnveloped ;
@@ -31,12 +31,17 @@ export function useSofa(config: SofaPluginConfig): Plugin {
31
31
sofaHandler = createSofaHandler ( {
32
32
...config ,
33
33
schema : onSchemaChangeEventPayload . schema ,
34
- async context ( serverContext : YogaInitialContext ) {
34
+ context ( serverContext : YogaInitialContext ) {
35
35
const enveloped = getEnveloped ( serverContext ) ;
36
- const request = requestByContext . get ( serverContext ) ;
37
- const contextValue = await enveloped . contextFactory ( { request } ) ;
38
- envelopedByContext . set ( contextValue as YogaInitialContext , enveloped ) ;
39
- return contextValue ;
36
+ const contextValue$ = enveloped . contextFactory ( serverContext ) ;
37
+ if ( isPromise ( contextValue$ ) ) {
38
+ return contextValue$ . then ( contextValue => {
39
+ envelopedByContext . set ( contextValue , enveloped ) ;
40
+ return contextValue ;
41
+ } ) ;
42
+ }
43
+ envelopedByContext . set ( contextValue$ , enveloped ) ;
44
+ return contextValue$ ;
40
45
} ,
41
46
execute (
42
47
...args :
@@ -111,8 +116,7 @@ export function useSofa(config: SofaPluginConfig): Plugin {
111
116
} ) ;
112
117
} ,
113
118
async onRequest ( { request, serverContext, endResponse } ) {
114
- requestByContext . set ( serverContext as YogaInitialContext , request ) ;
115
- const response = await sofaHandler . handle ( request , serverContext as Record < string , unknown > ) ;
119
+ const response = await sofaHandler ( request , serverContext as Record < string , unknown > ) ;
116
120
if ( response != null && response . status !== 404 ) {
117
121
endResponse ( response ) ;
118
122
}
0 commit comments