Skip to content

Commit

Permalink
polish: rename executeImpl to executeOperation (#4045)
Browse files Browse the repository at this point in the history
inline much of the original executeOperation
leave out executeRootGroupedFieldSet, will help reduce the diff for
#4026
  • Loading branch information
yaacovCR authored Apr 5, 2024
1 parent a81e623 commit 13f84cd
Showing 1 changed file with 98 additions and 86 deletions.
184 changes: 98 additions & 86 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,38 +234,103 @@ export function experimentalExecuteIncrementally(
return { errors: exeContext };
}

return executeImpl(exeContext);
return executeOperation(exeContext);
}

function executeImpl(
/**
* Implements the "Executing operations" section of the spec.
*
* Returns a Promise that will eventually resolve to the data described by
* The "Response" section of the GraphQL specification.
*
* If errors are encountered while executing a GraphQL field, only that
* field and its descendants will be omitted, and sibling fields will still
* be executed. An execution which encounters errors will still result in a
* resolved Promise.
*
* Errors from sub-fields of a NonNull type may propagate to the top level,
* at which point we still log the error and null the parent field, which
* in this case is the entire response.
*/
function executeOperation(
exeContext: ExecutionContext,
): PromiseOrValue<ExecutionResult | ExperimentalIncrementalExecutionResults> {
// Return a Promise that will eventually resolve to the data described by
// The "Response" section of the GraphQL specification.
//
// If errors are encountered while executing a GraphQL field, only that
// field and its descendants will be omitted, and sibling fields will still
// be executed. An execution which encounters errors will still result in a
// resolved Promise.
//
// Errors from sub-fields of a NonNull type may propagate to the top level,
// at which point we still log the error and null the parent field, which
// in this case is the entire response.
const incrementalPublisher = exeContext.incrementalPublisher;
const initialResultRecord = new InitialResultRecord();
try {
const data = executeOperation(exeContext, initialResultRecord);
if (isPromise(data)) {
return data.then(
const {
operation,
schema,
fragments,
variableValues,
rootValue,
incrementalPublisher,
} = exeContext;
const rootType = schema.getRootType(operation.operation);
if (rootType == null) {
throw new GraphQLError(
`Schema is not configured to execute ${operation.operation} operation.`,
{ nodes: operation },
);
}

const { fields, newDeferUsages } = collectFields(
schema,
fragments,
variableValues,
rootType,
operation,
);
const { groupedFieldSet, newGroupedFieldSetDetailsMap } =
buildFieldPlan(fields);

const newDeferMap = addNewDeferredFragments(
incrementalPublisher,
newDeferUsages,
initialResultRecord,
);

const path = undefined;

const newDeferredGroupedFieldSetRecords = addNewDeferredGroupedFieldSets(
incrementalPublisher,
newGroupedFieldSetDetailsMap,
newDeferMap,
path,
);

const result = executeRootGroupedFieldSet(
exeContext,
operation.operation,
rootType,
rootValue,
groupedFieldSet,
initialResultRecord,
newDeferMap,
);

executeDeferredGroupedFieldSets(
exeContext,
rootType,
rootValue,
path,
newDeferredGroupedFieldSetRecords,
newDeferMap,
);

if (isPromise(result)) {
return result.then(
(resolved) =>
incrementalPublisher.buildDataResponse(initialResultRecord, resolved),
(error) =>
incrementalPublisher.buildErrorResponse(initialResultRecord, error),
);
}
return incrementalPublisher.buildDataResponse(initialResultRecord, data);
return incrementalPublisher.buildDataResponse(initialResultRecord, result);
} catch (error) {
return incrementalPublisher.buildErrorResponse(initialResultRecord, error);
return exeContext.incrementalPublisher.buildErrorResponse(
initialResultRecord,
error,
);
}
}

Expand Down Expand Up @@ -384,102 +449,49 @@ function buildPerEventExecutionContext(
};
}

/**
* Implements the "Executing operations" section of the spec.
*/
function executeOperation(
function executeRootGroupedFieldSet(
exeContext: ExecutionContext,
operation: OperationTypeNode,
rootType: GraphQLObjectType,
rootValue: unknown,
groupedFieldSet: GroupedFieldSet,
initialResultRecord: InitialResultRecord,
newDeferMap: ReadonlyMap<DeferUsage, DeferredFragmentRecord>,
): PromiseOrValue<ObjMap<unknown>> {
const {
operation,
schema,
fragments,
variableValues,
rootValue,
incrementalPublisher,
} = exeContext;
const rootType = schema.getRootType(operation.operation);
if (rootType == null) {
throw new GraphQLError(
`Schema is not configured to execute ${operation.operation} operation.`,
{ nodes: operation },
);
}

const { fields, newDeferUsages } = collectFields(
schema,
fragments,
variableValues,
rootType,
operation,
);
const { groupedFieldSet, newGroupedFieldSetDetailsMap } =
buildFieldPlan(fields);

const newDeferMap = addNewDeferredFragments(
incrementalPublisher,
newDeferUsages,
initialResultRecord,
);

const path = undefined;

const newDeferredGroupedFieldSetRecords = addNewDeferredGroupedFieldSets(
incrementalPublisher,
newGroupedFieldSetDetailsMap,
newDeferMap,
path,
);

let result;
switch (operation.operation) {
switch (operation) {
case OperationTypeNode.QUERY:
result = executeFields(
return executeFields(
exeContext,
rootType,
rootValue,
path,
undefined,
groupedFieldSet,
initialResultRecord,
newDeferMap,
);
break;
case OperationTypeNode.MUTATION:
result = executeFieldsSerially(
return executeFieldsSerially(
exeContext,
rootType,
rootValue,
path,
undefined,
groupedFieldSet,
initialResultRecord,
newDeferMap,
);
break;
case OperationTypeNode.SUBSCRIPTION:
// TODO: deprecate `subscribe` and move all logic here
// Temporary solution until we finish merging execute and subscribe together
result = executeFields(
return executeFields(
exeContext,
rootType,
rootValue,
path,
undefined,
groupedFieldSet,
initialResultRecord,
newDeferMap,
);
}

executeDeferredGroupedFieldSets(
exeContext,
rootType,
rootValue,
path,
newDeferredGroupedFieldSetRecords,
newDeferMap,
);

return result;
}

/**
Expand Down Expand Up @@ -1725,7 +1737,7 @@ function mapSourceToResponse(
return mapAsyncIterable(
resultOrStream,
(payload: unknown) =>
executeImpl(
executeOperation(
buildPerEventExecutionContext(exeContext, payload),
// typecast to ExecutionResult, not possible to return
// ExperimentalIncrementalExecutionResults when
Expand Down

0 comments on commit 13f84cd

Please sign in to comment.