Skip to content

Commit 22a0bfa

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 22a0bfa

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

src/execution/execute.ts

Lines changed: 16 additions & 19 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,15 +1041,14 @@ 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);
1040-
1041-
// Return early errors if execution context failed.
1042-
if (!('schema' in exeContext)) {
1043-
return { errors: exeContext };
1044-
}
1044+
return prepareContextAndRunFn(args, subscribeImpl);
1045+
}
10451046

1047+
export function subscribeImpl(
1048+
exeContext: ExecutionContext,
1049+
): PromiseOrValue<
1050+
AsyncGenerator<ExecutionResult, void, void> | ExecutionResult
1051+
> {
10461052
const resultOrStream = createSourceEventStreamImpl(exeContext);
10471053

10481054
if (isPromise(resultOrStream)) {
@@ -1106,16 +1112,7 @@ function mapSourceToResponse(
11061112
export function createSourceEventStream(
11071113
args: ExecutionArgs,
11081114
): 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);
1115+
return prepareContextAndRunFn(args, createSourceEventStreamImpl);
11191116
}
11201117

11211118
function createSourceEventStreamImpl(

0 commit comments

Comments
 (0)