Skip to content

incremental: introduce IncrementalGraph class to manage tree of subsequent results #4094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor(incremental): introduce StreamRecord type
  • Loading branch information
yaacovCR committed Jun 3, 2024
commit f5b1c33ec5d2a46f84ce8be814ad5768bc717eb3
7 changes: 4 additions & 3 deletions src/execution/IncrementalGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
IncrementalDataRecord,
IncrementalDataRecordResult,
ReconcilableDeferredGroupedFieldSetResult,
StreamRecord,
SubsequentResultRecord,
} from './types.js';
import { isDeferredGroupedFieldSetRecord } from './types.js';
Expand All @@ -27,11 +28,11 @@ function isDeferredFragmentNode(

function isStreamNode(
subsequentResultNode: SubsequentResultNode,
): subsequentResultNode is SubsequentResultRecord {
): subsequentResultNode is StreamRecord {
return 'path' in subsequentResultNode;
}

type SubsequentResultNode = DeferredFragmentNode | SubsequentResultRecord;
type SubsequentResultNode = DeferredFragmentNode | StreamRecord;

/**
* @internal
Expand Down Expand Up @@ -215,7 +216,7 @@ export class IncrementalGraph {
}
}

removeStream(streamRecord: SubsequentResultRecord): void {
removeStream(streamRecord: StreamRecord): void {
this._removePending(streamRecord);
}

Expand Down
18 changes: 9 additions & 9 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ import type {
IncrementalDataRecord,
StreamItemsRecord,
StreamItemsResult,
SubsequentResultRecord,
StreamRecord,
} from './types.js';
import { isReconcilableStreamItemsResult } from './types.js';
import {
Expand Down Expand Up @@ -1094,12 +1094,12 @@ async function completeAsyncIteratorValue(
while (true) {
if (streamUsage && index >= streamUsage.initialCount) {
const returnFn = asyncIterator.return;
let streamRecord: SubsequentResultRecord | CancellableStreamRecord;
let streamRecord: StreamRecord | CancellableStreamRecord;
if (returnFn === undefined) {
streamRecord = {
label: streamUsage.label,
path,
} as SubsequentResultRecord;
} as StreamRecord;
} else {
streamRecord = {
label: streamUsage.label,
Expand Down Expand Up @@ -1266,7 +1266,7 @@ function completeIterableValue(
const item = iteration.value;

if (streamUsage && index >= streamUsage.initialCount) {
const streamRecord: SubsequentResultRecord = {
const streamRecord: StreamRecord = {
label: streamUsage.label,
path,
};
Expand Down Expand Up @@ -2212,7 +2212,7 @@ function getDeferredFragmentRecords(
}

function firstSyncStreamItems(
streamRecord: SubsequentResultRecord,
streamRecord: StreamRecord,
initialItem: PromiseOrValue<unknown>,
initialIndex: number,
iterator: Iterator<unknown>,
Expand Down Expand Up @@ -2315,7 +2315,7 @@ function prependNextResolvedStreamItems(
}

function firstAsyncStreamItems(
streamRecord: SubsequentResultRecord,
streamRecord: StreamRecord,
path: Path,
initialIndex: number,
asyncIterator: AsyncIterator<unknown>,
Expand All @@ -2341,7 +2341,7 @@ function firstAsyncStreamItems(
}

async function getNextAsyncStreamItemsResult(
streamRecord: SubsequentResultRecord,
streamRecord: StreamRecord,
path: Path,
index: number,
asyncIterator: AsyncIterator<unknown>,
Expand Down Expand Up @@ -2395,7 +2395,7 @@ async function getNextAsyncStreamItemsResult(
}

function completeStreamItems(
streamRecord: SubsequentResultRecord,
streamRecord: StreamRecord,
itemPath: Path,
item: unknown,
exeContext: ExecutionContext,
Expand Down Expand Up @@ -2495,7 +2495,7 @@ function completeStreamItems(

function buildStreamItemsResult(
errors: ReadonlyArray<GraphQLError> | undefined,
streamRecord: SubsequentResultRecord,
streamRecord: StreamRecord,
result: GraphQLWrappedResult<unknown>,
): StreamItemsResult {
return {
Expand Down
20 changes: 11 additions & 9 deletions src/execution/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,20 +209,22 @@ export interface DeferredGroupedFieldSetRecord {
result: PromiseOrValue<DeferredGroupedFieldSetResult>;
}

export interface SubsequentResultRecord {
export type SubsequentResultRecord = DeferredFragmentRecord | StreamRecord;

export interface DeferredFragmentRecord {
path: Path | undefined;
label: string | undefined;
id?: string | undefined;
parent: DeferredFragmentRecord | undefined;
}

export interface DeferredFragmentRecord extends SubsequentResultRecord {
path: Path | undefined;
export interface StreamRecord {
path: Path;
label: string | undefined;
id?: string | undefined;
parent: DeferredFragmentRecord | undefined;
}

export interface CancellableStreamRecord extends SubsequentResultRecord {
export interface CancellableStreamRecord extends StreamRecord {
earlyReturn: () => Promise<unknown>;
}

Expand All @@ -233,7 +235,7 @@ export function isCancellableStreamRecord(
}

interface ReconcilableStreamItemsResult {
streamRecord: SubsequentResultRecord;
streamRecord: StreamRecord;
result: BareStreamItemsResult;
incrementalDataRecords: ReadonlyArray<IncrementalDataRecord> | undefined;
errors?: never;
Expand All @@ -246,14 +248,14 @@ export function isReconcilableStreamItemsResult(
}

interface TerminatingStreamItemsResult {
streamRecord: SubsequentResultRecord;
streamRecord: StreamRecord;
result?: never;
incrementalDataRecords?: never;
errors?: never;
}

interface NonReconcilableStreamItemsResult {
streamRecord: SubsequentResultRecord;
streamRecord: StreamRecord;
errors: ReadonlyArray<GraphQLError>;
result?: never;
}
Expand All @@ -264,7 +266,7 @@ export type StreamItemsResult =
| NonReconcilableStreamItemsResult;

export interface StreamItemsRecord {
streamRecord: SubsequentResultRecord;
streamRecord: StreamRecord;
result: PromiseOrValue<StreamItemsResult>;
}

Expand Down