Skip to content

Commit 17ffea7

Browse files
committed
refactor: DRY with context building and *Impl functions
The `execute`/`executeImpl` and `createSourceEventStream`/`createSourceEventStreamImpl` functions follow the same basic pattern of building the contet and using it to run a function. This PR extracts that pattern into a separate function. For good measure, the same pattern in applied to the soon-to-be-deprecated `subscribe` function. Hheavier refactoring is on the way from @IvanGoncharov (see #3639 (review)), but in the meantime, this consolidates the common pattern without any breaking changes.
1 parent c1fe951 commit 17ffea7

File tree

1 file changed

+18
-27
lines changed

1 file changed

+18
-27
lines changed

src/execution/execute.ts

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@ export interface ExecutionArgs {
165165
* a GraphQLError will be thrown immediately explaining the invalid input.
166166
*/
167167
export function execute(args: ExecutionArgs): PromiseOrValue<ExecutionResult> {
168+
return prepareContextAndRunFn(args, executeImpl);
169+
}
170+
171+
function prepareContextAndRunFn<T>(
172+
args: ExecutionArgs,
173+
fn: (exeContext: ExecutionContext) => T,
174+
): ExecutionResult | T {
168175
// If a valid execution context cannot be created due to incorrect arguments,
169176
// a "Response" with only errors is returned.
170177
const exeContext = buildExecutionContext(args);
@@ -174,7 +181,7 @@ export function execute(args: ExecutionArgs): PromiseOrValue<ExecutionResult> {
174181
return { errors: exeContext };
175182
}
176183

177-
return executeImpl(exeContext);
184+
return fn(exeContext);
178185
}
179186

180187
function executeImpl(
@@ -1034,24 +1041,17 @@ export function subscribe(
10341041
): PromiseOrValue<
10351042
AsyncGenerator<ExecutionResult, void, void> | ExecutionResult
10361043
> {
1037-
// If a valid execution context cannot be created due to incorrect arguments,
1038-
// a "Response" with only errors is returned.
1039-
const exeContext = buildExecutionContext(args);
1044+
return prepareContextAndRunFn(args, (exeContext: ExecutionContext) => {
1045+
const resultOrStream = createSourceEventStreamImpl(exeContext);
10401046

1041-
// Return early errors if execution context failed.
1042-
if (!('schema' in exeContext)) {
1043-
return { errors: exeContext };
1044-
}
1045-
1046-
const resultOrStream = createSourceEventStreamImpl(exeContext);
1047-
1048-
if (isPromise(resultOrStream)) {
1049-
return resultOrStream.then((resolvedResultOrStream) =>
1050-
mapSourceToResponse(exeContext, resolvedResultOrStream),
1051-
);
1052-
}
1047+
if (isPromise(resultOrStream)) {
1048+
return resultOrStream.then((resolvedResultOrStream) =>
1049+
mapSourceToResponse(exeContext, resolvedResultOrStream),
1050+
);
1051+
}
10531052

1054-
return mapSourceToResponse(exeContext, resultOrStream);
1053+
return mapSourceToResponse(exeContext, resultOrStream);
1054+
});
10551055
}
10561056

10571057
function mapSourceToResponse(
@@ -1106,16 +1106,7 @@ function mapSourceToResponse(
11061106
export function createSourceEventStream(
11071107
args: ExecutionArgs,
11081108
): PromiseOrValue<AsyncIterable<unknown> | ExecutionResult> {
1109-
// If a valid execution context cannot be created due to incorrect arguments,
1110-
// a "Response" with only errors is returned.
1111-
const exeContext = buildExecutionContext(args);
1112-
1113-
// Return early errors if execution context failed.
1114-
if (!('schema' in exeContext)) {
1115-
return { errors: exeContext };
1116-
}
1117-
1118-
return createSourceEventStreamImpl(exeContext);
1109+
return prepareContextAndRunFn(args, createSourceEventStreamImpl);
11191110
}
11201111

11211112
function createSourceEventStreamImpl(

0 commit comments

Comments
 (0)