Skip to content

Commit

Permalink
rename the things
Browse files Browse the repository at this point in the history
Future => IncrementalDataRecord
FutureResult => IncrementalDataRecordResult
RawDeferResult => BareDeferredGroupedFieldSetResult
RawStreamItemsResult => BareStreamItemsResult
  • Loading branch information
yaacovCR committed Mar 21, 2024
1 parent 5644ddf commit 4263701
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 83 deletions.
83 changes: 50 additions & 33 deletions src/execution/IncrementalPublisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@ export interface FormattedSubsequentIncrementalExecutionResult<
extensions?: TExtensions;
}

interface RawDeferResult<TData = ObjMap<unknown>> {
interface BareDeferredGroupedFieldSetResult<TData = ObjMap<unknown>> {
errors?: ReadonlyArray<GraphQLError>;
data: TData;
}

export interface IncrementalDeferResult<
TData = ObjMap<unknown>,
TExtensions = ObjMap<unknown>,
> extends RawDeferResult<TData> {
> extends BareDeferredGroupedFieldSetResult<TData> {
id: string;
subPath?: ReadonlyArray<string | number>;
extensions?: TExtensions;
Expand All @@ -118,15 +118,15 @@ export interface FormattedIncrementalDeferResult<
extensions?: TExtensions;
}

interface RawStreamItemsResult<TData = ReadonlyArray<unknown>> {
interface BareStreamItemsResult<TData = ReadonlyArray<unknown>> {
errors?: ReadonlyArray<GraphQLError>;
items: TData;
}

export interface IncrementalStreamResult<
TData = ReadonlyArray<unknown>,
TExtensions = ObjMap<unknown>,
> extends RawStreamItemsResult<TData> {
> extends BareStreamItemsResult<TData> {
id: string;
subPath?: ReadonlyArray<string | number>;
extensions?: TExtensions;
Expand Down Expand Up @@ -175,10 +175,14 @@ export function buildIncrementalResponse(
context: IncrementalPublisherContext,
result: ObjMap<unknown>,
errors: ReadonlyArray<GraphQLError> | undefined,
futures: ReadonlyArray<Future>,
incrementalDataRecords: ReadonlyArray<IncrementalDataRecord>,
): ExperimentalIncrementalExecutionResults {
const incrementalPublisher = new IncrementalPublisher(context);
return incrementalPublisher.buildResponse(result, errors, futures);
return incrementalPublisher.buildResponse(
result,
errors,
incrementalDataRecords,
);
}

interface IncrementalPublisherContext {
Expand All @@ -195,7 +199,7 @@ class IncrementalPublisher {
private _context: IncrementalPublisherContext;
private _nextId: number;
private _pending: Set<SubsequentResultRecord>;
private _completedResultQueue: Array<FutureResult>;
private _completedResultQueue: Array<IncrementalDataRecordResult>;
private _newPending: Set<SubsequentResultRecord>;
private _incremental: Array<IncrementalResult>;
private _completed: Array<CompletedResult>;
Expand All @@ -217,9 +221,9 @@ class IncrementalPublisher {
buildResponse(
data: ObjMap<unknown>,
errors: ReadonlyArray<GraphQLError> | undefined,
futures: ReadonlyArray<Future>,
incrementalDataRecords: ReadonlyArray<IncrementalDataRecord>,
): ExperimentalIncrementalExecutionResults {
this._addFutures(futures);
this._addIncrementalDataRecords(incrementalDataRecords);
this._pruneEmpty();

const pending = this._pendingSourcesToResults();
Expand All @@ -235,14 +239,16 @@ class IncrementalPublisher {
};
}

private _addFutures(futures: ReadonlyArray<Future>): void {
for (const future of futures) {
if (isDeferredGroupedFieldSetRecord(future)) {
for (const deferredFragmentRecord of future.deferredFragmentRecords) {
private _addIncrementalDataRecords(
incrementalDataRecords: ReadonlyArray<IncrementalDataRecord>,
): void {
for (const incrementalDataRecord of incrementalDataRecords) {
if (isDeferredGroupedFieldSetRecord(incrementalDataRecord)) {
for (const deferredFragmentRecord of incrementalDataRecord.deferredFragmentRecords) {
this._addDeferredFragmentRecord(deferredFragmentRecord);
}

const result = future.result;
const result = incrementalDataRecord.result;
if (isPromise(result)) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
result.then((resolved) => {
Expand All @@ -255,12 +261,12 @@ class IncrementalPublisher {
continue;
}

const streamRecord = future.streamRecord;
const streamRecord = incrementalDataRecord.streamRecord;
if (streamRecord.id === undefined) {
this._newPending.add(streamRecord);
}

const result = future.getResult();
const result = incrementalDataRecord.getResult();
if (isPromise(result)) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
result.then((resolved) => {
Expand Down Expand Up @@ -383,7 +389,7 @@ class IncrementalPublisher {
while (!isDone) {
let pending: Array<PendingResult> = [];

let completedResult: FutureResult | undefined;
let completedResult: IncrementalDataRecordResult | undefined;
while (
(completedResult = this._completedResultQueue.shift()) !== undefined
) {
Expand Down Expand Up @@ -514,8 +520,10 @@ class IncrementalPublisher {
);
}

if (deferredGroupedFieldSetResult.futures) {
this._addFutures(deferredGroupedFieldSetResult.futures);
if (deferredGroupedFieldSetResult.incrementalDataRecords) {
this._addIncrementalDataRecords(
deferredGroupedFieldSetResult.incrementalDataRecords,
);
}

for (const deferredFragmentRecord of deferredGroupedFieldSetResult.deferredFragmentRecords) {
Expand Down Expand Up @@ -608,8 +616,10 @@ class IncrementalPublisher {

this._incremental.push(incrementalEntry);

if (streamItemsResult.futures) {
this._addFutures(streamItemsResult.futures);
if (streamItemsResult.incrementalDataRecords) {
this._addIncrementalDataRecords(
streamItemsResult.incrementalDataRecords,
);
this._pruneEmpty();
}
}
Expand Down Expand Up @@ -655,16 +665,16 @@ export function isDeferredFragmentRecord(
}

export function isDeferredGroupedFieldSetRecord(
future: Future,
): future is DeferredGroupedFieldSetRecord {
return future instanceof DeferredGroupedFieldSetRecord;
incrementalDataRecord: IncrementalDataRecord,
): incrementalDataRecord is DeferredGroupedFieldSetRecord {
return incrementalDataRecord instanceof DeferredGroupedFieldSetRecord;
}

export interface IncrementalContext {
deferUsageSet: DeferUsageSet | undefined;
path: Path | undefined;
errors?: Map<Path | undefined, GraphQLError> | undefined;
futures?: Array<Future> | undefined;
incrementalDataRecords?: Array<IncrementalDataRecord> | undefined;
}

export type DeferredGroupedFieldSetResult =
Expand All @@ -680,8 +690,8 @@ export function isDeferredGroupedFieldSetResult(
interface ReconcilableDeferredGroupedFieldSetResult {
deferredFragmentRecords: ReadonlyArray<DeferredFragmentRecord>;
path: Array<string | number>;
result: RawDeferResult;
futures?: ReadonlyArray<Future> | undefined;
result: BareDeferredGroupedFieldSetResult;
incrementalDataRecords?: ReadonlyArray<IncrementalDataRecord> | undefined;
sent?: true | undefined;
}

Expand Down Expand Up @@ -785,14 +795,14 @@ interface NonReconcilableStreamItemsResult {

interface NonTerminatingStreamItemsResult {
streamRecord: StreamRecord;
result: RawStreamItemsResult;
futures?: ReadonlyArray<Future> | undefined;
result: BareStreamItemsResult;
incrementalDataRecords?: ReadonlyArray<IncrementalDataRecord> | undefined;
}

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

Expand Down Expand Up @@ -848,14 +858,21 @@ export class StreamItemsRecord {
this.nextStreamItems !== undefined
? {
...result,
futures: [this.nextStreamItems, ...(result.futures ?? [])],
incrementalDataRecords: [
this.nextStreamItems,
...(result.incrementalDataRecords ?? []),
],
}
: result;
}
}

export type Future = DeferredGroupedFieldSetRecord | StreamItemsRecord;
export type IncrementalDataRecord =
| DeferredGroupedFieldSetRecord
| StreamItemsRecord;

export type FutureResult = DeferredGroupedFieldSetResult | StreamItemsResult;
export type IncrementalDataRecordResult =
| DeferredGroupedFieldSetResult
| StreamItemsResult;

type SubsequentResultRecord = DeferredFragmentRecord | StreamRecord;
Loading

0 comments on commit 4263701

Please sign in to comment.