Skip to content

Commit fd40497

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 graphql#3639 (review)), but in the meantime, this consolidates the common pattern without any breaking changes.
1 parent 59c87c3 commit fd40497

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(
@@ -1107,24 +1114,17 @@ export function subscribe(
11071114
): PromiseOrValue<
11081115
AsyncGenerator<ExecutionResult, void, void> | ExecutionResult
11091116
> {
1110-
// If a valid execution context cannot be created due to incorrect arguments,
1111-
// a "Response" with only errors is returned.
1112-
const exeContext = buildExecutionContext(args);
1117+
return prepareContextAndRunFn(args, (exeContext: ExecutionContext) => {
1118+
const resultOrStream = createSourceEventStreamImpl(exeContext);
11131119

1114-
// Return early errors if execution context failed.
1115-
if (!('schema' in exeContext)) {
1116-
return { errors: exeContext };
1117-
}
1118-
1119-
const resultOrStream = createSourceEventStreamImpl(exeContext);
1120-
1121-
if (isPromise(resultOrStream)) {
1122-
return resultOrStream.then((resolvedResultOrStream) =>
1123-
mapSourceToResponse(exeContext, resolvedResultOrStream),
1124-
);
1125-
}
1120+
if (isPromise(resultOrStream)) {
1121+
return resultOrStream.then((resolvedResultOrStream) =>
1122+
mapSourceToResponse(exeContext, resolvedResultOrStream),
1123+
);
1124+
}
11261125

1127-
return mapSourceToResponse(exeContext, resultOrStream);
1126+
return mapSourceToResponse(exeContext, resultOrStream);
1127+
});
11281128
}
11291129

11301130
function mapSourceToResponse(
@@ -1179,16 +1179,7 @@ function mapSourceToResponse(
11791179
export function createSourceEventStream(
11801180
args: ExecutionArgs,
11811181
): PromiseOrValue<AsyncIterable<unknown> | ExecutionResult> {
1182-
// If a valid execution context cannot be created due to incorrect arguments,
1183-
// a "Response" with only errors is returned.
1184-
const exeContext = buildExecutionContext(args);
1185-
1186-
// Return early errors if execution context failed.
1187-
if (!('schema' in exeContext)) {
1188-
return { errors: exeContext };
1189-
}
1190-
1191-
return createSourceEventStreamImpl(exeContext);
1182+
return prepareContextAndRunFn(args, createSourceEventStreamImpl);
11921183
}
11931184

11941185
function createSourceEventStreamImpl(

0 commit comments

Comments
 (0)