Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
1. use throwIfAborted
2. use correct path when aborting for a fieldPath within executeFields
3. polish tests to throw a proper Error as the reason
  • Loading branch information
yaacovCR committed Oct 23, 2024
1 parent ae108b2 commit f05fc11
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 31 deletions.
43 changes: 21 additions & 22 deletions src/execution/__tests__/abort-signal-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const schema = buildSchema(`
`);

describe('Execute: Cancellation', () => {
it('should stop the execution when aborted', async () => {
it('should stop the execution when aborted during object field completion', async () => {
const abortController = new AbortController();
const document = parse(`
query {
Expand Down Expand Up @@ -89,7 +89,7 @@ describe('Execute: Cancellation', () => {
},
});

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

const result = await resultPromise;

Expand All @@ -100,8 +100,8 @@ describe('Execute: Cancellation', () => {
errors: [
{
message: 'Aborted',
path: ['todo'],
locations: [{ line: 3, column: 9 }],
path: ['todo', 'id'],
locations: [{ line: 4, column: 11 }],
},
],
});
Expand Down Expand Up @@ -135,7 +135,7 @@ describe('Execute: Cancellation', () => {
},
});

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

const result = await resultPromise;

Expand All @@ -149,8 +149,8 @@ describe('Execute: Cancellation', () => {
errors: [
{
message: 'Aborted',
path: ['todo', 'author'],
locations: [{ line: 5, column: 11 }],
path: ['todo', 'author', 'id'],
locations: [{ line: 6, column: 13 }],
},
],
});
Expand All @@ -165,9 +165,7 @@ describe('Execute: Cancellation', () => {
... on Todo @defer {
text
author {
... on Author @defer {
id
}
id
}
}
}
Expand All @@ -189,7 +187,7 @@ describe('Execute: Cancellation', () => {
abortSignal: abortController.signal,
});

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

const result = await resultPromise;

Expand All @@ -200,8 +198,8 @@ describe('Execute: Cancellation', () => {
errors: [
{
message: 'Aborted',
path: ['todo'],
locations: [{ line: 3, column: 9 }],
path: ['todo', 'id'],
locations: [{ line: 4, column: 11 }],
},
],
});
Expand All @@ -216,9 +214,7 @@ describe('Execute: Cancellation', () => {
... on Todo @defer {
text
author {
... on Author @defer {
id
}
id
}
}
}
Expand All @@ -232,9 +228,8 @@ describe('Execute: Cancellation', () => {
Promise.resolve({
id: '1',
text: 'hello world',
/* c8 ignore next 2 */
author: async () =>
Promise.resolve(() => expect.fail('Should not be called')),
/* c8 ignore next */
author: () => expect.fail('Should not be called'),
}),
},
abortController.signal,
Expand All @@ -244,7 +239,7 @@ describe('Execute: Cancellation', () => {
await resolveOnNextTick();
await resolveOnNextTick();

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

const result = await resultPromise;

Expand All @@ -264,6 +259,8 @@ describe('Execute: Cancellation', () => {
errors: [
{
message: 'Aborted',
path: ['todo', 'text'],
locations: [{ line: 6, column: 13 }],
},
],
id: '0',
Expand Down Expand Up @@ -294,7 +291,7 @@ describe('Execute: Cancellation', () => {
},
});

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

const result = await resultPromise;

Expand Down Expand Up @@ -325,7 +322,9 @@ describe('Execute: Cancellation', () => {
}
}
`);
abortController.abort('Aborted');

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

const result = await execute({
document,
schema,
Expand Down
27 changes: 18 additions & 9 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,10 @@ export function validateExecutionArgs(
abortSignal,
} = args;

if (abortSignal?.aborted) {
return [new GraphQLError(abortSignal.reason)];
try {
abortSignal?.throwIfAborted();
} catch (rawError: unknown) {
return [locatedError(rawError, undefined)];
}

// If the schema used for execution is invalid, throw an error.
Expand Down Expand Up @@ -665,10 +667,11 @@ function executeFieldsSerially(
groupedFieldSet,
(graphqlWrappedResult, [responseName, fieldDetailsList]) => {
const fieldPath = addPath(path, responseName, parentType.name);
const abortSignal = exeContext.validatedExecutionArgs.abortSignal;
if (abortSignal?.aborted) {
try {
exeContext.validatedExecutionArgs.abortSignal?.throwIfAborted();
} catch (rawError) {
handleFieldError(
new Error(abortSignal.reason),
rawError,
exeContext,
parentType,
fieldDetailsList,
Expand Down Expand Up @@ -728,12 +731,18 @@ function executeFields(

try {
for (const [responseName, fieldDetailsList] of groupedFieldSet) {
const abortSignal = exeContext.validatedExecutionArgs.abortSignal;
if (abortSignal?.aborted) {
throw new GraphQLError(abortSignal.reason);
const fieldPath = addPath(path, responseName, parentType.name);

try {
exeContext.validatedExecutionArgs.abortSignal?.throwIfAborted();
} catch (rawError) {
throw locatedError(
rawError,
toNodes(fieldDetailsList),
pathToArray(fieldPath),
);
}

const fieldPath = addPath(path, responseName, parentType.name);
const result = executeField(
exeContext,
parentType,
Expand Down

0 comments on commit f05fc11

Please sign in to comment.