Skip to content

Commit 057e9d2

Browse files
committed
fix: handle default and different types passed to abort method (graphql#4258)
1 parent 01f459e commit 057e9d2

File tree

2 files changed

+110
-13
lines changed

2 files changed

+110
-13
lines changed

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

Lines changed: 107 additions & 10 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
},
@@ -158,7 +255,7 @@ describe('Execute: Cancellation', () => {
158255
abortSignal: abortController.signal,
159256
});
160257

161-
abortController.abort('Aborted');
258+
abortController.abort();
162259

163260
const result = await resultPromise;
164261

@@ -168,7 +265,7 @@ describe('Execute: Cancellation', () => {
168265
},
169266
errors: [
170267
{
171-
message: 'Aborted',
268+
message: 'This operation was aborted',
172269
path: ['todo'],
173270
locations: [{ line: 3, column: 9 }],
174271
},
@@ -196,7 +293,7 @@ describe('Execute: Cancellation', () => {
196293
},
197294
});
198295

199-
abortController.abort('Aborted');
296+
abortController.abort();
200297

201298
const result = await resultPromise;
202299

@@ -207,7 +304,7 @@ describe('Execute: Cancellation', () => {
207304
},
208305
errors: [
209306
{
210-
message: 'Aborted',
307+
message: 'This operation was aborted',
211308
path: ['bar'],
212309
locations: [{ line: 4, column: 9 }],
213310
},
@@ -227,7 +324,7 @@ describe('Execute: Cancellation', () => {
227324
}
228325
}
229326
`);
230-
abortController.abort('Aborted');
327+
abortController.abort();
231328
const result = await execute({
232329
document,
233330
schema,
@@ -241,7 +338,7 @@ describe('Execute: Cancellation', () => {
241338
expectJSON(result).toDeepEqual({
242339
errors: [
243340
{
244-
message: 'Aborted',
341+
message: 'This operation was aborted',
245342
},
246343
],
247344
});

src/execution/execute.ts

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

346346
if (abortSignal?.aborted) {
347-
return [locatedError(new Error(abortSignal.reason), undefined)];
347+
return [locatedError(abortSignal.reason, undefined)];
348348
}
349349

350350
// If the schema used for execution is invalid, throw an error.
@@ -484,7 +484,7 @@ function executeFieldsSerially(
484484
const abortSignal = exeContext.validatedExecutionArgs.abortSignal;
485485
if (abortSignal?.aborted) {
486486
handleFieldError(
487-
new Error(abortSignal.reason),
487+
abortSignal.reason,
488488
exeContext,
489489
parentType,
490490
fieldDetailsList,
@@ -1268,7 +1268,7 @@ function completeObjectValue(
12681268
const abortSignal = validatedExecutionArgs.abortSignal;
12691269
if (abortSignal?.aborted) {
12701270
throw locatedError(
1271-
new Error(abortSignal.reason),
1271+
abortSignal.reason,
12721272
toNodes(fieldDetailsList),
12731273
pathToArray(path),
12741274
);

0 commit comments

Comments
 (0)