Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 25 additions & 0 deletions src/execution/ResolveInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ export class ResolveInfo implements GraphQLResolveInfo {
private _fieldDetailsList: FieldDetailsList;
private _parentType: GraphQLObjectType;
private _path: Path;
private _abortSignal: AbortSignal | undefined;
private _registerAbortSignal: () => {
abortSignal: AbortSignal | undefined;
unregister?: () => void;
};
private _unregisterAbortSignal: (() => void) | undefined;

private _fieldName: string | undefined;
private _fieldNodes: ReadonlyArray<FieldNode> | undefined;
Expand All @@ -37,18 +43,24 @@ export class ResolveInfo implements GraphQLResolveInfo {
private _operation: OperationDefinitionNode | undefined;
private _variableValues: VariableValues | undefined;

// eslint-disable-next-line max-params
constructor(
validatedExecutionArgs: ValidatedExecutionArgs,
fieldDef: GraphQLField<unknown, unknown>,
fieldDetailsList: FieldDetailsList,
parentType: GraphQLObjectType,
path: Path,
registerAbortSignal: () => {
abortSignal: AbortSignal | undefined;
unregister?: () => void;
},
) {
this._validatedExecutionArgs = validatedExecutionArgs;
this._fieldDef = fieldDef;
this._fieldDetailsList = fieldDetailsList;
this._parentType = parentType;
this._path = path;
this._registerAbortSignal = registerAbortSignal;
}

get fieldName(): string {
Expand Down Expand Up @@ -103,4 +115,17 @@ export class ResolveInfo implements GraphQLResolveInfo {
this._variableValues ??= this._validatedExecutionArgs.variableValues;
return this._variableValues;
}

get abortSignal(): AbortSignal | undefined {
if (this._abortSignal !== undefined) {
return this._abortSignal;
}
const { abortSignal, unregister } = this._registerAbortSignal();
this._abortSignal = abortSignal;
this._unregisterAbortSignal = unregister;
return this._abortSignal;
}
unregisterAbortSignal(): void {
this._unregisterAbortSignal?.();
}
}
21 changes: 21 additions & 0 deletions src/execution/__tests__/ResolveInfo-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,22 @@ describe('ResolveInfo', () => {
assert(fieldDetailsList != null);

const path = { key: 'test', prev: undefined, typename: 'Query' };

const abortController = new AbortController();
const abortSignal = abortController.signal;
let unregisterCalled = false;
const resolveInfo = new ResolveInfo(
validatedExecutionArgs,
query.getFields().test,
fieldDetailsList,
query,
path,
() => ({
abortSignal,
unregister: () => {
unregisterCalled = true;
},
}),
);

it('exposes fieldName', () => {
Expand Down Expand Up @@ -99,4 +109,15 @@ describe('ResolveInfo', () => {
validatedExecutionArgs.variableValues,
);
});

it('exposes abortSignal', () => {
const retrievedAbortSignal = resolveInfo.abortSignal;
expect(retrievedAbortSignal).to.equal(abortSignal);
expect(retrievedAbortSignal).to.equal(resolveInfo.abortSignal); // ensure same reference
});

it('calls unregisterAbortSignal', () => {
resolveInfo.unregisterAbortSignal();
expect(unregisterCalled).to.equal(true);
});
});
113 changes: 113 additions & 0 deletions src/execution/__tests__/cancellablePromise-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import { expectPromise } from '../../__testUtils__/expectPromise.js';

import { cancellablePromise } from '../cancellablePromise.js';

describe('cancellablePromise', () => {
it('works to wrap a resolved promise', async () => {
const abortController = new AbortController();

const promise = Promise.resolve(1);

const withCancellation = cancellablePromise(
promise,
abortController.signal,
);

expect(await withCancellation).to.equal(1);
});

it('works to wrap a rejected promise', async () => {
const abortController = new AbortController();

const promise = Promise.reject(new Error('Rejected!'));

const withCancellation = cancellablePromise(
promise,
abortController.signal,
);

await expectPromise(withCancellation).toRejectWith('Rejected!');
});

it('works to cancel an already resolved promise', async () => {
const abortController = new AbortController();

const promise = Promise.resolve(1);

const withCancellation = cancellablePromise(
promise,
abortController.signal,
);

abortController.abort(new Error('Cancelled!'));

await expectPromise(withCancellation).toRejectWith('Cancelled!');
});

it('works to cancel an already resolved promise after abort signal triggered', async () => {
const abortController = new AbortController();

abortController.abort(new Error('Cancelled!'));

const promise = Promise.resolve(1);

const withCancellation = cancellablePromise(
promise,
abortController.signal,
);

await expectPromise(withCancellation).toRejectWith('Cancelled!');
});

it('works to cancel an already rejected promise after abort signal triggered', async () => {
const abortController = new AbortController();

abortController.abort(new Error('Cancelled!'));

const promise = Promise.reject(new Error('Rejected!'));

const withCancellation = cancellablePromise(
promise,
abortController.signal,
);

await expectPromise(withCancellation).toRejectWith('Cancelled!');
});

it('works to cancel a hanging promise', async () => {
const abortController = new AbortController();

const promise = new Promise(() => {
/* never resolves */
});

const withCancellation = cancellablePromise(
promise,
abortController.signal,
);

abortController.abort(new Error('Cancelled!'));

await expectPromise(withCancellation).toRejectWith('Cancelled!');
});

it('works to cancel a hanging promise created after abort signal triggered', async () => {
const abortController = new AbortController();

abortController.abort(new Error('Cancelled!'));

const promise = new Promise(() => {
/* never resolves */
});

const withCancellation = cancellablePromise(
promise,
abortController.signal,
);

await expectPromise(withCancellation).toRejectWith('Cancelled!');
});
});
Loading
Loading