Skip to content

Commit cb2bb7e

Browse files
committed
fix: handle default and different types passed to abort method (#4258)
1 parent b542051 commit cb2bb7e

2 files changed

Lines changed: 108 additions & 11 deletions

File tree

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

Lines changed: 105 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,104 @@ describe('Execute: Cancellation', () => {
6060
},
6161
});
6262

63-
abortController.abort('Aborted');
63+
abortController.abort();
64+
65+
const result = await resultPromise;
66+
67+
expect(result.errors?.[0].originalError?.name).to.equal('AbortError');
68+
69+
expectJSON(result).toDeepEqual({
70+
data: {
71+
todo: null,
72+
},
73+
errors: [
74+
{
75+
message: 'This operation was aborted',
76+
path: ['todo'],
77+
locations: [{ line: 3, column: 9 }],
78+
},
79+
],
80+
});
81+
});
82+
83+
it('should stop the execution when aborted during object field completion with a custom error', async () => {
84+
const abortController = new AbortController();
85+
const document = parse(`
86+
query {
87+
todo {
88+
id
89+
author {
90+
id
91+
}
92+
}
93+
}
94+
`);
95+
96+
const resultPromise = execute({
97+
document,
98+
schema,
99+
abortSignal: abortController.signal,
100+
rootValue: {
101+
todo: async () =>
102+
Promise.resolve({
103+
id: '1',
104+
text: 'Hello, World!',
105+
/* c8 ignore next */
106+
author: () => expect.fail('Should not be called'),
107+
}),
108+
},
109+
});
110+
111+
const customError = new Error('Custom abort error');
112+
abortController.abort(customError);
113+
114+
const result = await resultPromise;
115+
116+
expect(result.errors?.[0].originalError).to.equal(customError);
117+
118+
expectJSON(result).toDeepEqual({
119+
data: {
120+
todo: null,
121+
},
122+
errors: [
123+
{
124+
message: 'Custom abort error',
125+
path: ['todo'],
126+
locations: [{ line: 3, column: 9 }],
127+
},
128+
],
129+
});
130+
});
131+
132+
it('should stop the execution when aborted during object field completion with a custom string', async () => {
133+
const abortController = new AbortController();
134+
const document = parse(`
135+
query {
136+
todo {
137+
id
138+
author {
139+
id
140+
}
141+
}
142+
}
143+
`);
144+
145+
const resultPromise = execute({
146+
document,
147+
schema,
148+
abortSignal: abortController.signal,
149+
rootValue: {
150+
todo: async () =>
151+
Promise.resolve({
152+
id: '1',
153+
text: 'Hello, World!',
154+
/* c8 ignore next */
155+
author: () => expect.fail('Should not be called'),
156+
}),
157+
},
158+
});
159+
160+
abortController.abort('Custom abort error message');
64161

65162
const result = await resultPromise;
66163

@@ -70,7 +167,7 @@ describe('Execute: Cancellation', () => {
70167
},
71168
errors: [
72169
{
73-
message: 'Aborted',
170+
message: 'Unexpected error value: "Custom abort error message"',
74171
path: ['todo'],
75172
locations: [{ line: 3, column: 9 }],
76173
},
@@ -106,7 +203,7 @@ describe('Execute: Cancellation', () => {
106203
},
107204
});
108205

109-
abortController.abort('Aborted');
206+
abortController.abort();
110207

111208
const result = await resultPromise;
112209

@@ -119,7 +216,7 @@ describe('Execute: Cancellation', () => {
119216
},
120217
errors: [
121218
{
122-
message: 'Aborted',
219+
message: 'This operation was aborted',
123220
path: ['todo', 'author'],
124221
locations: [{ line: 5, column: 11 }],
125222
},
@@ -147,7 +244,7 @@ describe('Execute: Cancellation', () => {
147244
},
148245
});
149246

150-
abortController.abort('Aborted');
247+
abortController.abort();
151248

152249
const result = await resultPromise;
153250

@@ -158,7 +255,7 @@ describe('Execute: Cancellation', () => {
158255
},
159256
errors: [
160257
{
161-
message: 'Aborted',
258+
message: 'This operation was aborted',
162259
path: ['bar'],
163260
locations: [{ line: 4, column: 9 }],
164261
},
@@ -178,7 +275,7 @@ describe('Execute: Cancellation', () => {
178275
}
179276
}
180277
`);
181-
abortController.abort('Aborted');
278+
abortController.abort();
182279
const result = await execute({
183280
document,
184281
schema,
@@ -192,7 +289,7 @@ describe('Execute: Cancellation', () => {
192289
expectJSON(result).toDeepEqual({
193290
errors: [
194291
{
195-
message: 'Aborted',
292+
message: 'This operation was aborted',
196293
},
197294
],
198295
});

src/execution/execute.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ export function validateExecutionArgs(
383383
} = args;
384384

385385
if (abortSignal?.aborted) {
386-
return [locatedError(new Error(abortSignal.reason), undefined)];
386+
return [locatedError(abortSignal.reason, undefined)];
387387
}
388388

389389
// If the schema used for execution is invalid, throw an error.
@@ -523,7 +523,7 @@ function executeFieldsSerially(
523523
const abortSignal = exeContext.validatedExecutionArgs.abortSignal;
524524
if (abortSignal?.aborted) {
525525
handleFieldError(
526-
new Error(abortSignal.reason),
526+
abortSignal.reason,
527527
exeContext,
528528
parentType,
529529
fieldDetailsList,
@@ -1300,7 +1300,7 @@ function completeObjectValue(
13001300
const abortSignal = validatedExecutionArgs.abortSignal;
13011301
if (abortSignal?.aborted) {
13021302
throw locatedError(
1303-
new Error(abortSignal.reason),
1303+
abortSignal.reason,
13041304
toNodes(fieldDetailsList),
13051305
pathToArray(path),
13061306
);

0 commit comments

Comments
 (0)