@@ -4,6 +4,7 @@ import { describe, it } from 'mocha';
4
4
import { expectJSON } from '../../__testUtils__/expectJSON.js' ;
5
5
import { resolveOnNextTick } from '../../__testUtils__/resolveOnNextTick.js' ;
6
6
7
+ import { AbortController } from '../../jsutils/AbortController.js' ;
7
8
import { inspect } from '../../jsutils/inspect.js' ;
8
9
9
10
import { Kind } from '../../language/kinds.js' ;
@@ -1276,59 +1277,59 @@ describe('Execute: Handles basic execution tasks', () => {
1276
1277
expect ( possibleTypes ) . to . deep . equal ( [ fooObject ] ) ;
1277
1278
} ) ;
1278
1279
1279
- describe ( 'Abort execution' , ( ) => {
1280
- it ( 'stops execution and throws an error when signal is aborted' , async ( ) => {
1281
- /**
1282
- * This test has 3 resolvers nested in each other.
1283
- * Every resolve function waits 200ms before returning data.
1284
- *
1285
- * The test waits for the first resolver and half of the 2nd resolver execution time (200ms + 100ms)
1286
- * and then aborts the execution.
1287
- *
1288
- * 2nd resolver execution finishes, and we then expect to not execute the 3rd resolver
1289
- * and to get an error about aborted operation.
1290
- */
1291
-
1292
- const WAIT_MS_BEFORE_RESOLVING = 200 ;
1293
- const ABORT_IN_MS_AFTER_STARTING_EXECUTION =
1294
- WAIT_MS_BEFORE_RESOLVING + WAIT_MS_BEFORE_RESOLVING / 2 ;
1295
-
1296
- const schema = new GraphQLSchema ( {
1297
- query : new GraphQLObjectType ( {
1298
- name : 'Query' ,
1299
- fields : {
1300
- resolvesIn500ms : {
1301
- type : new GraphQLObjectType ( {
1302
- name : 'ResolvesIn500ms' ,
1303
- fields : {
1304
- resolvesIn400ms : {
1305
- type : new GraphQLObjectType ( {
1306
- name : 'ResolvesIn400ms' ,
1307
- fields : {
1308
- shouldNotBeResolved : {
1309
- type : GraphQLString ,
1310
- resolve : ( ) => {
1311
- throw new Error ( 'This should not be executed!' ) ;
1312
- } ,
1280
+ it ( 'stops execution and throws an error when signal is aborted' , async ( ) => {
1281
+ /**
1282
+ * This test has 3 resolvers nested in each other.
1283
+ * Every resolve function waits 200ms before returning data.
1284
+ *
1285
+ * The test waits for the first resolver and half of the 2nd resolver execution time (200ms + 100ms)
1286
+ * and then aborts the execution.
1287
+ *
1288
+ * 2nd resolver execution finishes, and we then expect to not execute the 3rd resolver
1289
+ * and to get an error about aborted operation.
1290
+ */
1291
+
1292
+ const WAIT_MS_BEFORE_RESOLVING = 200 ;
1293
+ const ABORT_IN_MS_AFTER_STARTING_EXECUTION =
1294
+ WAIT_MS_BEFORE_RESOLVING + WAIT_MS_BEFORE_RESOLVING / 2 ;
1295
+
1296
+ const schema = new GraphQLSchema ( {
1297
+ query : new GraphQLObjectType ( {
1298
+ name : 'Query' ,
1299
+ fields : {
1300
+ resolvesIn500ms : {
1301
+ type : new GraphQLObjectType ( {
1302
+ name : 'ResolvesIn500ms' ,
1303
+ fields : {
1304
+ resolvesIn400ms : {
1305
+ type : new GraphQLObjectType ( {
1306
+ name : 'ResolvesIn400ms' ,
1307
+ fields : {
1308
+ shouldNotBeResolved : {
1309
+ type : GraphQLString ,
1310
+ /* c8 ignore next 3 */
1311
+ resolve : ( ) => {
1312
+ throw new Error ( 'This should not be executed!' ) ;
1313
1313
} ,
1314
1314
} ,
1315
+ } ,
1316
+ } ) ,
1317
+ resolve : ( ) =>
1318
+ new Promise ( ( resolve ) => {
1319
+ setTimeout ( ( ) => resolve ( { } ) , WAIT_MS_BEFORE_RESOLVING ) ;
1315
1320
} ) ,
1316
- resolve : ( ) =>
1317
- new Promise ( ( resolve ) => {
1318
- setTimeout ( ( ) => resolve ( { } ) , WAIT_MS_BEFORE_RESOLVING ) ;
1319
- } ) ,
1320
- } ,
1321
1321
} ,
1322
+ } ,
1323
+ } ) ,
1324
+ resolve : ( ) =>
1325
+ new Promise ( ( resolve ) => {
1326
+ setTimeout ( ( ) => resolve ( { } ) , WAIT_MS_BEFORE_RESOLVING ) ;
1322
1327
} ) ,
1323
- resolve : ( ) =>
1324
- new Promise ( ( resolve ) => {
1325
- setTimeout ( ( ) => resolve ( { } ) , WAIT_MS_BEFORE_RESOLVING ) ;
1326
- } ) ,
1327
- } ,
1328
1328
} ,
1329
- } ) ,
1330
- } ) ;
1331
- const document = parse ( `
1329
+ } ,
1330
+ } ) ,
1331
+ } ) ;
1332
+ const document = parse ( `
1332
1333
query {
1333
1334
resolvesIn500ms {
1334
1335
resolvesIn400ms {
@@ -1338,67 +1339,22 @@ describe('Execute: Handles basic execution tasks', () => {
1338
1339
}
1339
1340
` ) ;
1340
1341
1341
- const abortController = new AbortController ( ) ;
1342
- const executionPromise = execute ( {
1343
- schema,
1344
- document,
1345
- signal : abortController . signal ,
1346
- } ) ;
1347
-
1348
- setTimeout (
1349
- ( ) => abortController . abort ( ) ,
1350
- ABORT_IN_MS_AFTER_STARTING_EXECUTION ,
1351
- ) ;
1352
-
1353
- const result = await executionPromise ;
1354
- expect ( result . errors ?. [ 0 ] . message ) . to . eq (
1355
- 'Execution aborted. Reason: AbortError: This operation was aborted' ,
1356
- ) ;
1357
- expect ( result . data ) . to . eql ( {
1358
- resolvesIn500ms : { resolvesIn400ms : null } ,
1359
- } ) ;
1360
- } ) ;
1361
-
1362
- const abortMessageTestInputs = [
1363
- { message : 'Aborted from somewhere' , reason : 'Aborted from somewhere' } ,
1364
- { message : undefined , reason : 'AbortError: This operation was aborted' } ,
1365
- ] ;
1366
-
1367
- for ( const { message, reason } of abortMessageTestInputs ) {
1368
- it ( 'aborts with "Reason:" in the error message' , async ( ) => {
1369
- const schema = new GraphQLSchema ( {
1370
- query : new GraphQLObjectType ( {
1371
- name : 'Query' ,
1372
- fields : {
1373
- a : {
1374
- type : GraphQLString ,
1375
- resolve : ( ) =>
1376
- new Promise ( ( resolve ) => {
1377
- setTimeout ( ( ) => resolve ( { } ) , 100 ) ;
1378
- } ) ,
1379
- } ,
1380
- } ,
1381
- } ) ,
1382
- } ) ;
1383
-
1384
- const document = parse ( `
1385
- query { a }
1386
- ` ) ;
1387
-
1388
- const abortController = new AbortController ( ) ;
1389
- const executionPromise = execute ( {
1390
- schema,
1391
- document,
1392
- signal : abortController . signal ,
1393
- } ) ;
1342
+ const abortController = new AbortController ( ) ;
1343
+ const executionPromise = execute ( {
1344
+ schema,
1345
+ document,
1346
+ signal : abortController . signal ,
1347
+ } ) ;
1394
1348
1395
- abortController . abort ( message ) ;
1349
+ setTimeout (
1350
+ ( ) => abortController . abort ( ) ,
1351
+ ABORT_IN_MS_AFTER_STARTING_EXECUTION ,
1352
+ ) ;
1396
1353
1397
- const { errors } = await executionPromise ;
1398
- expect ( errors ?. [ 0 ] . message ) . to . eq (
1399
- `Execution aborted. Reason: ${ reason } ` ,
1400
- ) ;
1401
- } ) ;
1402
- }
1354
+ const result = await executionPromise ;
1355
+ expect ( result . errors ?. [ 0 ] . message ) . to . eq ( 'Execution aborted.' ) ;
1356
+ expect ( result . data ) . to . eql ( {
1357
+ resolvesIn500ms : { resolvesIn400ms : null } ,
1358
+ } ) ;
1403
1359
} ) ;
1404
1360
} ) ;
0 commit comments