Skip to content

Commit c345e40

Browse files
committed
fix: handle default and different types passed to abort method
1 parent cf1c080 commit c345e40

File tree

2 files changed

+107
-15
lines changed

2 files changed

+107
-15
lines changed

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

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

92-
abortController.abort('Aborted');
92+
abortController.abort();
9393

9494
const result = await resultPromise;
9595

@@ -99,7 +99,99 @@ describe('Execute: Cancellation', () => {
9999
},
100100
errors: [
101101
{
102-
message: 'Aborted',
102+
message: 'This operation was aborted',
103+
path: ['todo', 'id'],
104+
locations: [{ line: 4, column: 11 }],
105+
},
106+
],
107+
});
108+
});
109+
110+
it('should stop the execution when aborted during object field completion with a custom error', async () => {
111+
const abortController = new AbortController();
112+
const document = parse(`
113+
query {
114+
todo {
115+
id
116+
author {
117+
id
118+
}
119+
}
120+
}
121+
`);
122+
123+
const resultPromise = execute({
124+
document,
125+
schema,
126+
abortSignal: abortController.signal,
127+
rootValue: {
128+
todo: async () =>
129+
Promise.resolve({
130+
id: '1',
131+
text: 'Hello, World!',
132+
/* c8 ignore next */
133+
author: () => expect.fail('Should not be called'),
134+
}),
135+
},
136+
});
137+
138+
abortController.abort(new Error('Custom abort error'));
139+
140+
const result = await resultPromise;
141+
142+
expectJSON(result).toDeepEqual({
143+
data: {
144+
todo: null,
145+
},
146+
errors: [
147+
{
148+
message: 'Custom abort error',
149+
path: ['todo', 'id'],
150+
locations: [{ line: 4, column: 11 }],
151+
},
152+
],
153+
});
154+
});
155+
156+
it('should stop the execution when aborted during object field completion with a custom string', async () => {
157+
const abortController = new AbortController();
158+
const document = parse(`
159+
query {
160+
todo {
161+
id
162+
author {
163+
id
164+
}
165+
}
166+
}
167+
`);
168+
169+
const resultPromise = execute({
170+
document,
171+
schema,
172+
abortSignal: abortController.signal,
173+
rootValue: {
174+
todo: async () =>
175+
Promise.resolve({
176+
id: '1',
177+
text: 'Hello, World!',
178+
/* c8 ignore next */
179+
author: () => expect.fail('Should not be called'),
180+
}),
181+
},
182+
});
183+
184+
abortController.abort('Custom abort error message');
185+
186+
const result = await resultPromise;
187+
188+
expectJSON(result).toDeepEqual({
189+
data: {
190+
todo: null,
191+
},
192+
errors: [
193+
{
194+
message: 'Unexpected error value: "Custom abort error message"',
103195
path: ['todo', 'id'],
104196
locations: [{ line: 4, column: 11 }],
105197
},
@@ -135,7 +227,7 @@ describe('Execute: Cancellation', () => {
135227
},
136228
});
137229

138-
abortController.abort('Aborted');
230+
abortController.abort();
139231

140232
const result = await resultPromise;
141233

@@ -148,7 +240,7 @@ describe('Execute: Cancellation', () => {
148240
},
149241
errors: [
150242
{
151-
message: 'Aborted',
243+
message: 'This operation was aborted',
152244
path: ['todo', 'author', 'id'],
153245
locations: [{ line: 6, column: 13 }],
154246
},
@@ -187,7 +279,7 @@ describe('Execute: Cancellation', () => {
187279
abortSignal: abortController.signal,
188280
});
189281

190-
abortController.abort('Aborted');
282+
abortController.abort();
191283

192284
const result = await resultPromise;
193285

@@ -197,7 +289,7 @@ describe('Execute: Cancellation', () => {
197289
},
198290
errors: [
199291
{
200-
message: 'Aborted',
292+
message: 'This operation was aborted',
201293
path: ['todo', 'id'],
202294
locations: [{ line: 4, column: 11 }],
203295
},
@@ -242,7 +334,7 @@ describe('Execute: Cancellation', () => {
242334
await resolveOnNextTick();
243335
await resolveOnNextTick();
244336

245-
abortController.abort('Aborted');
337+
abortController.abort();
246338

247339
const result = await resultPromise;
248340

@@ -267,7 +359,7 @@ describe('Execute: Cancellation', () => {
267359
line: 6,
268360
},
269361
],
270-
message: 'Aborted',
362+
message: 'This operation was aborted',
271363
path: ['todo', 'text'],
272364
},
273365
],
@@ -299,7 +391,7 @@ describe('Execute: Cancellation', () => {
299391
},
300392
});
301393

302-
abortController.abort('Aborted');
394+
abortController.abort();
303395

304396
const result = await resultPromise;
305397

@@ -310,7 +402,7 @@ describe('Execute: Cancellation', () => {
310402
},
311403
errors: [
312404
{
313-
message: 'Aborted',
405+
message: 'This operation was aborted',
314406
path: ['bar'],
315407
locations: [{ line: 4, column: 9 }],
316408
},
@@ -330,7 +422,7 @@ describe('Execute: Cancellation', () => {
330422
}
331423
}
332424
`);
333-
abortController.abort('Aborted');
425+
abortController.abort();
334426
const result = await execute({
335427
document,
336428
schema,
@@ -344,7 +436,7 @@ describe('Execute: Cancellation', () => {
344436
expectJSON(result).toDeepEqual({
345437
errors: [
346438
{
347-
message: 'Aborted',
439+
message: 'This operation was aborted',
348440
},
349441
],
350442
});

src/execution/execute.ts

Lines changed: 3 additions & 3 deletions
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,
@@ -732,7 +732,7 @@ function executeFields(
732732
const abortSignal = exeContext.validatedExecutionArgs.abortSignal;
733733
if (abortSignal?.aborted) {
734734
throw locatedError(
735-
new Error(abortSignal.reason),
735+
abortSignal.reason,
736736
toNodes(fieldDetailsList),
737737
pathToArray(fieldPath),
738738
);

0 commit comments

Comments
 (0)