11import {
22 subscribe ,
3- execute ,
43 validate ,
54 GraphQLSchema ,
65 isSchema ,
@@ -13,9 +12,11 @@ import {
1312 GraphQLObjectType ,
1413} from 'graphql' ;
1514
15+ import { execute } from 'graphql/experimental' ;
16+
1617import isPromise from 'is-promise' ;
1718
18- import { mapAsyncIterator , ExecutionResult } from '@graphql-tools/utils' ;
19+ import { mapAsyncIterator , ExecutionResult , isAsyncIterable } from '@graphql-tools/utils' ;
1920
2021import {
2122 IDelegateToSchemaOptions ,
@@ -25,6 +26,7 @@ import {
2526 StitchingInfo ,
2627 Endpoint ,
2728 Transform ,
29+ Executor ,
2830} from './types' ;
2931
3032import { isSubschemaConfig } from './Subschema' ;
@@ -189,8 +191,16 @@ export function delegateRequest({
189191 info,
190192 } ) ;
191193
192- if ( isPromise ( executionResult ) ) {
193- return executionResult . then ( originalResult => transformer . transformResult ( originalResult ) ) ;
194+ if ( isAsyncIterable ( executionResult ) ) {
195+ return asyncIterableToResult ( executionResult ) . then ( originalResult => {
196+ const transformedResult = transformer . transformResult ( originalResult ) ;
197+ transformedResult [ 'ASYNC_ITERABLE' ] = executionResult ;
198+ return transformedResult ;
199+ } ) ;
200+ } else if ( isPromise ( executionResult ) ) {
201+ return ( executionResult as Promise < ExecutionResult > ) . then ( originalResult =>
202+ transformer . transformResult ( originalResult )
203+ ) ;
194204 }
195205 return transformer . transformResult ( executionResult ) ;
196206 }
@@ -203,7 +213,7 @@ export function delegateRequest({
203213 context,
204214 info,
205215 } ) . then ( ( subscriptionResult : AsyncIterableIterator < ExecutionResult > | ExecutionResult ) => {
206- if ( Symbol . asyncIterator in subscriptionResult ) {
216+ if ( isAsyncIterable ( subscriptionResult ) ) {
207217 // "subscribe" to the subscription result and map the result through the transforms
208218 return mapAsyncIterator < ExecutionResult , any > (
209219 subscriptionResult as AsyncIterableIterator < ExecutionResult > ,
@@ -229,15 +239,15 @@ function validateRequest(targetSchema: GraphQLSchema, document: DocumentNode) {
229239 }
230240}
231241
232- function createDefaultExecutor ( schema : GraphQLSchema , rootValue : Record < string , any > ) {
233- return ( { document, context, variables, info } : ExecutionParams ) =>
242+ function createDefaultExecutor ( schema : GraphQLSchema , rootValue : Record < string , any > ) : Executor {
243+ return ( ( { document, context, variables, info } : ExecutionParams ) =>
234244 execute ( {
235245 schema,
236246 document,
237247 contextValue : context ,
238248 variableValues : variables ,
239249 rootValue : rootValue ?? info ?. rootValue ,
240- } ) ;
250+ } ) ) as Executor ;
241251}
242252
243253function createDefaultSubscriber ( schema : GraphQLSchema , rootValue : Record < string , any > ) {
@@ -250,3 +260,9 @@ function createDefaultSubscriber(schema: GraphQLSchema, rootValue: Record<string
250260 rootValue : rootValue ?? info ?. rootValue ,
251261 } ) as any ;
252262}
263+
264+ async function asyncIterableToResult ( asyncIterable : AsyncIterable < ExecutionResult > ) : Promise < any > {
265+ const asyncIterator = asyncIterable [ Symbol . asyncIterator ] ( ) ;
266+ const payload = await asyncIterator . next ( ) ;
267+ return payload . value ;
268+ }
0 commit comments