Skip to content

Commit bd7c360

Browse files
committed
add id and subPath to incremental results
1 parent 8a66084 commit bd7c360

File tree

4 files changed

+161
-111
lines changed

4 files changed

+161
-111
lines changed

src/execution/IncrementalPublisher.ts

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ export interface IncrementalDeferResult<
100100
> {
101101
errors?: ReadonlyArray<GraphQLError>;
102102
data: TData;
103-
path?: ReadonlyArray<string | number>;
103+
id?: string;
104+
subPath?: ReadonlyArray<string | number>;
104105
extensions?: TExtensions;
105106
}
106107

@@ -110,7 +111,8 @@ export interface FormattedIncrementalDeferResult<
110111
> {
111112
errors?: ReadonlyArray<GraphQLFormattedError>;
112113
data: TData;
113-
path?: ReadonlyArray<string | number>;
114+
id?: string;
115+
subPath?: ReadonlyArray<string | number>;
114116
extensions?: TExtensions;
115117
}
116118

@@ -120,7 +122,8 @@ export interface IncrementalStreamResult<
120122
> {
121123
errors?: ReadonlyArray<GraphQLError>;
122124
items: TData;
123-
path?: ReadonlyArray<string | number>;
125+
id?: string;
126+
subPath?: ReadonlyArray<string | number>;
124127
extensions?: TExtensions;
125128
}
126129

@@ -130,7 +133,8 @@ export interface FormattedIncrementalStreamResult<
130133
> {
131134
errors?: ReadonlyArray<GraphQLFormattedError>;
132135
items: TData;
133-
path?: ReadonlyArray<string | number>;
136+
id?: string;
137+
subPath?: ReadonlyArray<string | number>;
134138
extensions?: TExtensions;
135139
}
136140

@@ -562,7 +566,8 @@ export class IncrementalPublisher {
562566
}
563567
const incrementalResult: IncrementalStreamResult = {
564568
items: subsequentResultRecord.items,
565-
path: subsequentResultRecord.streamRecord.path,
569+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
570+
id: subsequentResultRecord.streamRecord.id!,
566571
};
567572
if (subsequentResultRecord.errors.length > 0) {
568573
incrementalResult.errors = subsequentResultRecord.errors;
@@ -579,11 +584,8 @@ export class IncrementalPublisher {
579584
for (const deferredGroupedFieldSetRecord of subsequentResultRecord.deferredGroupedFieldSetRecords) {
580585
if (!deferredGroupedFieldSetRecord.sent) {
581586
deferredGroupedFieldSetRecord.sent = true;
582-
const incrementalResult: IncrementalDeferResult = {
583-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
584-
data: deferredGroupedFieldSetRecord.data!,
585-
path: deferredGroupedFieldSetRecord.path,
586-
};
587+
const incrementalResult: IncrementalDeferResult =
588+
this._getIncrementalDeferResult(deferredGroupedFieldSetRecord);
587589
if (deferredGroupedFieldSetRecord.errors.length > 0) {
588590
incrementalResult.errors = deferredGroupedFieldSetRecord.errors;
589591
}
@@ -600,6 +602,40 @@ export class IncrementalPublisher {
600602
};
601603
}
602604

605+
private _getIncrementalDeferResult(
606+
deferredGroupedFieldSetRecord: DeferredGroupedFieldSetRecord,
607+
): IncrementalDeferResult {
608+
const { data, deferredFragmentRecords } = deferredGroupedFieldSetRecord;
609+
let maxLength = deferredFragmentRecords[0].path.length;
610+
let maxIndex = 0;
611+
for (let i = 1; i < deferredFragmentRecords.length; i++) {
612+
const deferredFragmentRecord = deferredFragmentRecords[i];
613+
const length = deferredFragmentRecord.path.length;
614+
if (length > maxLength) {
615+
maxLength = length;
616+
maxIndex = i;
617+
}
618+
}
619+
const recordWithLongestPath = deferredFragmentRecords[maxIndex];
620+
const longestPath = recordWithLongestPath.path;
621+
const subPath = deferredGroupedFieldSetRecord.path.slice(
622+
longestPath.length,
623+
);
624+
const id = recordWithLongestPath.id;
625+
const incrementalDeferResult: IncrementalDeferResult = {
626+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
627+
data: data!,
628+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
629+
id: id!,
630+
};
631+
632+
if (subPath.length > 0) {
633+
incrementalDeferResult.subPath = subPath;
634+
}
635+
636+
return incrementalDeferResult;
637+
}
638+
603639
private _completedRecordToResult(
604640
completedRecord: DeferredFragmentRecord | StreamRecord,
605641
): CompletedResult {

0 commit comments

Comments
 (0)