Skip to content

Commit

Permalink
fix: handle default and different types passed to abort method (#4258)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR authored Oct 27, 2024
1 parent 83da23a commit 9dae904
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 15 deletions.
121 changes: 109 additions & 12 deletions src/execution/__tests__/abort-signal-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,104 @@ describe('Execute: Cancellation', () => {
},
});

abortController.abort('Aborted');
abortController.abort();

const result = await resultPromise;

expect(result.errors?.[0].originalError?.name).to.equal('AbortError');

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

it('should stop the execution when aborted during object field completion with a custom error', async () => {
const abortController = new AbortController();
const document = parse(`
query {
todo {
id
author {
id
}
}
}
`);

const resultPromise = execute({
document,
schema,
abortSignal: abortController.signal,
rootValue: {
todo: async () =>
Promise.resolve({
id: '1',
text: 'Hello, World!',
/* c8 ignore next */
author: () => expect.fail('Should not be called'),
}),
},
});

const customError = new Error('Custom abort error');
abortController.abort(customError);

const result = await resultPromise;

expect(result.errors?.[0].originalError).to.equal(customError);

expectJSON(result).toDeepEqual({
data: {
todo: null,
},
errors: [
{
message: 'Custom abort error',
path: ['todo'],
locations: [{ line: 3, column: 9 }],
},
],
});
});

it('should stop the execution when aborted during object field completion with a custom string', async () => {
const abortController = new AbortController();
const document = parse(`
query {
todo {
id
author {
id
}
}
}
`);

const resultPromise = execute({
document,
schema,
abortSignal: abortController.signal,
rootValue: {
todo: async () =>
Promise.resolve({
id: '1',
text: 'Hello, World!',
/* c8 ignore next */
author: () => expect.fail('Should not be called'),
}),
},
});

abortController.abort('Custom abort error message');

const result = await resultPromise;

Expand All @@ -99,7 +196,7 @@ describe('Execute: Cancellation', () => {
},
errors: [
{
message: 'Aborted',
message: 'Unexpected error value: "Custom abort error message"',
path: ['todo'],
locations: [{ line: 3, column: 9 }],
},
Expand Down Expand Up @@ -135,7 +232,7 @@ describe('Execute: Cancellation', () => {
},
});

abortController.abort('Aborted');
abortController.abort();

const result = await resultPromise;

Expand All @@ -148,7 +245,7 @@ describe('Execute: Cancellation', () => {
},
errors: [
{
message: 'Aborted',
message: 'This operation was aborted',
path: ['todo', 'author'],
locations: [{ line: 5, column: 11 }],
},
Expand Down Expand Up @@ -187,7 +284,7 @@ describe('Execute: Cancellation', () => {
abortSignal: abortController.signal,
});

abortController.abort('Aborted');
abortController.abort();

const result = await resultPromise;

Expand All @@ -197,7 +294,7 @@ describe('Execute: Cancellation', () => {
},
errors: [
{
message: 'Aborted',
message: 'This operation was aborted',
path: ['todo'],
locations: [{ line: 3, column: 9 }],
},
Expand Down Expand Up @@ -242,7 +339,7 @@ describe('Execute: Cancellation', () => {
await resolveOnNextTick();
await resolveOnNextTick();

abortController.abort('Aborted');
abortController.abort();

const result = await resultPromise;

Expand Down Expand Up @@ -271,7 +368,7 @@ describe('Execute: Cancellation', () => {
line: 7,
},
],
message: 'Aborted',
message: 'This operation was aborted',
path: ['todo', 'author'],
},
],
Expand Down Expand Up @@ -304,7 +401,7 @@ describe('Execute: Cancellation', () => {
},
});

abortController.abort('Aborted');
abortController.abort();

const result = await resultPromise;

Expand All @@ -315,7 +412,7 @@ describe('Execute: Cancellation', () => {
},
errors: [
{
message: 'Aborted',
message: 'This operation was aborted',
path: ['bar'],
locations: [{ line: 4, column: 9 }],
},
Expand All @@ -335,7 +432,7 @@ describe('Execute: Cancellation', () => {
}
}
`);
abortController.abort('Aborted');
abortController.abort();
const result = await execute({
document,
schema,
Expand All @@ -349,7 +446,7 @@ describe('Execute: Cancellation', () => {
expectJSON(result).toDeepEqual({
errors: [
{
message: 'Aborted',
message: 'This operation was aborted',
},
],
});
Expand Down
6 changes: 3 additions & 3 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ export function validateExecutionArgs(
} = args;

if (abortSignal?.aborted) {
return [locatedError(new Error(abortSignal.reason), undefined)];
return [locatedError(abortSignal.reason, undefined)];
}

// If the schema used for execution is invalid, throw an error.
Expand Down Expand Up @@ -668,7 +668,7 @@ function executeFieldsSerially(
const abortSignal = exeContext.validatedExecutionArgs.abortSignal;
if (abortSignal?.aborted) {
handleFieldError(
new Error(abortSignal.reason),
abortSignal.reason,
exeContext,
parentType,
fieldDetailsList,
Expand Down Expand Up @@ -1732,7 +1732,7 @@ function completeObjectValue(
const abortSignal = validatedExecutionArgs.abortSignal;
if (abortSignal?.aborted) {
throw locatedError(
new Error(abortSignal.reason),
abortSignal.reason,
toNodes(fieldDetailsList),
pathToArray(path),
);
Expand Down

0 comments on commit 9dae904

Please sign in to comment.