1
- import type {
2
- ExecutionArgs ,
3
- ExecutionResult ,
4
- GraphQLError ,
5
- OperationDefinitionNode ,
6
- } from 'graphql' ;
1
+ import type { ExecutionArgs , ExecutionResult , GraphQLError } from 'graphql' ;
7
2
import { experimentalExecuteQueryOrMutationOrSubscriptionEvent } from 'graphql' ;
8
3
import type {
9
- FragmentDetails ,
10
4
GroupedFieldSet ,
11
5
// eslint-disable-next-line n/no-missing-import
12
6
} from 'graphql/execution/collectFields.js' ;
@@ -103,14 +97,12 @@ export function transform(
103
97
prefix ,
104
98
) ;
105
99
106
- const withExecutors : Array < {
107
- subschemaConfig : SubschemaConfig ;
108
- operation : OperationDefinitionNode ;
109
- fragments : ObjMap < FragmentDetails > ;
110
- executor : ( ) => PromiseOrValue <
111
- ExecutionResult | ExperimentalIncrementalExecutionResults
112
- > ;
113
- } > = [ ] ;
100
+ const mergedResult = new MergedResult ( ) ;
101
+ const transformedResults : Array <
102
+ PromiseOrValue < ExecutionResult | CompletedInitialResult >
103
+ > = [ ] ;
104
+
105
+ let containsPromise = false ;
114
106
for ( const subschemaConfig of Object . values (
115
107
args . subschemas ?? {
116
108
target : {
@@ -129,8 +121,8 @@ export function transform(
129
121
) ;
130
122
131
123
if ( ! ( 'operation' in transformedForSubschema ) ) {
132
- // return ` data: null` to mimic non-gateway behavior
133
- return { data : null , errors : transformedForSubschema } ;
124
+ transformedResults . push ( { data : null , errors : transformedForSubschema } ) ;
125
+ continue ;
134
126
}
135
127
136
128
const executor : Executor =
@@ -145,56 +137,38 @@ export function transform(
145
137
( details ) => details . definition ,
146
138
) ;
147
139
148
- withExecutors . push ( {
149
- subschemaConfig,
140
+ const originalResult = executor ( {
141
+ ...originalArgs ,
142
+ schema : subschema ,
150
143
operation : transformedOperation ,
151
144
fragments : transformedFragments ,
152
- executor : ( ) =>
153
- executor ( {
154
- ...originalArgs ,
155
- schema : subschema ,
156
- operation : transformedOperation ,
157
- fragments : transformedFragments ,
158
- fragmentDefinitions : transformedFragmentDefinitions ,
159
- } ) ,
145
+ fragmentDefinitions : transformedFragmentDefinitions ,
160
146
} ) ;
161
- }
162
147
163
- const mergedResult = new MergedResult ( ) ;
164
- const transformedResults : Array <
165
- PromiseOrValue < ExecutionResult | CompletedInitialResult >
166
- > = [ ] ;
167
-
168
- let containsPromise = false ;
169
- for ( const {
170
- subschemaConfig,
171
- operation,
172
- fragments,
173
- executor,
174
- } of withExecutors ) {
175
- const originalResult = executor ( ) ;
176
148
const subschemaLabel = subschemaConfig . label ;
177
149
if ( isPromise ( originalResult ) ) {
178
150
containsPromise = true ;
179
151
transformedResults . push (
180
- originalResult . then ( ( resolved ) => {
181
- mergedResult . add ( subschemaLabel , resolved ) ;
182
- return completeInitialResult (
152
+ originalResult . then ( ( resolved ) =>
153
+ completeInitialResult (
183
154
context ,
184
- operation ,
185
- fragments ,
186
- mergedResult . getMergedData ( ) ,
187
- ) ;
188
- } ) ,
155
+ transformedOperation ,
156
+ transformedFragments ,
157
+ subschemaConfig . label ,
158
+ resolved ,
159
+ mergedResult ,
160
+ ) ,
161
+ ) ,
189
162
) ;
190
163
continue ;
191
164
}
192
- mergedResult . add ( subschemaLabel , originalResult ) ;
193
165
const transformed = completeInitialResult (
194
166
context ,
195
- operation ,
196
- fragments ,
197
- mergedResult . getMergedData ( ) ,
167
+ transformedOperation ,
168
+ transformedFragments ,
169
+ subschemaLabel ,
170
+ originalResult ,
171
+ mergedResult ,
198
172
) ;
199
173
// TODO: add test case for asynchronous transform
200
174
/* c8 ignore next 3 */
0 commit comments