@@ -13,7 +13,7 @@ import {
13
13
type YogaServerInstance ,
14
14
type YogaServerOptions ,
15
15
} from 'graphql-yoga' ;
16
- import { AsyncResource } from 'node:async_hooks' ;
16
+ import { AsyncLocalStorage } from 'node:async_hooks' ;
17
17
import type { WebSocket } from 'ws' ;
18
18
import { type GqlContextType } from '~/common' ;
19
19
import { HttpAdapter , type IRequest } from '../http' ;
@@ -105,7 +105,10 @@ export class Driver extends AbstractDriver<DriverConfig> {
105
105
* So this allows our "yoga" plugins to be executed.
106
106
*/
107
107
private makeWsHandler ( options : DriverConfig ) {
108
- const asyncContextBySocket = new WeakMap < WebSocket , AsyncResource > ( ) ;
108
+ const asyncContextBySocket = new WeakMap <
109
+ WebSocket ,
110
+ < R > ( fn : ( ) => R ) => R
111
+ > ( ) ;
109
112
interface WsExecutionArgs extends ExecutionArgs {
110
113
socket : WebSocket ;
111
114
envelop : ReturnType < ReturnType < typeof envelop > > ;
@@ -125,7 +128,7 @@ export class Driver extends AbstractDriver<DriverConfig> {
125
128
// unique envelop (yoga) instance per request.
126
129
execute : ( wsArgs ) => {
127
130
const { envelop, socket, ...args } = wsArgs as WsExecutionArgs ;
128
- return asyncContextBySocket . get ( socket ) ! . runInAsyncScope ( ( ) => {
131
+ return asyncContextBySocket . get ( socket ) ! ( ( ) => {
129
132
return envelop . execute ( args ) ;
130
133
} ) ;
131
134
} ,
@@ -134,7 +137,7 @@ export class Driver extends AbstractDriver<DriverConfig> {
134
137
// Because this is called via socket.onmessage, we don't have
135
138
// the same async context we started with.
136
139
// Grab and resume it.
137
- return asyncContextBySocket . get ( socket ) ! . runInAsyncScope ( ( ) => {
140
+ return asyncContextBySocket . get ( socket ) ! ( ( ) => {
138
141
return envelop . subscribe ( args ) ;
139
142
} ) ;
140
143
} ,
@@ -174,7 +177,7 @@ export class Driver extends AbstractDriver<DriverConfig> {
174
177
175
178
const wsHandler : FastifyRoute [ 'wsHandler' ] = function ( socket , req ) {
176
179
// Save a reference to the current async context, so we can resume it.
177
- asyncContextBySocket . set ( socket , new AsyncResource ( 'graphql-ws' ) ) ;
180
+ asyncContextBySocket . set ( socket , AsyncLocalStorage . snapshot ( ) ) ;
178
181
return fastifyWsHandler . call ( this , socket , req ) ;
179
182
} ;
180
183
return wsHandler ;
0 commit comments