@@ -151,19 +151,17 @@ export interface FormattedExecutionResult<
151151 extensions ?: TExtensions ;
152152}
153153
154- export type ExperimentalExecuteIncrementallyResults <
154+ export interface ExperimentalIncrementalExecutionResults <
155155 TData = ObjMap < unknown > ,
156156 TExtensions = ObjMap < unknown > ,
157- > =
158- | { singleResult : ExecutionResult < TData , TExtensions > }
159- | {
160- initialResult : InitialIncrementalExecutionResult < TData , TExtensions > ;
161- subsequentResults : AsyncGenerator <
162- SubsequentIncrementalExecutionResult < TData , TExtensions > ,
163- void ,
164- void
165- > ;
166- } ;
157+ > {
158+ initialResult : InitialIncrementalExecutionResult < TData , TExtensions > ;
159+ subsequentResults : AsyncGenerator <
160+ SubsequentIncrementalExecutionResult < TData , TExtensions > ,
161+ void ,
162+ void
163+ > ;
164+ }
167165
168166export interface InitialIncrementalExecutionResult <
169167 TData = ObjMap < unknown > ,
@@ -287,19 +285,19 @@ const UNEXPECTED_MULTIPLE_PAYLOADS =
287285export function execute ( args : ExecutionArgs ) : PromiseOrValue < ExecutionResult > {
288286 const result = experimentalExecuteIncrementally ( args ) ;
289287 if ( ! isPromise ( result ) ) {
290- if ( 'singleResult ' in result ) {
291- return result . singleResult ;
288+ if ( 'initialResult ' in result ) {
289+ throw new Error ( UNEXPECTED_MULTIPLE_PAYLOADS ) ;
292290 }
293- throw new Error ( UNEXPECTED_MULTIPLE_PAYLOADS ) ;
291+ return result ;
294292 }
295293
296294 return result . then ( ( incrementalResult ) => {
297- if ( 'singleResult' in incrementalResult ) {
298- return incrementalResult . singleResult ;
295+ if ( 'initialResult' in incrementalResult ) {
296+ return {
297+ errors : [ new GraphQLError ( UNEXPECTED_MULTIPLE_PAYLOADS ) ] ,
298+ } ;
299299 }
300- return {
301- errors : [ new GraphQLError ( UNEXPECTED_MULTIPLE_PAYLOADS ) ] ,
302- } ;
300+ return incrementalResult ;
303301 } ) ;
304302}
305303
@@ -308,31 +306,31 @@ export function execute(args: ExecutionArgs): PromiseOrValue<ExecutionResult> {
308306 * including `@defer` and `@stream` as proposed in
309307 * https://github.com/graphql/graphql-spec/pull/742
310308 *
311- * This function returns a Promise of an ExperimentalExecuteIncrementallyResults
312- * object. This object either contains a single ExecutionResult as
313- * `singleResult`, or an `initialResult` and a stream of `subsequentResults`.
309+ * This function returns a Promise of an ExperimentalIncrementalExecutionResults
310+ * object. This object either consists of a single ExecutionResult, or an
311+ * object containing an `initialResult` and a stream of `subsequentResults`.
314312 *
315313 * If the arguments to this function do not result in a legal execution context,
316314 * a GraphQLError will be thrown immediately explaining the invalid input.
317315 */
318316export function experimentalExecuteIncrementally (
319317 args : ExecutionArgs ,
320- ) : PromiseOrValue < ExperimentalExecuteIncrementallyResults > {
318+ ) : PromiseOrValue < ExecutionResult | ExperimentalIncrementalExecutionResults > {
321319 // If a valid execution context cannot be created due to incorrect arguments,
322320 // a "Response" with only errors is returned.
323321 const exeContext = buildExecutionContext ( args ) ;
324322
325323 // Return early errors if execution context failed.
326324 if ( ! ( 'schema' in exeContext ) ) {
327- return { singleResult : { errors : exeContext } } ;
325+ return { errors : exeContext } ;
328326 }
329327
330328 return executeImpl ( exeContext ) ;
331329}
332330
333331function executeImpl (
334332 exeContext : ExecutionContext ,
335- ) : PromiseOrValue < ExperimentalExecuteIncrementallyResults > {
333+ ) : PromiseOrValue < ExecutionResult | ExperimentalIncrementalExecutionResults > {
336334 // Return a Promise that will eventually resolve to the data described by
337335 // The "Response" section of the GraphQL specification.
338336 //
@@ -359,11 +357,11 @@ function executeImpl(
359357 subsequentResults : yieldSubsequentPayloads ( exeContext ) ,
360358 } ;
361359 }
362- return { singleResult : initialResult } ;
360+ return initialResult ;
363361 } ,
364362 ( error ) => {
365363 exeContext . errors . push ( error ) ;
366- return { singleResult : buildResponse ( null , exeContext . errors ) } ;
364+ return buildResponse ( null , exeContext . errors ) ;
367365 } ,
368366 ) ;
369367 }
@@ -377,10 +375,10 @@ function executeImpl(
377375 subsequentResults : yieldSubsequentPayloads ( exeContext ) ,
378376 } ;
379377 }
380- return { singleResult : initialResult } ;
378+ return initialResult ;
381379 } catch ( error ) {
382380 exeContext . errors . push ( error ) ;
383- return { singleResult : buildResponse ( null , exeContext . errors ) } ;
381+ return buildResponse ( null , exeContext . errors ) ;
384382 }
385383}
386384
@@ -397,7 +395,7 @@ export function executeSync(args: ExecutionArgs): ExecutionResult {
397395 throw new Error ( 'GraphQL execution failed to complete synchronously.' ) ;
398396 }
399397
400- return result . singleResult ;
398+ return result ;
401399}
402400
403401/**
@@ -1469,7 +1467,11 @@ export const defaultFieldResolver: GraphQLFieldResolver<unknown, unknown> =
14691467 * If the operation succeeded, the promise resolves to an AsyncIterator, which
14701468 * yields a stream of ExecutionResults representing the response stream.
14711469 *
1472- * This function does not support incremental delivery (`@defer` and `@stream`).
1470+ * This function also supports experimental incremental delivery directives
1471+ * (`@defer` and `@stream`). To use these directives, they should be added to
1472+ * the schema and TS generic parameter TMaybeIncremental should be set to `true`
1473+ * (default: false).
1474+ * * This function does not support incremental delivery (`@defer` and `@stream`).
14731475 * If an operation which would defer or stream data is executed with this
14741476 * function, each `InitialIncrementalExecutionResult` and
14751477 * `SubsequentIncrementalExecutionResult` in the result stream will be replaced
@@ -1577,7 +1579,9 @@ export function experimentalSubscribeIncrementally(
15771579}
15781580
15791581async function * ensureAsyncIterable (
1580- someExecutionResult : ExperimentalExecuteIncrementallyResults ,
1582+ someExecutionResult :
1583+ | ExecutionResult
1584+ | ExperimentalIncrementalExecutionResults ,
15811585) : AsyncGenerator <
15821586 | ExecutionResult
15831587 | InitialIncrementalExecutionResult
@@ -1589,7 +1593,7 @@ async function* ensureAsyncIterable(
15891593 yield someExecutionResult . initialResult ;
15901594 yield * someExecutionResult . subsequentResults ;
15911595 } else {
1592- yield someExecutionResult . singleResult ;
1596+ yield someExecutionResult ;
15931597 }
15941598}
15951599
0 commit comments