@@ -3,8 +3,10 @@ import { subgraphNameByExecutionRequest } from '@graphql-mesh/fusion-runtime';
3
3
import { UpstreamErrorExtensions } from '@graphql-mesh/transport-common' ;
4
4
import { getHeadersObj } from '@graphql-mesh/utils' ;
5
5
import {
6
+ createDeferred ,
6
7
createGraphQLError ,
7
8
ExecutionRequest ,
9
+ ExecutionResult ,
8
10
isAsyncIterable ,
9
11
isPromise ,
10
12
} from '@graphql-tools/utils' ;
@@ -52,6 +54,13 @@ export function useUpstreamTimeout<TContext extends Record<string, any>>(
52
54
timeoutSignal ,
53
55
) ;
54
56
}
57
+ const timeoutDeferred = createDeferred < ExecutionResult > ( ) ;
58
+ function rejectDeferred ( ) {
59
+ timeoutDeferred . reject ( timeoutSignal ?. reason ) ;
60
+ }
61
+ timeoutSignal . addEventListener ( 'abort' , rejectDeferred , {
62
+ once : true ,
63
+ } ) ;
55
64
const signals : AbortSignal [ ] = [ ] ;
56
65
signals . push ( timeoutSignal ) ;
57
66
if ( executionRequest . signal ) {
@@ -67,19 +76,7 @@ export function useUpstreamTimeout<TContext extends Record<string, any>>(
67
76
if ( ! isPromise ( res$ ) ) {
68
77
return res$ ;
69
78
}
70
- return Promise . race ( [
71
- new Promise < never > ( ( _ , reject ) => {
72
- if ( timeoutSignal . aborted ) {
73
- return reject ( timeoutSignal . reason ) ;
74
- }
75
- timeoutSignal . addEventListener (
76
- 'abort' ,
77
- ( ) => reject ( timeoutSignal . reason ) ,
78
- { once : true } ,
79
- ) ;
80
- } ) ,
81
- res$ ,
82
- ] )
79
+ return Promise . race ( [ timeoutDeferred . promise , res$ ] )
83
80
. then ( ( result ) => {
84
81
if ( isAsyncIterable ( result ) ) {
85
82
return {
@@ -119,6 +116,8 @@ export function useUpstreamTimeout<TContext extends Record<string, any>>(
119
116
throw e ;
120
117
} )
121
118
. finally ( ( ) => {
119
+ timeoutDeferred . resolve ( undefined as any ) ;
120
+ timeoutSignal . removeEventListener ( 'abort' , rejectDeferred ) ;
122
121
// Remove from the map after used so we don't see it again
123
122
errorExtensionsByExecRequest . delete ( executionRequest ) ;
124
123
timeoutSignalsByExecutionRequest . delete ( executionRequest ) ;
0 commit comments