Skip to content

Commit 9dae904

Browse files
authored
fix: handle default and different types passed to abort method (#4258)
1 parent 83da23a commit 9dae904

File tree

2 files changed

+112
-15
lines changed

2 files changed

+112
-15
lines changed

src/execution/__tests__/abort-signal-test.ts

+109-12
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,104 @@ describe('Execute: Cancellation', () => {
8989
},
9090
});
9191

92-
abortController.abort('Aborted');
92+
abortController.abort();
93+
94+
const result = await resultPromise;
95+
96+
expect(result.errors?.[0].originalError?.name).to.equal('AbortError');
97+
98+
expectJSON(result).toDeepEqual({
99+
data: {
100+
todo: null,
101+
},
102+
errors: [
103+
{
104+
message: 'This operation was aborted',
105+
path: ['todo'],
106+
locations: [{ line: 3, column: 9 }],
107+
},
108+
],
109+
});
110+
});
111+
112+
it('should stop the execution when aborted during object field completion with a custom error', async () => {
113+
const abortController = new AbortController();
114+
const document = parse(`
115+
query {
116+
todo {
117+
id
118+
author {
119+
id
120+
}
121+
}
122+
}
123+
`);
124+
125+
const resultPromise = execute({
126+
document,
127+
schema,
128+
abortSignal: abortController.signal,
129+
rootValue: {
130+
todo: async () =>
131+
Promise.resolve({
132+
id: '1',
133+
text: 'Hello, World!',
134+
/* c8 ignore next */
135+
author: () => expect.fail('Should not be called'),
136+
}),
137+
},
138+
});
139+
140+
const customError = new Error('Custom abort error');
141+
abortController.abort(customError);
142+
143+
const result = await resultPromise;
144+
145+
expect(result.errors?.[0].originalError).to.equal(customError);
146+
147+
expectJSON(result).toDeepEqual({
148+
data: {
149+
todo: null,
150+
},
151+
errors: [
152+
{
153+
message: 'Custom abort error',
154+
path: ['todo'],
155+
locations: [{ line: 3, column: 9 }],
156+
},
157+
],
158+
});
159+
});
160+
161+
it('should stop the execution when aborted during object field completion with a custom string', async () => {
162+
const abortController = new AbortController();
163+
const document = parse(`
164+
query {
165+
todo {
166+
id
167+
author {
168+
id
169+
}
170+
}
171+
}
172+
`);
173+
174+
const resultPromise = execute({
175+
document,
176+
schema,
177+
abortSignal: abortController.signal,
178+
rootValue: {
179+
todo: async () =>
180+
Promise.resolve({
181+
id: '1',
182+
text: 'Hello, World!',
183+
/* c8 ignore next */
184+
author: () => expect.fail('Should not be called'),
185+
}),
186+
},
187+
});
188+
189+
abortController.abort('Custom abort error message');
93190

94191
const result = await resultPromise;
95192

@@ -99,7 +196,7 @@ describe('Execute: Cancellation', () => {
99196
},
100197
errors: [
101198
{
102-
message: 'Aborted',
199+
message: 'Unexpected error value: "Custom abort error message"',
103200
path: ['todo'],
104201
locations: [{ line: 3, column: 9 }],
105202
},
@@ -135,7 +232,7 @@ describe('Execute: Cancellation', () => {
135232
},
136233
});
137234

138-
abortController.abort('Aborted');
235+
abortController.abort();
139236

140237
const result = await resultPromise;
141238

@@ -148,7 +245,7 @@ describe('Execute: Cancellation', () => {
148245
},
149246
errors: [
150247
{
151-
message: 'Aborted',
248+
message: 'This operation was aborted',
152249
path: ['todo', 'author'],
153250
locations: [{ line: 5, column: 11 }],
154251
},
@@ -187,7 +284,7 @@ describe('Execute: Cancellation', () => {
187284
abortSignal: abortController.signal,
188285
});
189286

190-
abortController.abort('Aborted');
287+
abortController.abort();
191288

192289
const result = await resultPromise;
193290

@@ -197,7 +294,7 @@ describe('Execute: Cancellation', () => {
197294
},
198295
errors: [
199296
{
200-
message: 'Aborted',
297+
message: 'This operation was aborted',
201298
path: ['todo'],
202299
locations: [{ line: 3, column: 9 }],
203300
},
@@ -242,7 +339,7 @@ describe('Execute: Cancellation', () => {
242339
await resolveOnNextTick();
243340
await resolveOnNextTick();
244341

245-
abortController.abort('Aborted');
342+
abortController.abort();
246343

247344
const result = await resultPromise;
248345

@@ -271,7 +368,7 @@ describe('Execute: Cancellation', () => {
271368
line: 7,
272369
},
273370
],
274-
message: 'Aborted',
371+
message: 'This operation was aborted',
275372
path: ['todo', 'author'],
276373
},
277374
],
@@ -304,7 +401,7 @@ describe('Execute: Cancellation', () => {
304401
},
305402
});
306403

307-
abortController.abort('Aborted');
404+
abortController.abort();
308405

309406
const result = await resultPromise;
310407

@@ -315,7 +412,7 @@ describe('Execute: Cancellation', () => {
315412
},
316413
errors: [
317414
{
318-
message: 'Aborted',
415+
message: 'This operation was aborted',
319416
path: ['bar'],
320417
locations: [{ line: 4, column: 9 }],
321418
},
@@ -335,7 +432,7 @@ describe('Execute: Cancellation', () => {
335432
}
336433
}
337434
`);
338-
abortController.abort('Aborted');
435+
abortController.abort();
339436
const result = await execute({
340437
document,
341438
schema,
@@ -349,7 +446,7 @@ describe('Execute: Cancellation', () => {
349446
expectJSON(result).toDeepEqual({
350447
errors: [
351448
{
352-
message: 'Aborted',
449+
message: 'This operation was aborted',
353450
},
354451
],
355452
});

src/execution/execute.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ export function validateExecutionArgs(
518518
} = args;
519519

520520
if (abortSignal?.aborted) {
521-
return [locatedError(new Error(abortSignal.reason), undefined)];
521+
return [locatedError(abortSignal.reason, undefined)];
522522
}
523523

524524
// If the schema used for execution is invalid, throw an error.
@@ -668,7 +668,7 @@ function executeFieldsSerially(
668668
const abortSignal = exeContext.validatedExecutionArgs.abortSignal;
669669
if (abortSignal?.aborted) {
670670
handleFieldError(
671-
new Error(abortSignal.reason),
671+
abortSignal.reason,
672672
exeContext,
673673
parentType,
674674
fieldDetailsList,
@@ -1732,7 +1732,7 @@ function completeObjectValue(
17321732
const abortSignal = validatedExecutionArgs.abortSignal;
17331733
if (abortSignal?.aborted) {
17341734
throw locatedError(
1735-
new Error(abortSignal.reason),
1735+
abortSignal.reason,
17361736
toNodes(fieldDetailsList),
17371737
pathToArray(path),
17381738
);

0 commit comments

Comments
 (0)