Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

incrementalDelivery: remove singleResult wrapper #3733

Merged
merged 2 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
64 changes: 32 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 @@ -308,31 +306,31 @@ export function execute(args: ExecutionArgs): PromiseOrValue<ExecutionResult> {
* including `@defer` and `@stream` as proposed in
* 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`.
* This function returns a Promise of an ExperimentalIncrementalExecutionResults
* 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 @@ -1577,7 +1575,9 @@ export function experimentalSubscribeIncrementally(
}

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

Expand Down
2 changes: 1 addition & 1 deletion src/execution/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export {
export type {
ExecutionArgs,
ExecutionResult,
ExperimentalExecuteIncrementallyResults,
ExperimentalIncrementalExecutionResults,
InitialIncrementalExecutionResult,
SubsequentIncrementalExecutionResult,
IncrementalDeferResult,
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ export {
export type {
ExecutionArgs,
ExecutionResult,
ExperimentalExecuteIncrementallyResults,
ExperimentalIncrementalExecutionResults,
InitialIncrementalExecutionResult,
SubsequentIncrementalExecutionResult,
IncrementalDeferResult,
Expand Down