Skip to content

Commit 31f3204

Browse files
committed
refactor: subscribe: introduce new buildPerEventExecutionContext
= introduces `buildPerEventExecutionContext` that creates an `ExecutionContext` for each subscribe event from the original `ExecutionContext` used to create the event stream = `subscribe` now directly builds the `ExecutionContext` instead of relying on `createSourceEventStream` = introduces `createSourceEventStreamImpl` and `executeImpl` functions that operate on the built `ExecutionContext` rather the `ExecutionArgs` = `subscribe` calls the `createSourceEventStreamImpl` function on the original context and eventuallys calls `executeImpl` on the per event context created by `buildEventExecutionContext`. Motivation: 1. remove unnecessary `buildExecutionContext` call, reducing duplicate work 2. paves the way for easily enhancing the `buildPerEventExecutionContext` to add a new `perEventContextFactory` argument to augment the context argument passed to resolvers as need per event. depends on #3638
1 parent 4e0ed36 commit 31f3204

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

src/execution/execute.ts

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ export function execute(args: ExecutionArgs): PromiseOrValue<ExecutionResult> {
174174
return { errors: exeContext };
175175
}
176176

177+
return executeImpl(exeContext);
178+
}
179+
180+
function executeImpl(
181+
exeContext: ExecutionContext,
182+
): PromiseOrValue<ExecutionResult> {
177183
// Return a Promise that will eventually resolve to the data described by
178184
// The "Response" section of the GraphQL specification.
179185
//
@@ -319,6 +325,17 @@ export function buildExecutionContext(
319325
};
320326
}
321327

328+
function buildPerEventExecutionContext(
329+
exeContext: ExecutionContext,
330+
payload: unknown,
331+
): ExecutionContext {
332+
return {
333+
...exeContext,
334+
rootValue: payload,
335+
errors: [],
336+
};
337+
}
338+
322339
/**
323340
* Implements the "Executing operations" section of the spec.
324341
*/
@@ -1017,20 +1034,29 @@ export function subscribe(
10171034
): PromiseOrValue<
10181035
AsyncGenerator<ExecutionResult, void, void> | ExecutionResult
10191036
> {
1020-
const resultOrStream = createSourceEventStream(args);
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+
}
1045+
1046+
const resultOrStream = createSourceEventStreamImpl(exeContext);
10211047

10221048
if (isPromise(resultOrStream)) {
10231049
return resultOrStream.then((resolvedResultOrStream) =>
1024-
mapSourceToResponse(resolvedResultOrStream, args),
1050+
mapSourceToResponse(exeContext, resolvedResultOrStream),
10251051
);
10261052
}
10271053

1028-
return mapSourceToResponse(resultOrStream, args);
1054+
return mapSourceToResponse(exeContext, resultOrStream);
10291055
}
10301056

10311057
function mapSourceToResponse(
1058+
exeContext: ExecutionContext,
10321059
resultOrStream: ExecutionResult | AsyncIterable<unknown>,
1033-
args: ExecutionArgs,
10341060
): PromiseOrValue<
10351061
AsyncGenerator<ExecutionResult, void, void> | ExecutionResult
10361062
> {
@@ -1045,10 +1071,7 @@ function mapSourceToResponse(
10451071
// "ExecuteSubscriptionEvent" algorithm, as it is nearly identical to the
10461072
// "ExecuteQuery" algorithm, for which `execute` is also used.
10471073
return mapAsyncIterator(resultOrStream, (payload: unknown) =>
1048-
execute({
1049-
...args,
1050-
rootValue: payload,
1051-
}),
1074+
executeImpl(buildPerEventExecutionContext(exeContext, payload)),
10521075
);
10531076
}
10541077

@@ -1092,6 +1115,12 @@ export function createSourceEventStream(
10921115
return { errors: exeContext };
10931116
}
10941117

1118+
return createSourceEventStreamImpl(exeContext);
1119+
}
1120+
1121+
function createSourceEventStreamImpl(
1122+
exeContext: ExecutionContext,
1123+
): PromiseOrValue<AsyncIterable<unknown> | ExecutionResult> {
10951124
try {
10961125
const eventStream = executeSubscription(exeContext);
10971126
if (isPromise(eventStream)) {

0 commit comments

Comments
 (0)