Skip to content

Commit

Permalink
add id and subPath to incremental results
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Aug 28, 2023
1 parent 8a66084 commit bd7c360
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 111 deletions.
56 changes: 46 additions & 10 deletions src/execution/IncrementalPublisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ export interface IncrementalDeferResult<
> {
errors?: ReadonlyArray<GraphQLError>;
data: TData;
path?: ReadonlyArray<string | number>;
id?: string;
subPath?: ReadonlyArray<string | number>;
extensions?: TExtensions;
}

Expand All @@ -110,7 +111,8 @@ export interface FormattedIncrementalDeferResult<
> {
errors?: ReadonlyArray<GraphQLFormattedError>;
data: TData;
path?: ReadonlyArray<string | number>;
id?: string;
subPath?: ReadonlyArray<string | number>;
extensions?: TExtensions;
}

Expand All @@ -120,7 +122,8 @@ export interface IncrementalStreamResult<
> {
errors?: ReadonlyArray<GraphQLError>;
items: TData;
path?: ReadonlyArray<string | number>;
id?: string;
subPath?: ReadonlyArray<string | number>;
extensions?: TExtensions;
}

Expand All @@ -130,7 +133,8 @@ export interface FormattedIncrementalStreamResult<
> {
errors?: ReadonlyArray<GraphQLFormattedError>;
items: TData;
path?: ReadonlyArray<string | number>;
id?: string;
subPath?: ReadonlyArray<string | number>;
extensions?: TExtensions;
}

Expand Down Expand Up @@ -562,7 +566,8 @@ export class IncrementalPublisher {
}
const incrementalResult: IncrementalStreamResult = {
items: subsequentResultRecord.items,
path: subsequentResultRecord.streamRecord.path,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
id: subsequentResultRecord.streamRecord.id!,
};
if (subsequentResultRecord.errors.length > 0) {
incrementalResult.errors = subsequentResultRecord.errors;
Expand All @@ -579,11 +584,8 @@ export class IncrementalPublisher {
for (const deferredGroupedFieldSetRecord of subsequentResultRecord.deferredGroupedFieldSetRecords) {
if (!deferredGroupedFieldSetRecord.sent) {
deferredGroupedFieldSetRecord.sent = true;
const incrementalResult: IncrementalDeferResult = {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
data: deferredGroupedFieldSetRecord.data!,
path: deferredGroupedFieldSetRecord.path,
};
const incrementalResult: IncrementalDeferResult =
this._getIncrementalDeferResult(deferredGroupedFieldSetRecord);
if (deferredGroupedFieldSetRecord.errors.length > 0) {
incrementalResult.errors = deferredGroupedFieldSetRecord.errors;
}
Expand All @@ -600,6 +602,40 @@ export class IncrementalPublisher {
};
}

private _getIncrementalDeferResult(
deferredGroupedFieldSetRecord: DeferredGroupedFieldSetRecord,
): IncrementalDeferResult {
const { data, deferredFragmentRecords } = deferredGroupedFieldSetRecord;
let maxLength = deferredFragmentRecords[0].path.length;
let maxIndex = 0;
for (let i = 1; i < deferredFragmentRecords.length; i++) {
const deferredFragmentRecord = deferredFragmentRecords[i];
const length = deferredFragmentRecord.path.length;
if (length > maxLength) {
maxLength = length;
maxIndex = i;
}
}
const recordWithLongestPath = deferredFragmentRecords[maxIndex];
const longestPath = recordWithLongestPath.path;
const subPath = deferredGroupedFieldSetRecord.path.slice(
longestPath.length,
);
const id = recordWithLongestPath.id;
const incrementalDeferResult: IncrementalDeferResult = {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
data: data!,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
id: id!,
};

if (subPath.length > 0) {
incrementalDeferResult.subPath = subPath;
}

return incrementalDeferResult;
}

private _completedRecordToResult(
completedRecord: DeferredFragmentRecord | StreamRecord,
): CompletedResult {
Expand Down
Loading

0 comments on commit bd7c360

Please sign in to comment.