@@ -15,7 +15,7 @@ import { GraphQLBoolean, GraphQLInt, GraphQLString } from '../../type/scalars';
1515import { GraphQLSchema } from '../../type/schema' ;
1616
1717import type { ExecutionArgs , ExecutionResult } from '../execute' ;
18- import { createSourceEventStream , subscribe } from '../execute' ;
18+ import { createSourceEventStream , execute , subscribe } from '../execute' ;
1919
2020import { SimplePubSub } from './simplePubSub' ;
2121
@@ -122,7 +122,7 @@ function createSubscription(pubsub: SimplePubSub<Email>) {
122122 } ) ,
123123 } ;
124124
125- return subscribe ( { schema : emailSchema , document, rootValue : data } ) ;
125+ return execute ( { schema : emailSchema , document, rootValue : data } ) ;
126126}
127127
128128// TODO: consider adding this method to testUtils (with tests)
@@ -150,22 +150,46 @@ function expectPromise(maybePromise: unknown) {
150150 } ;
151151}
152152
153- // TODO: consider adding this method to testUtils (with tests)
153+ // TODO: consider adding these method to testUtils (with tests)
154154function expectEqualPromisesOrValues < T > (
155- value1 : PromiseOrValue < T > ,
156- value2 : PromiseOrValue < T > ,
155+ items : ReadonlyArray < PromiseOrValue < T > > ,
157156) : 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 ) ;
164163 }
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+ }
165168
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 ;
169193}
170194
171195const DummyQueryType = new GraphQLObjectType ( {
@@ -195,10 +219,11 @@ function subscribeWithBadFn(
195219function subscribeWithBadArgs (
196220 args : ExecutionArgs ,
197221) : PromiseOrValue < ExecutionResult | AsyncIterable < unknown > > {
198- return expectEqualPromisesOrValues (
199- subscribe ( args ) ,
222+ return expectEqualPromisesOrValues ( [
223+ execute ( args ) ,
200224 createSourceEventStream ( args ) ,
201- ) ;
225+ subscribe ( args ) ,
226+ ] ) ;
202227}
203228
204229/* eslint-disable @typescript-eslint/require-await */
@@ -220,7 +245,7 @@ describe('Subscription Initialization Phase', () => {
220245 yield { foo : 'FooValue' } ;
221246 }
222247
223- const subscription = subscribe ( {
248+ const subscription = execute ( {
224249 schema,
225250 document : parse ( 'subscription { foo }' ) ,
226251 rootValue : { foo : fooGenerator } ,
@@ -256,7 +281,7 @@ describe('Subscription Initialization Phase', () => {
256281 } ) ,
257282 } ) ;
258283
259- const subscription = subscribe ( {
284+ const subscription = execute ( {
260285 schema,
261286 document : parse ( 'subscription { foo }' ) ,
262287 } ) ;
@@ -294,7 +319,7 @@ describe('Subscription Initialization Phase', () => {
294319 } ) ,
295320 } ) ;
296321
297- const promise = subscribe ( {
322+ const promise = execute ( {
298323 schema,
299324 document : parse ( 'subscription { foo }' ) ,
300325 } ) ;
@@ -329,7 +354,7 @@ describe('Subscription Initialization Phase', () => {
329354 yield { foo : 'FooValue' } ;
330355 }
331356
332- const subscription = subscribe ( {
357+ const subscription = execute ( {
333358 schema,
334359 document : parse ( 'subscription { foo }' ) ,
335360 rootValue : { customFoo : fooGenerator } ,
@@ -379,7 +404,7 @@ describe('Subscription Initialization Phase', () => {
379404 } ) ,
380405 } ) ;
381406
382- const subscription = subscribe ( {
407+ const subscription = execute ( {
383408 schema,
384409 document : parse ( 'subscription { foo bar }' ) ,
385410 } ) ;
@@ -530,7 +555,7 @@ describe('Subscription Initialization Phase', () => {
530555 }
531556 ` ) ;
532557
533- // If we receive variables that cannot be coerced correctly, subscribe () will
558+ // If we receive variables that cannot be coerced correctly, execute () will
534559 // resolve to an ExecutionResult that contains an informative error description.
535560 const result = subscribeWithBadArgs ( { schema, document, variableValues } ) ;
536561 expectJSON ( result ) . toDeepEqual ( {
@@ -945,7 +970,7 @@ describe('Subscription Publish Phase', () => {
945970 } ) ;
946971
947972 const document = parse ( 'subscription { newMessage }' ) ;
948- const subscription = subscribe ( { schema, document } ) ;
973+ const subscription = execute ( { schema, document } ) ;
949974 assert ( isAsyncIterable ( subscription ) ) ;
950975
951976 expect ( await subscription . next ( ) ) . to . deep . equal ( {
@@ -1006,7 +1031,7 @@ describe('Subscription Publish Phase', () => {
10061031 } ) ;
10071032
10081033 const document = parse ( 'subscription { newMessage }' ) ;
1009- const subscription = subscribe ( { schema, document } ) ;
1034+ const subscription = execute ( { schema, document } ) ;
10101035 assert ( isAsyncIterable ( subscription ) ) ;
10111036
10121037 expect ( await subscription . next ( ) ) . to . deep . equal ( {
0 commit comments