@@ -15,7 +15,7 @@ import { GraphQLBoolean, GraphQLInt, GraphQLString } from '../../type/scalars';
15
15
import { GraphQLSchema } from '../../type/schema' ;
16
16
17
17
import type { ExecutionArgs , ExecutionResult } from '../execute' ;
18
- import { createSourceEventStream , subscribe } from '../execute' ;
18
+ import { createSourceEventStream , execute , subscribe } from '../execute' ;
19
19
20
20
import { SimplePubSub } from './simplePubSub' ;
21
21
@@ -122,7 +122,7 @@ function createSubscription(pubsub: SimplePubSub<Email>) {
122
122
} ) ,
123
123
} ;
124
124
125
- return subscribe ( { schema : emailSchema , document, rootValue : data } ) ;
125
+ return execute ( { schema : emailSchema , document, rootValue : data } ) ;
126
126
}
127
127
128
128
// TODO: consider adding this method to testUtils (with tests)
@@ -150,22 +150,46 @@ function expectPromise(maybePromise: unknown) {
150
150
} ;
151
151
}
152
152
153
- // TODO: consider adding this method to testUtils (with tests)
153
+ // TODO: consider adding these method to testUtils (with tests)
154
154
function expectEqualPromisesOrValues < T > (
155
- value1 : PromiseOrValue < T > ,
156
- value2 : PromiseOrValue < T > ,
155
+ items : ReadonlyArray < PromiseOrValue < T > > ,
157
156
) : PromiseOrValue < T > {
158
- if ( isPromise ( value1 ) ) {
159
- assert ( isPromise ( value2 ) ) ;
160
- return Promise . all ( [ value1 , value2 ] ) . then ( ( resolved ) => {
161
- expectJSON ( resolved [ 1 ] ) . toDeepEqual ( resolved [ 0 ] ) ;
162
- return resolved [ 0 ] ;
163
- } ) ;
157
+ if ( isPromise ( items [ 0 ] ) ) {
158
+ if ( assertAllPromises ( items ) ) {
159
+ return Promise . all ( items ) . then ( expectMatchingValues ) ;
160
+ }
161
+ } else if ( assertNoPromises ( items ) ) {
162
+ return expectMatchingValues ( items ) ;
164
163
}
164
+ /* c8 ignore next 3 */
165
+ // Not reachable, all possible output types have been considered.
166
+ assert ( false , 'Receives mixture of promises and values.' ) ;
167
+ }
165
168
166
- assert ( ! isPromise ( value2 ) ) ;
167
- expectJSON ( value2 ) . toDeepEqual ( value1 ) ;
168
- return value1 ;
169
+ function expectMatchingValues < T > ( values : ReadonlyArray < T > ) : T {
170
+ const remainingValues = values . slice ( 1 ) ;
171
+ for ( const value of remainingValues ) {
172
+ expectJSON ( value ) . toDeepEqual ( values [ 0 ] ) ;
173
+ }
174
+ return values [ 0 ] ;
175
+ }
176
+
177
+ function assertAllPromises < T > (
178
+ items : ReadonlyArray < PromiseOrValue < T > > ,
179
+ ) : items is ReadonlyArray < Promise < T > > {
180
+ for ( const item of items ) {
181
+ assert ( isPromise ( item ) ) ;
182
+ }
183
+ return true ;
184
+ }
185
+
186
+ function assertNoPromises < T > (
187
+ items : ReadonlyArray < PromiseOrValue < T > > ,
188
+ ) : items is ReadonlyArray < T > {
189
+ for ( const item of items ) {
190
+ assert ( ! isPromise ( item ) ) ;
191
+ }
192
+ return true ;
169
193
}
170
194
171
195
const DummyQueryType = new GraphQLObjectType ( {
@@ -195,10 +219,11 @@ function subscribeWithBadFn(
195
219
function subscribeWithBadArgs (
196
220
args : ExecutionArgs ,
197
221
) : PromiseOrValue < ExecutionResult | AsyncIterable < unknown > > {
198
- return expectEqualPromisesOrValues (
199
- subscribe ( args ) ,
222
+ return expectEqualPromisesOrValues ( [
223
+ execute ( args ) ,
200
224
createSourceEventStream ( args ) ,
201
- ) ;
225
+ subscribe ( args ) ,
226
+ ] ) ;
202
227
}
203
228
204
229
/* eslint-disable @typescript-eslint/require-await */
@@ -220,7 +245,7 @@ describe('Subscription Initialization Phase', () => {
220
245
yield { foo : 'FooValue' } ;
221
246
}
222
247
223
- const subscription = subscribe ( {
248
+ const subscription = execute ( {
224
249
schema,
225
250
document : parse ( 'subscription { foo }' ) ,
226
251
rootValue : { foo : fooGenerator } ,
@@ -256,7 +281,7 @@ describe('Subscription Initialization Phase', () => {
256
281
} ) ,
257
282
} ) ;
258
283
259
- const subscription = subscribe ( {
284
+ const subscription = execute ( {
260
285
schema,
261
286
document : parse ( 'subscription { foo }' ) ,
262
287
} ) ;
@@ -294,7 +319,7 @@ describe('Subscription Initialization Phase', () => {
294
319
} ) ,
295
320
} ) ;
296
321
297
- const promise = subscribe ( {
322
+ const promise = execute ( {
298
323
schema,
299
324
document : parse ( 'subscription { foo }' ) ,
300
325
} ) ;
@@ -329,7 +354,7 @@ describe('Subscription Initialization Phase', () => {
329
354
yield { foo : 'FooValue' } ;
330
355
}
331
356
332
- const subscription = subscribe ( {
357
+ const subscription = execute ( {
333
358
schema,
334
359
document : parse ( 'subscription { foo }' ) ,
335
360
rootValue : { customFoo : fooGenerator } ,
@@ -379,7 +404,7 @@ describe('Subscription Initialization Phase', () => {
379
404
} ) ,
380
405
} ) ;
381
406
382
- const subscription = subscribe ( {
407
+ const subscription = execute ( {
383
408
schema,
384
409
document : parse ( 'subscription { foo bar }' ) ,
385
410
} ) ;
@@ -530,7 +555,7 @@ describe('Subscription Initialization Phase', () => {
530
555
}
531
556
` ) ;
532
557
533
- // If we receive variables that cannot be coerced correctly, subscribe () will
558
+ // If we receive variables that cannot be coerced correctly, execute () will
534
559
// resolve to an ExecutionResult that contains an informative error description.
535
560
const result = subscribeWithBadArgs ( { schema, document, variableValues } ) ;
536
561
expectJSON ( result ) . toDeepEqual ( {
@@ -945,7 +970,7 @@ describe('Subscription Publish Phase', () => {
945
970
} ) ;
946
971
947
972
const document = parse ( 'subscription { newMessage }' ) ;
948
- const subscription = subscribe ( { schema, document } ) ;
973
+ const subscription = execute ( { schema, document } ) ;
949
974
assert ( isAsyncIterable ( subscription ) ) ;
950
975
951
976
expect ( await subscription . next ( ) ) . to . deep . equal ( {
@@ -1006,7 +1031,7 @@ describe('Subscription Publish Phase', () => {
1006
1031
} ) ;
1007
1032
1008
1033
const document = parse ( 'subscription { newMessage }' ) ;
1009
- const subscription = subscribe ( { schema, document } ) ;
1034
+ const subscription = execute ( { schema, document } ) ;
1010
1035
assert ( isAsyncIterable ( subscription ) ) ;
1011
1036
1012
1037
expect ( await subscription . next ( ) ) . to . deep . equal ( {
0 commit comments