Skip to content
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

simplify CollectFields for @defer and @stream #3994

Merged
merged 4 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/execution/IncrementalPublisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
GraphQLFormattedError,
} from '../error/GraphQLError.js';

import type { GroupedFieldSet } from './collectFields.js';
import type { GroupedFieldSet } from './buildFieldPlan.js';

interface IncrementalUpdate<TData = unknown, TExtensions = ObjMap<unknown>> {
pending: ReadonlyArray<PendingResult>;
Expand Down
152 changes: 152 additions & 0 deletions src/execution/buildFieldPlan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import { getBySet } from '../jsutils/getBySet.js';
import { isSameSet } from '../jsutils/isSameSet.js';

import type { FieldDetails } from './collectFields.js';

export interface DeferUsage {
yaacovCR marked this conversation as resolved.
Show resolved Hide resolved
label: string | undefined;
ancestors: ReadonlyArray<Target>;
}

export const NON_DEFERRED_TARGET_SET: TargetSet = new Set<Target>([undefined]);

export type Target = DeferUsage | undefined;
export type TargetSet = ReadonlySet<Target>;
export type DeferUsageSet = ReadonlySet<DeferUsage>;

export interface FieldGroup {
fields: ReadonlyArray<FieldDetails>;
targets?: TargetSet | undefined;
knownTargets?: TargetSet | undefined;
}

export type GroupedFieldSet = Map<string, FieldGroup>;

export interface NewGroupedFieldSetDetails {
groupedFieldSet: GroupedFieldSet;
shouldInitiateDefer: boolean;
}

export function buildFieldPlan(
fields: Map<string, ReadonlyArray<FieldDetails>>,
parentTargets = NON_DEFERRED_TARGET_SET,
knownTargets = NON_DEFERRED_TARGET_SET,
): {
groupedFieldSet: GroupedFieldSet;
newGroupedFieldSetDetailsMap: Map<DeferUsageSet, NewGroupedFieldSetDetails>;
newDeferUsages: ReadonlyArray<DeferUsage>;
} {
const newDeferUsages: Set<DeferUsage> = new Set<DeferUsage>();
const newKnownTargets = new Set<Target>(knownTargets);

const groupedFieldSet = new Map<
string,
{ fields: Array<FieldDetails>; targets: TargetSet; knownTargets: TargetSet }
>();

const newGroupedFieldSetDetailsMap = new Map<
DeferUsageSet,
{
groupedFieldSet: Map<
string,
{
fields: Array<FieldDetails>;
targets: TargetSet;
knownTargets: TargetSet;
}
>;
shouldInitiateDefer: boolean;
}
>();

const map = new Map<
string,
{ targetSet: TargetSet; fieldDetailsList: ReadonlyArray<FieldDetails> }
>();
for (const [responseKey, fieldDetailsList] of fields) {
const targetSet = new Set<Target>();
for (const fieldDetails of fieldDetailsList) {
const target = fieldDetails.deferUsage;
targetSet.add(target);
if (!knownTargets.has(target)) {
// all targets that are not known must be defined
newDeferUsages.add(target as DeferUsage);
}
newKnownTargets.add(target);
}
map.set(responseKey, { targetSet, fieldDetailsList });
}

for (const [responseKey, { targetSet, fieldDetailsList }] of map) {
const maskingTargetList: Array<Target> = [];
for (const target of targetSet) {
if (
target === undefined ||
target.ancestors.every((ancestor) => !targetSet.has(ancestor))
) {
maskingTargetList.push(target);
}
}

const maskingTargets: TargetSet = new Set<Target>(maskingTargetList);
if (isSameSet(maskingTargets, parentTargets)) {
let fieldGroup = groupedFieldSet.get(responseKey);
if (fieldGroup === undefined) {
fieldGroup = {
fields: [],
targets: maskingTargets,
knownTargets: newKnownTargets,
};
groupedFieldSet.set(responseKey, fieldGroup);
}
fieldGroup.fields.push(...fieldDetailsList);
continue;
}

let newGroupedFieldSetDetails = getBySet(
newGroupedFieldSetDetailsMap,
maskingTargets,
);
let newGroupedFieldSet;
if (newGroupedFieldSetDetails === undefined) {
newGroupedFieldSet = new Map<
string,
{
fields: Array<FieldDetails>;
targets: TargetSet;
knownTargets: TargetSet;
}
>();

newGroupedFieldSetDetails = {
groupedFieldSet: newGroupedFieldSet,
shouldInitiateDefer: maskingTargetList.some(
(deferUsage) => !parentTargets.has(deferUsage),
),
};
newGroupedFieldSetDetailsMap.set(
// all new grouped field sets must not contain the initial result as a target
maskingTargets as DeferUsageSet,
newGroupedFieldSetDetails,
);
} else {
newGroupedFieldSet = newGroupedFieldSetDetails.groupedFieldSet;
}
let fieldGroup = newGroupedFieldSet.get(responseKey);
if (fieldGroup === undefined) {
fieldGroup = {
fields: [],
targets: maskingTargets,
knownTargets: newKnownTargets,
};
newGroupedFieldSet.set(responseKey, fieldGroup);
}
fieldGroup.fields.push(...fieldDetailsList);
}

return {
groupedFieldSet,
newGroupedFieldSetDetailsMap,
newDeferUsages: Array.from(newDeferUsages),
};
}
Loading
Loading