Skip to content

refactor: small refactors within execute #3638

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 8 additions & 31 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,6 @@ export interface ExecutionArgs {
* a GraphQLError will be thrown immediately explaining the invalid input.
*/
export function execute(args: ExecutionArgs): PromiseOrValue<ExecutionResult> {
const { schema, document, variableValues, rootValue } = args;

// If arguments are missing or incorrect, throw an error.
assertValidExecutionArguments(schema, document, variableValues);

// If a valid execution context cannot be created due to incorrect arguments,
// a "Response" with only errors is returned.
const exeContext = buildExecutionContext(args);
Expand All @@ -189,7 +184,7 @@ export function execute(args: ExecutionArgs): PromiseOrValue<ExecutionResult> {
// in this case is the entire response.
try {
const { operation } = exeContext;
const result = executeOperation(exeContext, operation, rootValue);
const result = executeOperation(exeContext, operation);
if (isPromise(result)) {
return result.then(
(data) => buildResponse(data, exeContext.errors),
Expand Down Expand Up @@ -281,6 +276,9 @@ export function buildExecutionContext(
subscribeFieldResolver,
} = args;

// If arguments are missing or incorrect, throw an error.
assertValidExecutionArguments(schema, document, rawVariableValues);

let operation: OperationDefinitionNode | undefined;
const fragments: ObjMap<FragmentDefinitionNode> = Object.create(null);
for (const definition of document.definitions) {
Expand Down Expand Up @@ -349,8 +347,7 @@ export function buildExecutionContext(
function executeOperation(
exeContext: ExecutionContext,
operation: OperationDefinitionNode,
rootValue: unknown,
): PromiseOrValue<ObjMap<unknown> | null> {
): PromiseOrValue<ObjMap<unknown>> {
const rootType = exeContext.schema.getRootType(operation.operation);
if (rootType == null) {
throw new GraphQLError(
Expand All @@ -368,6 +365,8 @@ function executeOperation(
);
const path = undefined;

const { rootValue } = exeContext;

switch (operation.operation) {
case OperationTypeNode.QUERY:
return executeFields(exeContext, rootType, rootValue, path, rootFields);
Expand Down Expand Up @@ -1107,31 +1106,9 @@ function mapSourceToResponse(
export function createSourceEventStream(
args: ExecutionArgs,
): PromiseOrValue<AsyncIterable<unknown> | ExecutionResult> {
const {
schema,
document,
rootValue,
contextValue,
variableValues,
operationName,
subscribeFieldResolver,
} = args;

// If arguments are missing or incorrectly typed, this is an internal
// developer mistake which should throw an early error.
assertValidExecutionArguments(schema, document, variableValues);

// If a valid execution context cannot be created due to incorrect arguments,
// a "Response" with only errors is returned.
const exeContext = buildExecutionContext({
schema,
document,
rootValue,
contextValue,
variableValues,
operationName,
subscribeFieldResolver,
});
const exeContext = buildExecutionContext(args);

// Return early errors if execution context failed.
if (!('schema' in exeContext)) {
Expand Down