Skip to content

Commit

Permalink
refactor execution plan functions to match spec
Browse files Browse the repository at this point in the history
moves simplest case inside the execute[Root|Sub]ExecutionPlan() functions to better match proposed spec edits

orders function in call order, moving executeSubExecutionPlan down closer to where it is used
  • Loading branch information
yaacovCR committed Oct 31, 2024
1 parent d59c725 commit 6e39947
Showing 1 changed file with 158 additions and 114 deletions.
272 changes: 158 additions & 114 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ export function experimentalExecuteQueryOrMutationOrSubscriptionEvent(
);
}

const collectedFields = collectFields(
const { groupedFieldSet, newDeferUsages } = collectFields(
schema,
fragments,
variableValues,
Expand All @@ -342,24 +342,14 @@ export function experimentalExecuteQueryOrMutationOrSubscriptionEvent(
hideSuggestions,
);

const { groupedFieldSet, newDeferUsages } = collectedFields;
const graphqlWrappedResult =
newDeferUsages.length === 0
? executeRootGroupedFieldSet(
exeContext,
operation.operation,
rootType,
rootValue,
groupedFieldSet,
undefined,
)
: executeExecutionPlan(
exeContext,
rootType,
rootValue,
newDeferUsages,
buildExecutionPlan(groupedFieldSet),
);
const graphqlWrappedResult = executeRootExecutionPlan(
exeContext,
operation.operation,
rootType,
rootValue,
groupedFieldSet,
newDeferUsages,
);

if (isPromise(graphqlWrappedResult)) {
return graphqlWrappedResult.then(
Expand All @@ -376,78 +366,6 @@ export function experimentalExecuteQueryOrMutationOrSubscriptionEvent(
}
}

function executeExecutionPlan(
exeContext: ExecutionContext,
returnType: GraphQLObjectType,
sourceValue: unknown,
newDeferUsages: ReadonlyArray<DeferUsage>,
executionPlan: ExecutionPlan,
path?: Path | undefined,
incrementalContext?: IncrementalContext | undefined,
deferMap?: ReadonlyMap<DeferUsage, DeferredFragmentRecord> | undefined,
): PromiseOrValue<GraphQLWrappedResult<ObjMap<unknown>>> {
const newDeferMap = getNewDeferMap(newDeferUsages, deferMap, path);

const { groupedFieldSet, newGroupedFieldSets } = executionPlan;

const graphqlWrappedResult = executeFields(
exeContext,
returnType,
sourceValue,
path,
groupedFieldSet,
incrementalContext,
newDeferMap,
);

if (newGroupedFieldSets.size > 0) {
const newPendingExecutionGroups = collectExecutionGroups(
exeContext,
returnType,
sourceValue,
path,
incrementalContext?.deferUsageSet,
newGroupedFieldSets,
newDeferMap,
);

return withNewExecutionGroups(
graphqlWrappedResult,
newPendingExecutionGroups,
);
}
return graphqlWrappedResult;
}

function withNewExecutionGroups(
result: PromiseOrValue<GraphQLWrappedResult<ObjMap<unknown>>>,
newPendingExecutionGroups: ReadonlyArray<PendingExecutionGroup>,
): PromiseOrValue<GraphQLWrappedResult<ObjMap<unknown>>> {
if (isPromise(result)) {
return result.then((resolved) => {
addIncrementalDataRecords(resolved, newPendingExecutionGroups);
return resolved;
});
}

addIncrementalDataRecords(result, newPendingExecutionGroups);
return result;
}

function addIncrementalDataRecords(
graphqlWrappedResult: GraphQLWrappedResult<unknown>,
incrementalDataRecords: ReadonlyArray<IncrementalDataRecord> | undefined,
): void {
if (incrementalDataRecords === undefined) {
return;
}
if (graphqlWrappedResult.incrementalDataRecords === undefined) {
graphqlWrappedResult.incrementalDataRecords = [...incrementalDataRecords];
} else {
graphqlWrappedResult.incrementalDataRecords.push(...incrementalDataRecords);
}
}

function withError(
errors: Array<GraphQLError> | undefined,
error: GraphQLError,
Expand Down Expand Up @@ -603,6 +521,73 @@ export function validateExecutionArgs(
};
}

function executeRootExecutionPlan(
exeContext: ExecutionContext,
operation: OperationTypeNode,
rootType: GraphQLObjectType,
rootValue: unknown,
originalGroupedFieldSet: GroupedFieldSet,
newDeferUsages: ReadonlyArray<DeferUsage>,
): PromiseOrValue<GraphQLWrappedResult<ObjMap<unknown>>> {
if (newDeferUsages.length === 0) {
return executeRootGroupedFieldSet(
exeContext,
operation,
rootType,
rootValue,
originalGroupedFieldSet,
undefined,
);
}
const newDeferMap = getNewDeferMap(newDeferUsages, undefined, undefined);

const { groupedFieldSet, newGroupedFieldSets } = buildExecutionPlan(
originalGroupedFieldSet,
);

const graphqlWrappedResult = executeRootGroupedFieldSet(
exeContext,
operation,
rootType,
rootValue,
groupedFieldSet,
newDeferMap,
);

if (newGroupedFieldSets.size > 0) {
const newPendingExecutionGroups = collectExecutionGroups(
exeContext,
rootType,
rootValue,
undefined,
undefined,
newGroupedFieldSets,
newDeferMap,
);

return withNewExecutionGroups(
graphqlWrappedResult,
newPendingExecutionGroups,
);
}
return graphqlWrappedResult;
}

function withNewExecutionGroups(
result: PromiseOrValue<GraphQLWrappedResult<ObjMap<unknown>>>,
newPendingExecutionGroups: ReadonlyArray<PendingExecutionGroup>,
): PromiseOrValue<GraphQLWrappedResult<ObjMap<unknown>>> {
if (isPromise(result)) {
return result.then((resolved) => {
addIncrementalDataRecords(resolved, newPendingExecutionGroups);
return resolved;
});
}

addIncrementalDataRecords(result, newPendingExecutionGroups);
return result;
}

function executeRootGroupedFieldSet(
exeContext: ExecutionContext,
operation: OperationTypeNode,
Expand Down Expand Up @@ -714,6 +699,20 @@ function executeFieldsSerially(
);
}

function addIncrementalDataRecords(
graphqlWrappedResult: GraphQLWrappedResult<unknown>,
incrementalDataRecords: ReadonlyArray<IncrementalDataRecord> | undefined,
): void {
if (incrementalDataRecords === undefined) {
return;
}
if (graphqlWrappedResult.incrementalDataRecords === undefined) {
graphqlWrappedResult.incrementalDataRecords = [...incrementalDataRecords];
} else {
graphqlWrappedResult.incrementalDataRecords.push(...incrementalDataRecords);
}
}

/**
* Implements the "Executing selection sets" section of the spec
* for fields that may be executed in parallel.
Expand Down Expand Up @@ -1871,29 +1870,74 @@ function collectAndExecuteSubfields(
fieldDetailsList,
);
const { groupedFieldSet, newDeferUsages } = collectedSubfields;
return deferMap === undefined && newDeferUsages.length === 0
? executeFields(
exeContext,
returnType,
result,
path,
groupedFieldSet,
incrementalContext,
undefined,
)
: executeExecutionPlan(
exeContext,
returnType,
result,
newDeferUsages,
buildSubExecutionPlan(
groupedFieldSet,
incrementalContext?.deferUsageSet,
),
path,
incrementalContext,
deferMap,
);
return executeSubExecutionPlan(
exeContext,
returnType,
result,
groupedFieldSet,
newDeferUsages,
path,
incrementalContext,
deferMap,
);
}

function executeSubExecutionPlan(
exeContext: ExecutionContext,
returnType: GraphQLObjectType,
sourceValue: unknown,
originalGroupedFieldSet: GroupedFieldSet,
newDeferUsages: ReadonlyArray<DeferUsage>,
path?: Path | undefined,
incrementalContext?: IncrementalContext | undefined,
deferMap?: ReadonlyMap<DeferUsage, DeferredFragmentRecord> | undefined,
): PromiseOrValue<GraphQLWrappedResult<ObjMap<unknown>>> {
if (deferMap === undefined && newDeferUsages.length === 0) {
return executeFields(
exeContext,
returnType,
sourceValue,
path,
originalGroupedFieldSet,
incrementalContext,
deferMap,
);
}

const newDeferMap = getNewDeferMap(newDeferUsages, deferMap, path);

const { groupedFieldSet, newGroupedFieldSets } = buildSubExecutionPlan(
originalGroupedFieldSet,
incrementalContext?.deferUsageSet,
);

const graphqlWrappedResult = executeFields(
exeContext,
returnType,
sourceValue,
path,
groupedFieldSet,
incrementalContext,
newDeferMap,
);

if (newGroupedFieldSets.size > 0) {
const newPendingExecutionGroups = collectExecutionGroups(
exeContext,
returnType,
sourceValue,
path,
incrementalContext?.deferUsageSet,
newGroupedFieldSets,
newDeferMap,
);

return withNewExecutionGroups(
graphqlWrappedResult,
newPendingExecutionGroups,
);
}
return graphqlWrappedResult;
}

function buildSubExecutionPlan(
Expand Down

0 comments on commit 6e39947

Please sign in to comment.