Skip to content

Commit

Permalink
re-introduction of simple errorPath based filtering
Browse files Browse the repository at this point in the history
removes the need for entire GraphQLResult class and host of helper functions, presumably improving performance in both the non-incremental and incremental cases

this filtering can probably be further improved, as we do not have to check for errors in the path tree higher than the current incremental context's path, if we chose to save that information.
  • Loading branch information
yaacovCR committed Mar 11, 2024
1 parent d156b24 commit 5c12269
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 256 deletions.
27 changes: 11 additions & 16 deletions src/execution/IncrementalPublisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,13 @@ export interface FormattedCompletedResult {
}

export function buildIncrementalResponse(
result: GraphQLResult<ObjMap<unknown>>,
result: ObjMap<unknown>,
errors: ReadonlyArray<GraphQLError>,
futures: ReadonlyArray<Future>,
cancellableStreams: Set<StreamRecord>,
): ExperimentalIncrementalExecutionResults {
const incrementalPublisher = new IncrementalPublisher(cancellableStreams);
return incrementalPublisher.buildResponse(result, errors);
return incrementalPublisher.buildResponse(result, errors, futures);
}

/**
Expand Down Expand Up @@ -204,11 +205,10 @@ class IncrementalPublisher {
}

buildResponse(
result: GraphQLResult<ObjMap<unknown>>,
data: ObjMap<unknown>,
errors: ReadonlyArray<GraphQLError>,
futures: ReadonlyArray<Future>,
): ExperimentalIncrementalExecutionResults {
const { result: data, futures } = result;

this._addFutures(futures);
this._pruneEmpty();

Expand Down Expand Up @@ -636,20 +636,11 @@ export function isDeferredGroupedFieldSetRecord(
return future instanceof DeferredGroupedFieldSetRecord;
}

/** @internal **/
export class GraphQLResult<T> {
result: T;
futures: ReadonlyArray<Future> = [];

constructor(result: T, futures: ReadonlyArray<Future>) {
this.result = result;
this.futures = futures;
}
}

export interface IncrementalContext {
deferUsageSet: DeferUsageSet | undefined;
errors: Array<GraphQLError>;
errorPaths: Set<Path>;
futures: Array<Future>;
}

export interface DeferredGroupedFieldSetResult {
Expand Down Expand Up @@ -699,6 +690,8 @@ export class DeferredGroupedFieldSetRecord {
const incrementalContext: IncrementalContext = {
deferUsageSet,
errors: [],
errorPaths: new Set(),
futures: [],
};

for (const deferredFragmentRecord of this.deferredFragmentRecords) {
Expand Down Expand Up @@ -793,6 +786,8 @@ export class StreamItemsRecord {
const incrementalContext: IncrementalContext = {
deferUsageSet: undefined,
errors: [],
errorPaths: new Set(),
futures: [],
};

this._result = executor(incrementalContext);
Expand Down
10 changes: 5 additions & 5 deletions src/execution/__tests__/defer-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1116,23 +1116,23 @@ describe('Execute: defer directive', () => {
},
},
pending: [
{ id: '0', path: [] },
{ id: '1', path: ['a', 'b'] },
{ id: '0', path: ['a', 'b'] },
{ id: '1', path: [] },
],
hasNext: true,
},
{
incremental: [
{
data: { e: { f: 'f' } },
id: '1',
id: '0',
},
{
data: { g: { h: 'h' } },
id: '0',
id: '1',
},
],
completed: [{ id: '1' }, { id: '0' }],
completed: [{ id: '0' }, { id: '1' }],
hasNext: false,
},
]);
Expand Down
13 changes: 11 additions & 2 deletions src/execution/__tests__/stream-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2096,14 +2096,23 @@ describe('Execute: stream directive', () => {
id: '2',
},
],
completed: [{ id: '2' }, { id: '1' }],
hasNext: false,
completed: [{ id: '2' }],
hasNext: true,
},
done: false,
});

const result5 = await iterator.next();
expectJSON(result5).toDeepEqual({
value: {
completed: [{ id: '1' }],
hasNext: false,
},
done: false,
});

const result6 = await iterator.next();
expectJSON(result6).toDeepEqual({
value: undefined,
done: true,
});
Expand Down
Loading

0 comments on commit 5c12269

Please sign in to comment.