Skip to content

Commit bfeffea

Browse files
committed
deduplicate all fields
1 parent 6ec9242 commit bfeffea

File tree

4 files changed

+297
-81
lines changed

4 files changed

+297
-81
lines changed

src/execution/__tests__/defer-test.ts

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ describe('Execute: defer directive', () => {
186186
}
187187
}
188188
fragment NameFragment on Hero {
189-
id
190189
name
191190
}
192191
`);
@@ -205,7 +204,6 @@ describe('Execute: defer directive', () => {
205204
incremental: [
206205
{
207206
data: {
208-
id: '1',
209207
name: 'Luke',
210208
},
211209
path: ['hero'],
@@ -410,9 +408,7 @@ describe('Execute: defer directive', () => {
410408
{
411409
incremental: [
412410
{
413-
data: {
414-
name: 'Luke',
415-
},
411+
data: {},
416412
path: ['hero'],
417413
},
418414
],
@@ -447,9 +443,7 @@ describe('Execute: defer directive', () => {
447443
{
448444
incremental: [
449445
{
450-
data: {
451-
name: 'Luke',
452-
},
446+
data: {},
453447
path: ['hero'],
454448
},
455449
],
@@ -527,7 +521,7 @@ describe('Execute: defer directive', () => {
527521
]);
528522
});
529523

530-
it('Does not deduplicate leaf fields present in the initial payload', async () => {
524+
it('Can deduplicate leaf fields present in the initial payload', async () => {
531525
const document = parse(`
532526
query {
533527
hero {
@@ -584,11 +578,6 @@ describe('Execute: defer directive', () => {
584578
bar: 'bar',
585579
},
586580
},
587-
anotherNestedObject: {
588-
deeperObject: {
589-
foo: 'foo',
590-
},
591-
},
592581
},
593582
path: ['hero'],
594583
},
@@ -598,7 +587,7 @@ describe('Execute: defer directive', () => {
598587
]);
599588
});
600589

601-
it('Does not deduplicate fields with deferred fragments at multiple levels', async () => {
590+
it('Can deduplicate fields with deferred fragments at multiple levels', async () => {
602591
const document = parse(`
603592
query {
604593
hero {
@@ -649,32 +638,18 @@ describe('Execute: defer directive', () => {
649638
incremental: [
650639
{
651640
data: {
652-
foo: 'foo',
653641
bar: 'bar',
654642
baz: 'baz',
655643
bak: 'bak',
656644
},
657645
path: ['hero', 'nestedObject', 'deeperObject'],
658646
},
659647
{
660-
data: {
661-
deeperObject: {
662-
foo: 'foo',
663-
bar: 'bar',
664-
baz: 'baz',
665-
},
666-
},
648+
data: {},
667649
path: ['hero', 'nestedObject'],
668650
},
669651
{
670-
data: {
671-
nestedObject: {
672-
deeperObject: {
673-
foo: 'foo',
674-
bar: 'bar',
675-
},
676-
},
677-
},
652+
data: {},
678653
path: ['hero'],
679654
},
680655
],
@@ -729,11 +704,7 @@ describe('Execute: defer directive', () => {
729704
path: ['hero', 'nestedObject', 'deeperObject'],
730705
},
731706
{
732-
data: {
733-
nestedObject: {
734-
deeperObject: {},
735-
},
736-
},
707+
data: {},
737708
path: ['hero'],
738709
},
739710
],
@@ -797,13 +768,6 @@ describe('Execute: defer directive', () => {
797768
},
798769
{
799770
data: {
800-
a: {
801-
b: {
802-
e: {
803-
f: 'f',
804-
},
805-
},
806-
},
807771
g: {
808772
h: 'h',
809773
},

src/execution/collectFields.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ import type {
1313
import { OperationTypeNode } from '../language/ast.js';
1414
import { Kind } from '../language/kinds.js';
1515

16-
import type { GraphQLObjectType } from '../type/definition.js';
17-
import { isAbstractType } from '../type/definition.js';
16+
import type {
17+
GraphQLObjectType,
18+
GraphQLOutputType,
19+
} from '../type/definition.js';
20+
import { GraphQLList, isAbstractType, isLeafType } from '../type/definition.js';
1821
import {
1922
GraphQLDeferDirective,
2023
GraphQLIncludeDirective,
@@ -42,13 +45,17 @@ import { getDirectiveValues } from './values.js';
4245
export interface FieldGroup {
4346
depth: number;
4447
fields: Map<number | undefined, ReadonlyArray<FieldNode>>;
48+
isLeaf: boolean;
49+
listDepth: number;
4550
}
4651

4752
export type GroupedFieldSet = Map<string, FieldGroup>;
4853

4954
interface MutableFieldGroup {
5055
depth: number;
5156
fields: AccumulatorMap<number | undefined, FieldNode>;
57+
isLeaf: boolean;
58+
listDepth: number;
5259
}
5360

5461
/**
@@ -166,9 +173,13 @@ function collectFieldsImpl(
166173
const key = getFieldEntryKey(selection);
167174
let fieldGroup = groupedFieldSet.get(key);
168175
if (!fieldGroup) {
176+
const fieldDef = schema.getField(runtimeType, selection.name.value);
177+
const isLeaf = isLeafType(fieldDef?.type);
169178
fieldGroup = {
170179
depth,
171180
fields: new AccumulatorMap<number | undefined, FieldNode>(),
181+
isLeaf,
182+
listDepth: getListDepth(fieldDef?.type),
172183
};
173184
groupedFieldSet.set(key, fieldGroup);
174185
}
@@ -327,3 +338,13 @@ function doesFragmentConditionMatch(
327338
function getFieldEntryKey(node: FieldNode): string {
328339
return node.alias ? node.alias.value : node.name.value;
329340
}
341+
342+
function getListDepth(type: GraphQLOutputType | undefined): number {
343+
let listDepth = 0;
344+
let unwrappedType = type;
345+
while (unwrappedType instanceof GraphQLList) {
346+
listDepth++;
347+
unwrappedType = unwrappedType.ofType;
348+
}
349+
return listDepth;
350+
}

0 commit comments

Comments
 (0)