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' ;
@@ -187,8 +189,16 @@ export function delegateRequest({
187189 info,
188190 } ) ;
189191
190- if ( isPromise ( executionResult ) ) {
191- return executionResult . then ( originalResult => transformer . transformResult ( originalResult ) ) ;
192+ if ( isAsyncIterable ( executionResult ) ) {
193+ return asyncIterableToResult ( executionResult ) . then ( originalResult => {
194+ const transformedResult = transformer . transformResult ( originalResult ) ;
195+ transformedResult [ 'ASYNC_ITERABLE' ] = executionResult ;
196+ return transformedResult ;
197+ } ) ;
198+ } else if ( isPromise ( executionResult ) ) {
199+ return ( executionResult as Promise < ExecutionResult > ) . then ( originalResult =>
200+ transformer . transformResult ( originalResult )
201+ ) ;
192202 }
193203 return transformer . transformResult ( executionResult ) ;
194204 }
@@ -201,7 +211,7 @@ export function delegateRequest({
201211 context,
202212 info,
203213 } ) . then ( ( subscriptionResult : AsyncIterableIterator < ExecutionResult > | ExecutionResult ) => {
204- if ( Symbol . asyncIterator in subscriptionResult ) {
214+ if ( isAsyncIterable ( subscriptionResult ) ) {
205215 // "subscribe" to the subscription result and map the result through the transforms
206216 return mapAsyncIterator < ExecutionResult , any > (
207217 subscriptionResult as AsyncIterableIterator < ExecutionResult > ,
@@ -227,15 +237,15 @@ function validateRequest(targetSchema: GraphQLSchema, document: DocumentNode) {
227237 }
228238}
229239
230- function createDefaultExecutor ( schema : GraphQLSchema , rootValue : Record < string , any > ) {
231- return ( { document, context, variables, info } : ExecutionParams ) =>
240+ function createDefaultExecutor ( schema : GraphQLSchema , rootValue : Record < string , any > ) : Executor {
241+ return ( ( { document, context, variables, info } : ExecutionParams ) =>
232242 execute ( {
233243 schema,
234244 document,
235245 contextValue : context ,
236246 variableValues : variables ,
237247 rootValue : rootValue ?? info ?. rootValue ,
238- } ) ;
248+ } ) ) as Executor ;
239249}
240250
241251function createDefaultSubscriber ( schema : GraphQLSchema , rootValue : Record < string , any > ) {
@@ -248,3 +258,9 @@ function createDefaultSubscriber(schema: GraphQLSchema, rootValue: Record<string
248258 rootValue : rootValue ?? info ?. rootValue ,
249259 } ) as any ;
250260}
261+
262+ async function asyncIterableToResult ( asyncIterable : AsyncIterable < ExecutionResult > ) : Promise < any > {
263+ const asyncIterator = asyncIterable [ Symbol . asyncIterator ] ( ) ;
264+ const payload = await asyncIterator . next ( ) ;
265+ return payload . value ;
266+ }
0 commit comments