@@ -4,6 +4,7 @@ import { describe, it } from 'mocha';
44import { expectJSON } from '../../__testUtils__/expectJSON.js' ;
55import { resolveOnNextTick } from '../../__testUtils__/resolveOnNextTick.js' ;
66
7+ import { AbortController } from '../../jsutils/AbortController.js' ;
78import { inspect } from '../../jsutils/inspect.js' ;
89
910import { Kind } from '../../language/kinds.js' ;
@@ -1314,59 +1315,59 @@ describe('Execute: Handles basic execution tasks', () => {
13141315 expect ( possibleTypes ) . to . deep . equal ( [ fooObject ] ) ;
13151316 } ) ;
13161317
1317- describe ( 'Abort execution' , ( ) => {
1318- it ( 'stops execution and throws an error when signal is aborted' , async ( ) => {
1319- /**
1320- * This test has 3 resolvers nested in each other.
1321- * Every resolve function waits 200ms before returning data.
1322- *
1323- * The test waits for the first resolver and half of the 2nd resolver execution time (200ms + 100ms)
1324- * and then aborts the execution.
1325- *
1326- * 2nd resolver execution finishes, and we then expect to not execute the 3rd resolver
1327- * and to get an error about aborted operation.
1328- */
1329-
1330- const WAIT_MS_BEFORE_RESOLVING = 200 ;
1331- const ABORT_IN_MS_AFTER_STARTING_EXECUTION =
1332- WAIT_MS_BEFORE_RESOLVING + WAIT_MS_BEFORE_RESOLVING / 2 ;
1333-
1334- const schema = new GraphQLSchema ( {
1335- query : new GraphQLObjectType ( {
1336- name : 'Query' ,
1337- fields : {
1338- resolvesIn500ms : {
1339- type : new GraphQLObjectType ( {
1340- name : 'ResolvesIn500ms' ,
1341- fields : {
1342- resolvesIn400ms : {
1343- type : new GraphQLObjectType ( {
1344- name : 'ResolvesIn400ms' ,
1345- fields : {
1346- shouldNotBeResolved : {
1347- type : GraphQLString ,
1348- resolve : ( ) => {
1349- throw new Error ( 'This should not be executed!' ) ;
1350- } ,
1318+ it ( 'stops execution and throws an error when signal is aborted' , async ( ) => {
1319+ /**
1320+ * This test has 3 resolvers nested in each other.
1321+ * Every resolve function waits 200ms before returning data.
1322+ *
1323+ * The test waits for the first resolver and half of the 2nd resolver execution time (200ms + 100ms)
1324+ * and then aborts the execution.
1325+ *
1326+ * 2nd resolver execution finishes, and we then expect to not execute the 3rd resolver
1327+ * and to get an error about aborted operation.
1328+ */
1329+
1330+ const WAIT_MS_BEFORE_RESOLVING = 200 ;
1331+ const ABORT_IN_MS_AFTER_STARTING_EXECUTION =
1332+ WAIT_MS_BEFORE_RESOLVING + WAIT_MS_BEFORE_RESOLVING / 2 ;
1333+
1334+ const schema = new GraphQLSchema ( {
1335+ query : new GraphQLObjectType ( {
1336+ name : 'Query' ,
1337+ fields : {
1338+ resolvesIn500ms : {
1339+ type : new GraphQLObjectType ( {
1340+ name : 'ResolvesIn500ms' ,
1341+ fields : {
1342+ resolvesIn400ms : {
1343+ type : new GraphQLObjectType ( {
1344+ name : 'ResolvesIn400ms' ,
1345+ fields : {
1346+ shouldNotBeResolved : {
1347+ type : GraphQLString ,
1348+ /* c8 ignore next 3 */
1349+ resolve : ( ) => {
1350+ throw new Error ( 'This should not be executed!' ) ;
13511351 } ,
13521352 } ,
1353+ } ,
1354+ } ) ,
1355+ resolve : ( ) =>
1356+ new Promise ( ( resolve ) => {
1357+ setTimeout ( ( ) => resolve ( { } ) , WAIT_MS_BEFORE_RESOLVING ) ;
13531358 } ) ,
1354- resolve : ( ) =>
1355- new Promise ( ( resolve ) => {
1356- setTimeout ( ( ) => resolve ( { } ) , WAIT_MS_BEFORE_RESOLVING ) ;
1357- } ) ,
1358- } ,
13591359 } ,
1360+ } ,
1361+ } ) ,
1362+ resolve : ( ) =>
1363+ new Promise ( ( resolve ) => {
1364+ setTimeout ( ( ) => resolve ( { } ) , WAIT_MS_BEFORE_RESOLVING ) ;
13601365 } ) ,
1361- resolve : ( ) =>
1362- new Promise ( ( resolve ) => {
1363- setTimeout ( ( ) => resolve ( { } ) , WAIT_MS_BEFORE_RESOLVING ) ;
1364- } ) ,
1365- } ,
13661366 } ,
1367- } ) ,
1368- } ) ;
1369- const document = parse ( `
1367+ } ,
1368+ } ) ,
1369+ } ) ;
1370+ const document = parse ( `
13701371 query {
13711372 resolvesIn500ms {
13721373 resolvesIn400ms {
@@ -1376,67 +1377,22 @@ describe('Execute: Handles basic execution tasks', () => {
13761377 }
13771378 ` ) ;
13781379
1379- const abortController = new AbortController ( ) ;
1380- const executionPromise = execute ( {
1381- schema,
1382- document,
1383- signal : abortController . signal ,
1384- } ) ;
1385-
1386- setTimeout (
1387- ( ) => abortController . abort ( ) ,
1388- ABORT_IN_MS_AFTER_STARTING_EXECUTION ,
1389- ) ;
1390-
1391- const result = await executionPromise ;
1392- expect ( result . errors ?. [ 0 ] . message ) . to . eq (
1393- 'Execution aborted. Reason: AbortError: This operation was aborted' ,
1394- ) ;
1395- expect ( result . data ) . to . eql ( {
1396- resolvesIn500ms : { resolvesIn400ms : null } ,
1397- } ) ;
1398- } ) ;
1399-
1400- const abortMessageTestInputs = [
1401- { message : 'Aborted from somewhere' , reason : 'Aborted from somewhere' } ,
1402- { message : undefined , reason : 'AbortError: This operation was aborted' } ,
1403- ] ;
1404-
1405- for ( const { message, reason } of abortMessageTestInputs ) {
1406- it ( 'aborts with "Reason:" in the error message' , async ( ) => {
1407- const schema = new GraphQLSchema ( {
1408- query : new GraphQLObjectType ( {
1409- name : 'Query' ,
1410- fields : {
1411- a : {
1412- type : GraphQLString ,
1413- resolve : ( ) =>
1414- new Promise ( ( resolve ) => {
1415- setTimeout ( ( ) => resolve ( { } ) , 100 ) ;
1416- } ) ,
1417- } ,
1418- } ,
1419- } ) ,
1420- } ) ;
1421-
1422- const document = parse ( `
1423- query { a }
1424- ` ) ;
1425-
1426- const abortController = new AbortController ( ) ;
1427- const executionPromise = execute ( {
1428- schema,
1429- document,
1430- signal : abortController . signal ,
1431- } ) ;
1380+ const abortController = new AbortController ( ) ;
1381+ const executionPromise = execute ( {
1382+ schema,
1383+ document,
1384+ signal : abortController . signal ,
1385+ } ) ;
14321386
1433- abortController . abort ( message ) ;
1387+ setTimeout (
1388+ ( ) => abortController . abort ( ) ,
1389+ ABORT_IN_MS_AFTER_STARTING_EXECUTION ,
1390+ ) ;
14341391
1435- const { errors } = await executionPromise ;
1436- expect ( errors ?. [ 0 ] . message ) . to . eq (
1437- `Execution aborted. Reason: ${ reason } ` ,
1438- ) ;
1439- } ) ;
1440- }
1392+ const result = await executionPromise ;
1393+ expect ( result . errors ?. [ 0 ] . message ) . to . eq ( 'Execution aborted.' ) ;
1394+ expect ( result . data ) . to . eql ( {
1395+ resolvesIn500ms : { resolvesIn400ms : null } ,
1396+ } ) ;
14411397 } ) ;
14421398} ) ;
0 commit comments