Skip to content

Commit b520452

Browse files
committed
Simplify collectFields for @defer & @stream
1 parent 7a6d055 commit b520452

File tree

7 files changed

+338
-505
lines changed

7 files changed

+338
-505
lines changed

src/execution/IncrementalPublisher.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
GraphQLFormattedError,
99
} from '../error/GraphQLError.js';
1010

11-
import type { GroupedFieldSet } from './collectFields.js';
11+
import type { DeferUsage, GroupedFieldSet } from './collectFields.js';
1212

1313
interface IncrementalUpdate<TData = unknown, TExtensions = ObjMap<unknown>> {
1414
pending: ReadonlyArray<PendingResult>;
@@ -611,8 +611,11 @@ export class IncrementalPublisher {
611611
const { data, deferredFragmentRecords } = deferredGroupedFieldSetRecord;
612612
let maxLength = deferredFragmentRecords[0].path.length;
613613
let maxIndex = 0;
614-
for (let i = 1; i < deferredFragmentRecords.length; i++) {
614+
for (let i = 0; i < deferredFragmentRecords.length; i++) {
615615
const deferredFragmentRecord = deferredFragmentRecords[i];
616+
if (deferredFragmentRecord.id == null) {
617+
continue;
618+
}
616619
const length = deferredFragmentRecord.path.length;
617620
if (length > maxLength) {
618621
maxLength = length;
@@ -760,7 +763,6 @@ export class DeferredGroupedFieldSetRecord {
760763
path: ReadonlyArray<string | number>;
761764
deferredFragmentRecords: ReadonlyArray<DeferredFragmentRecord>;
762765
groupedFieldSet: GroupedFieldSet;
763-
shouldInitiateDefer: boolean;
764766
errors: Array<GraphQLError>;
765767
data: ObjMap<unknown> | undefined;
766768
sent: boolean;
@@ -769,12 +771,10 @@ export class DeferredGroupedFieldSetRecord {
769771
path: Path | undefined;
770772
deferredFragmentRecords: ReadonlyArray<DeferredFragmentRecord>;
771773
groupedFieldSet: GroupedFieldSet;
772-
shouldInitiateDefer: boolean;
773774
}) {
774775
this.path = pathToArray(opts.path);
775776
this.deferredFragmentRecords = opts.deferredFragmentRecords;
776777
this.groupedFieldSet = opts.groupedFieldSet;
777-
this.shouldInitiateDefer = opts.shouldInitiateDefer;
778778
this.errors = [];
779779
this.sent = false;
780780
}
@@ -783,6 +783,8 @@ export class DeferredGroupedFieldSetRecord {
783783
/** @internal */
784784
export class DeferredFragmentRecord {
785785
path: ReadonlyArray<string | number>;
786+
pathObj: Path | undefined;
787+
deferUsage: DeferUsage;
786788
label: string | undefined;
787789
id: string | undefined;
788790
children: Set<SubsequentResultRecord>;
@@ -792,9 +794,12 @@ export class DeferredFragmentRecord {
792794
pendingSent?: boolean;
793795
_pending: Set<DeferredGroupedFieldSetRecord>;
794796

795-
constructor(opts: { path: Path | undefined; label: string | undefined }) {
797+
constructor(opts: { path: Path | undefined; deferUsage: DeferUsage }) {
798+
this.pathObj = opts.path;
796799
this.path = pathToArray(opts.path);
797-
this.label = opts.label;
800+
this.deferUsage = opts.deferUsage;
801+
this.label = opts.deferUsage.label;
802+
this.deferUsage = opts.deferUsage;
798803
this.children = new Set();
799804
this.filtered = false;
800805
this.deferredGroupedFieldSetRecords = new Set();

src/execution/__tests__/defer-test.ts

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -481,19 +481,11 @@ describe('Execute: defer directive', () => {
481481
}
482482
`);
483483
const result = await complete(document);
484-
expectJSON(result).toDeepEqual([
485-
{
486-
data: {
487-
hero: {},
488-
},
489-
pending: [{ id: '0', path: ['hero'] }],
490-
hasNext: true,
491-
},
492-
{
493-
completed: [{ id: '0' }],
494-
hasNext: false,
484+
expectJSON(result).toDeepEqual({
485+
data: {
486+
hero: {},
495487
},
496-
]);
488+
});
497489
});
498490

499491
it('Can separately emit defer fragments with different labels with varying fields', async () => {
@@ -775,40 +767,18 @@ describe('Execute: defer directive', () => {
775767
data: { hero: { friends: [{}, {}, {}] } },
776768
pending: [
777769
{ id: '0', path: ['hero', 'friends', 0] },
778-
{ id: '1', path: ['hero', 'friends', 0] },
779-
{ id: '2', path: ['hero', 'friends', 0] },
780-
{ id: '3', path: ['hero', 'friends', 0] },
781-
{ id: '4', path: ['hero', 'friends', 1] },
782-
{ id: '5', path: ['hero', 'friends', 1] },
783-
{ id: '6', path: ['hero', 'friends', 1] },
784-
{ id: '7', path: ['hero', 'friends', 1] },
785-
{ id: '8', path: ['hero', 'friends', 2] },
786-
{ id: '9', path: ['hero', 'friends', 2] },
787-
{ id: '10', path: ['hero', 'friends', 2] },
788-
{ id: '11', path: ['hero', 'friends', 2] },
770+
{ id: '1', path: ['hero', 'friends', 1] },
771+
{ id: '2', path: ['hero', 'friends', 2] },
789772
],
790773
hasNext: true,
791774
},
792775
{
793776
incremental: [
794777
{ data: { id: '2', name: 'Han' }, id: '0' },
795-
{ data: { id: '3', name: 'Leia' }, id: '4' },
796-
{ data: { id: '4', name: 'C-3PO' }, id: '8' },
797-
],
798-
completed: [
799-
{ id: '1' },
800-
{ id: '2' },
801-
{ id: '3' },
802-
{ id: '5' },
803-
{ id: '6' },
804-
{ id: '7' },
805-
{ id: '9' },
806-
{ id: '10' },
807-
{ id: '11' },
808-
{ id: '0' },
809-
{ id: '4' },
810-
{ id: '8' },
778+
{ data: { id: '3', name: 'Leia' }, id: '1' },
779+
{ data: { id: '4', name: 'C-3PO' }, id: '2' },
811780
],
781+
completed: [{ id: '0' }, { id: '1' }, { id: '2' }],
812782
hasNext: false,
813783
},
814784
]);

0 commit comments

Comments
 (0)