Skip to content

Commit

Permalink
incrementalDelivery: remove singleResult wrapper
Browse files Browse the repository at this point in the history
extracted from #3732
  • Loading branch information
yaacovCR committed Sep 13, 2022
1 parent 07a95b9 commit e1b5d34
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/execution/__tests__/defer-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ async function complete(document: DocumentNode) {
}
return results;
}
return result.singleResult;
return result;
}

describe('Execute: defer directive', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/execution/__tests__/stream-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ async function complete(document: DocumentNode, rootValue: unknown = {}) {
}
return results;
}
return result.singleResult;
return result;
}

async function completeAsync(
Expand Down
68 changes: 36 additions & 32 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,19 +151,17 @@ export interface FormattedExecutionResult<
extensions?: TExtensions;
}

export type ExperimentalExecuteIncrementallyResults<
export interface ExperimentalIncrementalExecutionResults<
TData = ObjMap<unknown>,
TExtensions = ObjMap<unknown>,
> =
| { singleResult: ExecutionResult<TData, TExtensions> }
| {
initialResult: InitialIncrementalExecutionResult<TData, TExtensions>;
subsequentResults: AsyncGenerator<
SubsequentIncrementalExecutionResult<TData, TExtensions>,
void,
void
>;
};
> {
initialResult: InitialIncrementalExecutionResult<TData, TExtensions>;
subsequentResults: AsyncGenerator<
SubsequentIncrementalExecutionResult<TData, TExtensions>,
void,
void
>;
}

export interface InitialIncrementalExecutionResult<
TData = ObjMap<unknown>,
Expand Down Expand Up @@ -287,19 +285,19 @@ const UNEXPECTED_MULTIPLE_PAYLOADS =
export function execute(args: ExecutionArgs): PromiseOrValue<ExecutionResult> {
const result = experimentalExecuteIncrementally(args);
if (!isPromise(result)) {
if ('singleResult' in result) {
return result.singleResult;
if ('initialResult' in result) {
throw new Error(UNEXPECTED_MULTIPLE_PAYLOADS);
}
throw new Error(UNEXPECTED_MULTIPLE_PAYLOADS);
return result;
}

return result.then((incrementalResult) => {
if ('singleResult' in incrementalResult) {
return incrementalResult.singleResult;
if ('initialResult' in incrementalResult) {
return {
errors: [new GraphQLError(UNEXPECTED_MULTIPLE_PAYLOADS)],
};
}
return {
errors: [new GraphQLError(UNEXPECTED_MULTIPLE_PAYLOADS)],
};
return incrementalResult;
});
}

Expand All @@ -309,30 +307,30 @@ export function execute(args: ExecutionArgs): PromiseOrValue<ExecutionResult> {
* https://github.com/graphql/graphql-spec/pull/742
*
* This function returns a Promise of an ExperimentalExecuteIncrementallyResults
* object. This object either contains a single ExecutionResult as
* `singleResult`, or an `initialResult` and a stream of `subsequentResults`.
* object. This object either consists of a single ExecutionResult, or an
* object containing an `initialResult` and a stream of `subsequentResults`.
*
* If the arguments to this function do not result in a legal execution context,
* a GraphQLError will be thrown immediately explaining the invalid input.
*/
export function experimentalExecuteIncrementally(
args: ExecutionArgs,
): PromiseOrValue<ExperimentalExecuteIncrementallyResults> {
): PromiseOrValue<ExecutionResult | ExperimentalIncrementalExecutionResults> {
// If a valid execution context cannot be created due to incorrect arguments,
// a "Response" with only errors is returned.
const exeContext = buildExecutionContext(args);

// Return early errors if execution context failed.
if (!('schema' in exeContext)) {
return { singleResult: { errors: exeContext } };
return { errors: exeContext };
}

return executeImpl(exeContext);
}

function executeImpl(
exeContext: ExecutionContext,
): PromiseOrValue<ExperimentalExecuteIncrementallyResults> {
): PromiseOrValue<ExecutionResult | ExperimentalIncrementalExecutionResults> {
// Return a Promise that will eventually resolve to the data described by
// The "Response" section of the GraphQL specification.
//
Expand All @@ -359,11 +357,11 @@ function executeImpl(
subsequentResults: yieldSubsequentPayloads(exeContext),
};
}
return { singleResult: initialResult };
return initialResult;
},
(error) => {
exeContext.errors.push(error);
return { singleResult: buildResponse(null, exeContext.errors) };
return buildResponse(null, exeContext.errors);
},
);
}
Expand All @@ -377,10 +375,10 @@ function executeImpl(
subsequentResults: yieldSubsequentPayloads(exeContext),
};
}
return { singleResult: initialResult };
return initialResult;
} catch (error) {
exeContext.errors.push(error);
return { singleResult: buildResponse(null, exeContext.errors) };
return buildResponse(null, exeContext.errors);
}
}

Expand All @@ -397,7 +395,7 @@ export function executeSync(args: ExecutionArgs): ExecutionResult {
throw new Error('GraphQL execution failed to complete synchronously.');
}

return result.singleResult;
return result;
}

/**
Expand Down Expand Up @@ -1469,7 +1467,11 @@ export const defaultFieldResolver: GraphQLFieldResolver<unknown, unknown> =
* If the operation succeeded, the promise resolves to an AsyncIterator, which
* yields a stream of ExecutionResults representing the response stream.
*
* This function does not support incremental delivery (`@defer` and `@stream`).
* This function also supports experimental incremental delivery directives
* (`@defer` and `@stream`). To use these directives, they should be added to
* the schema and TS generic parameter TMaybeIncremental should be set to `true`
* (default: false).
* * This function does not support incremental delivery (`@defer` and `@stream`).
* If an operation which would defer or stream data is executed with this
* function, each `InitialIncrementalExecutionResult` and
* `SubsequentIncrementalExecutionResult` in the result stream will be replaced
Expand Down Expand Up @@ -1577,7 +1579,9 @@ export function experimentalSubscribeIncrementally(
}

async function* ensureAsyncIterable(
someExecutionResult: ExperimentalExecuteIncrementallyResults,
someExecutionResult:
| ExecutionResult
| ExperimentalIncrementalExecutionResults,
): AsyncGenerator<
| ExecutionResult
| InitialIncrementalExecutionResult
Expand All @@ -1589,7 +1593,7 @@ async function* ensureAsyncIterable(
yield someExecutionResult.initialResult;
yield* someExecutionResult.subsequentResults;
} else {
yield someExecutionResult.singleResult;
yield someExecutionResult;
}
}

Expand Down

0 comments on commit e1b5d34

Please sign in to comment.