Skip to content

Commit 83a7f41

Browse files
committed
refactor: streamline exeContext access
-- In at least a few cases, destructuring assignment from exeContext can improve code readability. -- Overrides to exeContext can be set using object spread syntax.
1 parent b3733f0 commit 83a7f41

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

src/execution/execute.ts

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ export function execute(args: ExecutionArgs): PromiseOrValue<ExecutionResult> {
200200
return { errors: exeContext };
201201
}
202202

203-
return executeQueryOrMutation(exeContext, exeContext.operation, rootValue);
203+
return executeQueryOrMutation(exeContext);
204204
}
205205

206206
/**
@@ -225,14 +225,8 @@ export function executeSync(args: ExecutionArgs): ExecutionResult {
225225
*/
226226
function executeQueryOrMutation(
227227
exeContext: ExecutionContext,
228-
operation: OperationDefinitionNode,
229-
rootValue: unknown,
230228
): PromiseOrValue<ExecutionResult> {
231-
const data = executeQueryOrMutationRootFields(
232-
exeContext,
233-
operation,
234-
rootValue,
235-
);
229+
const data = executeQueryOrMutationRootFields(exeContext);
236230

237231
if (isPromise(data)) {
238232
return data.then((resolved) => buildResponse(exeContext, resolved));
@@ -366,14 +360,14 @@ export function buildExecutionContext(
366360
* */
367361
function executeQueryOrMutationRootFields(
368362
exeContext: ExecutionContext,
369-
operation: OperationDefinitionNode,
370-
rootValue: unknown,
371363
): PromiseOrValue<ObjMap<unknown> | null> {
372-
const type = getOperationRootType(exeContext.schema, operation);
364+
const { schema, fragments, rootValue, operation, variableValues } =
365+
exeContext;
366+
const type = getOperationRootType(schema, operation);
373367
const fields = collectFields(
374-
exeContext.schema,
375-
exeContext.fragments,
376-
exeContext.variableValues,
368+
schema,
369+
fragments,
370+
variableValues,
377371
type,
378372
operation.selectionSet,
379373
);
@@ -568,6 +562,9 @@ export function buildResolveInfo(
568562
parentType: GraphQLObjectType,
569563
path: Path,
570564
): GraphQLResolveInfo {
565+
const { schema, fragments, rootValue, operation, variableValues } =
566+
exeContext;
567+
571568
// The resolve function's optional fourth argument is a collection of
572569
// information about the current execution state.
573570
return {
@@ -576,11 +573,11 @@ export function buildResolveInfo(
576573
returnType: fieldDef.type,
577574
parentType,
578575
path,
579-
schema: exeContext.schema,
580-
fragments: exeContext.fragments,
581-
rootValue: exeContext.rootValue,
582-
operation: exeContext.operation,
583-
variableValues: exeContext.variableValues,
576+
schema,
577+
fragments,
578+
rootValue,
579+
operation,
580+
variableValues,
584581
};
585582
}
586583

@@ -1078,17 +1075,18 @@ export async function executeSubscription(
10781075
}
10791076

10801077
// For each payload yielded from a subscription, map it over the normal
1081-
// GraphQL `execute` function, with `payload` as the rootValue.
1078+
// GraphQL `execute` function, with `payload` as the rootValue and with
1079+
// an empty set of errors.
10821080
// This implements the "MapSourceToResponseEvent" algorithm described in
10831081
// the GraphQL specification. The `execute` function provides the
10841082
// "ExecuteSubscriptionEvent" algorithm, as it is nearly identical to the
10851083
// "ExecuteQuery" algorithm, for which `execute` is also used.
10861084
const mapSourceToResponse = (payload: unknown) =>
1087-
executeQueryOrMutation(
1088-
{ ...exeContext, errors: [] },
1089-
exeContext.operation,
1090-
payload,
1091-
);
1085+
executeQueryOrMutation({
1086+
...exeContext,
1087+
rootValue: payload,
1088+
errors: [],
1089+
});
10921090

10931091
// Map every source value to a ExecutionResult value as described above.
10941092
return mapAsyncIterator(resultOrStream, mapSourceToResponse);

0 commit comments

Comments
 (0)