Skip to content

draft: add support for fine grained aborting promises on error #3952

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

Closed
wants to merge 5 commits into from
Closed
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
2 changes: 1 addition & 1 deletion integrationTests/ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
"typescript-4.7": "npm:typescript@4.7.x",
"typescript-4.8": "npm:typescript@4.8.x",
"typescript-4.9": "npm:typescript@4.9.x",
"typescript-4.9": "npm:typescript@5.0.x"
"typescript-5.0": "npm:typescript@5.0.x"
}
}
8 changes: 7 additions & 1 deletion integrationTests/ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es2019", "es2020.promise", "es2020.bigint", "es2020.string"],
"lib": [
"es2019",
"es2020.promise",
"es2020.bigint",
"es2020.string",
"dom" // Workaround for missing web-compatible globals in `@types/node`
],
"noEmit": true,
"types": [],
"strict": true,
Expand Down
293 changes: 293 additions & 0 deletions src/execution/__tests__/executor-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,299 @@ describe('Execute: Handles basic execution tasks', () => {
expect(isAsyncResolverFinished).to.equal(true);
});

it('exits early on early abort', () => {
let isExecuted = false;

const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
field: {
type: GraphQLString,
/* c8 ignore next 3 */
resolve() {
isExecuted = true;
},
},
},
}),
});

const document = parse(`
{
field
}
`);

const abortController = new AbortController();
abortController.abort();

const result = execute({
schema,
document,
abortSignal: abortController.signal,
});

expect(isExecuted).to.equal(false);
expectJSON(result).toDeepEqual({
data: { field: null },
errors: [
{
message: 'This operation was aborted',
locations: [{ line: 3, column: 9 }],
path: ['field'],
},
],
});
});

it('exits early on abort mid-execution', async () => {
let isExecuted = false;

const asyncObjectType = new GraphQLObjectType({
name: 'AsyncObject',
fields: {
field: {
type: GraphQLString,
/* c8 ignore next 3 */
resolve() {
isExecuted = true;
},
},
},
});

const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
asyncObject: {
type: asyncObjectType,
async resolve() {
await resolveOnNextTick();
return {};
},
},
},
}),
});

const document = parse(`
{
asyncObject {
field
}
}
`);

const abortController = new AbortController();

const result = execute({
schema,
document,
abortSignal: abortController.signal,
});

abortController.abort();

expect(isExecuted).to.equal(false);
expectJSON(await result).toDeepEqual({
data: { asyncObject: { field: null } },
errors: [
{
message: 'This operation was aborted',
locations: [{ line: 4, column: 11 }],
path: ['asyncObject', 'field'],
},
],
});
expect(isExecuted).to.equal(false);
});

it('exits early on abort mid-resolver', async () => {
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
asyncField: {
type: GraphQLString,
async resolve(_parent, _args, _context, _info, abortSignal) {
await resolveOnNextTick();
abortSignal?.throwIfAborted();
},
},
},
}),
});

const document = parse(`
{
asyncField
}
`);

const abortController = new AbortController();

const result = execute({
schema,
document,
abortSignal: abortController.signal,
});

abortController.abort();

expectJSON(await result).toDeepEqual({
data: { asyncField: null },
errors: [
{
message: 'This operation was aborted',
locations: [{ line: 3, column: 9 }],
path: ['asyncField'],
},
],
});
});

it('exits early on abort mid-nested resolver', async () => {
const syncObjectType = new GraphQLObjectType({
name: 'SyncObject',
fields: {
asyncField: {
type: GraphQLString,
async resolve(_parent, _args, _context, _info, abortSignal) {
await resolveOnNextTick();
abortSignal?.throwIfAborted();
},
},
},
});

const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
syncObject: {
type: syncObjectType,
resolve() {
return {};
},
},
},
}),
});

const document = parse(`
{
syncObject {
asyncField
}
}
`);

const abortController = new AbortController();

const result = execute({
schema,
document,
abortSignal: abortController.signal,
});

abortController.abort();

expectJSON(await result).toDeepEqual({
data: { syncObject: { asyncField: null } },
errors: [
{
message: 'This operation was aborted',
locations: [{ line: 4, column: 11 }],
path: ['syncObject', 'asyncField'],
},
],
});
});

it('exits early on error', async () => {
const objectType = new GraphQLObjectType({
name: 'Object',
fields: {
nonNullNestedAsyncField: {
type: new GraphQLNonNull(GraphQLString),
async resolve() {
await resolveOnNextTick();
throw new Error('Oops');
},
},
nestedAsyncField: {
type: GraphQLString,
async resolve(_parent, _args, _context, _info, abortSignal) {
await resolveOnNextTick();
abortSignal?.throwIfAborted();
},
},
},
});

const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
object: {
type: objectType,
resolve() {
return {};
},
},
asyncField: {
type: GraphQLString,
async resolve() {
await resolveOnNextTick();
return 'asyncValue';
},
},
},
}),
});

const document = parse(`
{
object {
nonNullNestedAsyncField
nestedAsyncField
}
asyncField
}
`);

const abortController = new AbortController();

const result = execute({
schema,
document,
abortSignal: abortController.signal,
});

abortController.abort();

expectJSON(await result).toDeepEqual({
data: {
object: null,
asyncField: 'asyncValue',
},
errors: [
{
message: 'This operation was aborted',
locations: [{ line: 5, column: 11 }],
path: ['object', 'nestedAsyncField'],
},
{
message: 'Oops',
locations: [{ line: 4, column: 11 }],
path: ['object', 'nonNullNestedAsyncField'],
},
],
});
});

it('Full response path is included for non-nullable fields', () => {
const A: GraphQLObjectType = new GraphQLObjectType({
name: 'A',
Expand Down
Loading