Skip to content

Commit

Permalink
simplify FieldGroup
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Mar 20, 2024
1 parent 51f41eb commit 468972a
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 120 deletions.
8 changes: 6 additions & 2 deletions src/execution/IncrementalPublisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import type {
GraphQLFormattedError,
} from '../error/GraphQLError.js';

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

interface IncrementalUpdate<TData = unknown, TExtensions = ObjMap<unknown>> {
pending: ReadonlyArray<PendingResult>;
Expand Down Expand Up @@ -739,7 +740,7 @@ export class IncrementalPublisher {
}
}

function isDeferredGroupedFieldSetRecord(
export function isDeferredGroupedFieldSetRecord(
incrementalDataRecord: unknown,
): incrementalDataRecord is DeferredGroupedFieldSetRecord {
return incrementalDataRecord instanceof DeferredGroupedFieldSetRecord;
Expand All @@ -764,6 +765,7 @@ export class InitialResultRecord {
/** @internal */
export class DeferredGroupedFieldSetRecord {
path: ReadonlyArray<string | number>;
deferUsageSet: DeferUsageSet;
deferredFragmentRecords: ReadonlyArray<DeferredFragmentRecord>;
groupedFieldSet: GroupedFieldSet;
shouldInitiateDefer: boolean;
Expand All @@ -773,11 +775,13 @@ export class DeferredGroupedFieldSetRecord {

constructor(opts: {
path: Path | undefined;
deferUsageSet: DeferUsageSet;
deferredFragmentRecords: ReadonlyArray<DeferredFragmentRecord>;
groupedFieldSet: GroupedFieldSet;
shouldInitiateDefer: boolean;
}) {
this.path = pathToArray(opts.path);
this.deferUsageSet = opts.deferUsageSet;
this.deferredFragmentRecords = opts.deferredFragmentRecords;
this.groupedFieldSet = opts.groupedFieldSet;
this.shouldInitiateDefer = opts.shouldInitiateDefer;
Expand Down
84 changes: 22 additions & 62 deletions src/execution/buildFieldPlan.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,47 @@
import { getBySet } from '../jsutils/getBySet.js';
import { isSameSet } from '../jsutils/isSameSet.js';

import type { DeferUsage, FieldDetails } from './collectFields.js';
import type {
DeferUsage,
FieldGroup,
GroupedFieldSet,
} from './collectFields.js';

export type DeferUsageSet = ReadonlySet<DeferUsage>;

export interface FieldGroup {
fields: ReadonlyArray<FieldDetails>;
deferUsages?: DeferUsageSet | undefined;
}

export type GroupedFieldSet = Map<string, FieldGroup>;

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

export function buildFieldPlan(
fields: Map<string, ReadonlyArray<FieldDetails>>,
parentDeferUsages: DeferUsageSet = new Set<DeferUsage>(),
): {
export interface FieldPlan {
groupedFieldSet: GroupedFieldSet;
newGroupedFieldSetDetailsMap: Map<DeferUsageSet, NewGroupedFieldSetDetails>;
} {
const groupedFieldSet = new Map<
string,
{
fields: Array<FieldDetails>;
deferUsages: DeferUsageSet;
}
>();
}

export function buildFieldPlan(
originalGroupedFieldSet: GroupedFieldSet,
parentDeferUsages: DeferUsageSet = new Set<DeferUsage>(),
): FieldPlan {
const groupedFieldSet: GroupedFieldSet = new Map();

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

const map = new Map<
string,
{
deferUsageSet: DeferUsageSet;
fieldDetailsList: ReadonlyArray<FieldDetails>;
fieldGroup: FieldGroup;
}
>();

for (const [responseKey, fieldDetailsList] of fields) {
for (const [responseKey, fieldGroup] of originalGroupedFieldSet) {
const deferUsageSet = new Set<DeferUsage>();
let inOriginalResult = false;
for (const fieldDetails of fieldDetailsList) {
for (const fieldDetails of fieldGroup) {
const deferUsage = fieldDetails.deferUsage;
if (deferUsage === undefined) {
inOriginalResult = true;
Expand All @@ -77,20 +61,12 @@ export function buildFieldPlan(
}
});
}
map.set(responseKey, { deferUsageSet, fieldDetailsList });
map.set(responseKey, { deferUsageSet, fieldGroup });
}

for (const [responseKey, { deferUsageSet, fieldDetailsList }] of map) {
for (const [responseKey, { deferUsageSet, fieldGroup }] of map) {
if (isSameSet(deferUsageSet, parentDeferUsages)) {
let fieldGroup = groupedFieldSet.get(responseKey);
if (fieldGroup === undefined) {
fieldGroup = {
fields: [],
deferUsages: deferUsageSet,
};
groupedFieldSet.set(responseKey, fieldGroup);
}
fieldGroup.fields.push(...fieldDetailsList);
groupedFieldSet.set(responseKey, fieldGroup);
continue;
}

Expand All @@ -100,15 +76,7 @@ export function buildFieldPlan(
);
let newGroupedFieldSet;
if (newGroupedFieldSetDetails === undefined) {
newGroupedFieldSet = new Map<
string,
{
fields: Array<FieldDetails>;
deferUsages: DeferUsageSet;
knownDeferUsages: DeferUsageSet;
}
>();

newGroupedFieldSet = new Map();
newGroupedFieldSetDetails = {
groupedFieldSet: newGroupedFieldSet,
shouldInitiateDefer: Array.from(deferUsageSet).some(
Expand All @@ -122,15 +90,7 @@ export function buildFieldPlan(
} else {
newGroupedFieldSet = newGroupedFieldSetDetails.groupedFieldSet;
}
let fieldGroup = newGroupedFieldSet.get(responseKey);
if (fieldGroup === undefined) {
fieldGroup = {
fields: [],
deferUsages: deferUsageSet,
};
newGroupedFieldSet.set(responseKey, fieldGroup);
}
fieldGroup.fields.push(...fieldDetailsList);
newGroupedFieldSet.set(responseKey, fieldGroup);
}

return {
Expand Down
16 changes: 10 additions & 6 deletions src/execution/collectFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export interface FieldDetails {
deferUsage: DeferUsage | undefined;
}

export type FieldGroup = ReadonlyArray<FieldDetails>;

export type GroupedFieldSet = Map<string, FieldGroup>;

interface CollectFieldsContext {
schema: GraphQLSchema;
fragments: ObjMap<FragmentDefinitionNode>;
Expand All @@ -61,7 +65,7 @@ export function collectFields(
runtimeType: GraphQLObjectType,
operation: OperationDefinitionNode,
): {
fields: Map<string, ReadonlyArray<FieldDetails>>;
groupedFieldSet: GroupedFieldSet;
newDeferUsages: ReadonlyArray<DeferUsage>;
} {
const groupedFieldSet = new AccumulatorMap<string, FieldDetails>();
Expand All @@ -81,7 +85,7 @@ export function collectFields(
groupedFieldSet,
newDeferUsages,
);
return { fields: groupedFieldSet, newDeferUsages };
return { groupedFieldSet, newDeferUsages };
}

/**
Expand All @@ -101,9 +105,9 @@ export function collectSubfields(
variableValues: { [variable: string]: unknown },
operation: OperationDefinitionNode,
returnType: GraphQLObjectType,
fieldDetails: ReadonlyArray<FieldDetails>,
fieldGroup: FieldGroup,
): {
fields: Map<string, ReadonlyArray<FieldDetails>>;
groupedFieldSet: GroupedFieldSet;
newDeferUsages: ReadonlyArray<DeferUsage>;
} {
const context: CollectFieldsContext = {
Expand All @@ -117,7 +121,7 @@ export function collectSubfields(
const subGroupedFieldSet = new AccumulatorMap<string, FieldDetails>();
const newDeferUsages: Array<DeferUsage> = [];

for (const fieldDetail of fieldDetails) {
for (const fieldDetail of fieldGroup) {
const node = fieldDetail.node;
if (node.selectionSet) {
collectFieldsImpl(
Expand All @@ -131,7 +135,7 @@ export function collectSubfields(
}

return {
fields: subGroupedFieldSet,
groupedFieldSet: subGroupedFieldSet,
newDeferUsages,
};
}
Expand Down
Loading

0 comments on commit 468972a

Please sign in to comment.