Skip to content

Commit e081838

Browse files
authored
fix(incremental): fix logic around selecting id/subPath (#3987)
current loop assumes that the first analyzed DeferredFragmentRecord has been released as pending and thereofre has an `id`, which happens to be the case in this scenario, but is not reliable, uncovered by #3982. Demonstrates the peril of `!` which might point to requiring additional types, but we can leave that for a different PR. Another option is to include an `invariant` call -- that should be unnecessary, but would indeed be safer.
1 parent 2e29180 commit e081838

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/execution/IncrementalPublisher.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -609,29 +609,29 @@ export class IncrementalPublisher {
609609
deferredGroupedFieldSetRecord: DeferredGroupedFieldSetRecord,
610610
): IncrementalDeferResult {
611611
const { data, deferredFragmentRecords } = deferredGroupedFieldSetRecord;
612-
let maxLength = deferredFragmentRecords[0].path.length;
613-
let maxIndex = 0;
614-
for (let i = 1; i < deferredFragmentRecords.length; i++) {
615-
const deferredFragmentRecord = deferredFragmentRecords[i];
612+
let maxLength: number | undefined;
613+
let idWithLongestPath: string | undefined;
614+
for (const deferredFragmentRecord of deferredFragmentRecords) {
615+
const id = deferredFragmentRecord.id;
616+
if (id === undefined) {
617+
continue;
618+
}
616619
const length = deferredFragmentRecord.path.length;
617-
if (length > maxLength) {
620+
if (maxLength === undefined || length > maxLength) {
618621
maxLength = length;
619-
maxIndex = i;
622+
idWithLongestPath = id;
620623
}
621624
}
622-
const recordWithLongestPath = deferredFragmentRecords[maxIndex];
623-
const longestPath = recordWithLongestPath.path;
624-
const subPath = deferredGroupedFieldSetRecord.path.slice(
625-
longestPath.length,
626-
);
627-
const id = recordWithLongestPath.id;
625+
const subPath = deferredGroupedFieldSetRecord.path.slice(maxLength);
628626
const incrementalDeferResult: IncrementalDeferResult = {
629627
// safe because `data``is always defined when the record is completed
630628
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
631629
data: data!,
632-
// safe because `id` is defined once the fragment has been released as pending
630+
// safe because `id` is always defined once the fragment has been released
631+
// as pending and at least one fragment has been completed, so must have been
632+
// released as pending
633633
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
634-
id: id!,
634+
id: idWithLongestPath!,
635635
};
636636

637637
if (subPath.length > 0) {

0 commit comments

Comments
 (0)