Skip to content

Commit

Permalink
fix: handle default and different types passed to abort method
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Oct 27, 2024
1 parent cf1c080 commit c345e40
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 15 deletions.
116 changes: 104 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,7 @@ describe('Execute: Cancellation', () => {
},
});

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

const result = await resultPromise;

Expand All @@ -99,7 +99,99 @@ describe('Execute: Cancellation', () => {
},
errors: [
{
message: 'Aborted',
message: 'This operation was aborted',
path: ['todo', 'id'],
locations: [{ line: 4, column: 11 }],
},
],
});
});

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'),
}),
},
});

abortController.abort(new Error('Custom abort error'));

const result = await resultPromise;

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

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;

expectJSON(result).toDeepEqual({
data: {
todo: null,
},
errors: [
{
message: 'Unexpected error value: "Custom abort error message"',
path: ['todo', 'id'],
locations: [{ line: 4, column: 11 }],
},
Expand Down Expand Up @@ -135,7 +227,7 @@ describe('Execute: Cancellation', () => {
},
});

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

const result = await resultPromise;

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

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

const result = await resultPromise;

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

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

const result = await resultPromise;

Expand All @@ -267,7 +359,7 @@ describe('Execute: Cancellation', () => {
line: 6,
},
],
message: 'Aborted',
message: 'This operation was aborted',
path: ['todo', 'text'],
},
],
Expand Down Expand Up @@ -299,7 +391,7 @@ describe('Execute: Cancellation', () => {
},
});

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

const result = await resultPromise;

Expand All @@ -310,7 +402,7 @@ describe('Execute: Cancellation', () => {
},
errors: [
{
message: 'Aborted',
message: 'This operation was aborted',
path: ['bar'],
locations: [{ line: 4, column: 9 }],
},
Expand All @@ -330,7 +422,7 @@ describe('Execute: Cancellation', () => {
}
}
`);
abortController.abort('Aborted');
abortController.abort();
const result = await execute({
document,
schema,
Expand All @@ -344,7 +436,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 @@ -732,7 +732,7 @@ function executeFields(
const abortSignal = exeContext.validatedExecutionArgs.abortSignal;
if (abortSignal?.aborted) {
throw locatedError(
new Error(abortSignal.reason),
abortSignal.reason,
toNodes(fieldDetailsList),
pathToArray(fieldPath),
);
Expand Down

0 comments on commit c345e40

Please sign in to comment.