@@ -3,10 +3,8 @@ 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 ,
7
6
createGraphQLError ,
8
7
ExecutionRequest ,
9
- ExecutionResult ,
10
8
isAsyncIterable ,
11
9
isPromise ,
12
10
} from '@graphql-tools/utils' ;
@@ -59,19 +57,29 @@ export function useUpstreamTimeout<TContext extends Record<string, any>>(
59
57
if ( executionRequest . signal ) {
60
58
signals . push ( executionRequest . signal ) ;
61
59
}
62
- const timeoutDeferred = createDeferred < ExecutionResult > ( ) ;
63
- function rejectDeferred ( ) {
64
- timeoutDeferred . reject ( timeoutSignal ?. reason ) ;
65
- }
66
- timeoutSignal . addEventListener ( 'abort' , rejectDeferred , {
67
- once : true ,
60
+ // we want to create a new executionrequest and not mutate the existing one becaus, when using
61
+ // this with useUpstreamRetry, the same executionRequest will be used for each retry and we need
62
+ // to timeoutSignalsByExecutionRequest.set(...) again above
63
+ const res$ = executor ( {
64
+ ... executionRequest ,
65
+ signal : abortSignalAny ( signals ) ,
68
66
} ) ;
69
- executionRequest . signal = abortSignalAny ( signals ) ;
70
- const res$ = executor ( executionRequest ) ;
71
67
if ( ! isPromise ( res$ ) ) {
72
68
return res$ ;
73
69
}
74
- return Promise . race ( [ timeoutDeferred . promise , res$ ] )
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
+ ] )
75
83
. then ( ( result ) => {
76
84
if ( isAsyncIterable ( result ) ) {
77
85
return {
@@ -111,8 +119,6 @@ export function useUpstreamTimeout<TContext extends Record<string, any>>(
111
119
throw e ;
112
120
} )
113
121
. finally ( ( ) => {
114
- timeoutDeferred . resolve ( undefined as any ) ;
115
- timeoutSignal . removeEventListener ( 'abort' , rejectDeferred ) ;
116
122
// Remove from the map after used so we don't see it again
117
123
errorExtensionsByExecRequest . delete ( executionRequest ) ;
118
124
timeoutSignalsByExecutionRequest . delete ( executionRequest ) ;
0 commit comments