Skip to content

Commit 4891fe0

Browse files
committed
deduplicate all fields
1 parent b374774 commit 4891fe0

File tree

4 files changed

+280
-80
lines changed

4 files changed

+280
-80
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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { OperationTypeNode } from '../language/ast.js';
1414
import { Kind } from '../language/kinds.js';
1515

1616
import type { GraphQLObjectType } from '../type/definition.js';
17-
import { isAbstractType } from '../type/definition.js';
17+
import { isAbstractType, isLeafType } from '../type/definition.js';
1818
import {
1919
GraphQLDeferDirective,
2020
GraphQLIncludeDirective,
@@ -42,13 +42,15 @@ import { getDirectiveValues } from './values.js';
4242
export interface FieldGroup {
4343
depth: number;
4444
fields: Map<number | undefined, ReadonlyArray<FieldNode>>;
45+
isLeaf: boolean;
4546
}
4647

4748
export type GroupedFieldSet = Map<string, FieldGroup>;
4849

4950
interface MutableFieldGroup {
5051
depth: number;
5152
fields: AccumulatorMap<number | undefined, FieldNode>;
53+
isLeaf: boolean;
5254
}
5355

5456
/**
@@ -166,9 +168,12 @@ function collectFieldsImpl(
166168
const key = getFieldEntryKey(selection);
167169
let fieldGroup = groupedFieldSet.get(key);
168170
if (!fieldGroup) {
171+
const fieldDef = schema.getField(runtimeType, selection.name.value);
172+
const isLeaf = isLeafType(fieldDef?.type);
169173
fieldGroup = {
170174
depth,
171175
fields: new AccumulatorMap<number | undefined, FieldNode>(),
176+
isLeaf,
172177
};
173178
groupedFieldSet.set(key, fieldGroup);
174179
}

0 commit comments

Comments
 (0)