@@ -30,6 +30,7 @@ import {
30
30
findFieldsThatChangedType ,
31
31
findRemovedTypes ,
32
32
findTypesRemovedFromUnions ,
33
+ findTypesAddedToUnions ,
33
34
findTypesThatChangedKind ,
34
35
findValuesRemovedFromEnums ,
35
36
findValuesAddedToEnums ,
@@ -1390,6 +1391,60 @@ describe('findDangerousChanges', () => {
1390
1391
) ;
1391
1392
} ) ;
1392
1393
1394
+ it ( 'should detect if a type was added to a union type' , ( ) => {
1395
+ const type1 = new GraphQLObjectType ( {
1396
+ name : 'Type1' ,
1397
+ fields : {
1398
+ field1 : { type : GraphQLString } ,
1399
+ }
1400
+ } ) ;
1401
+ // logially equivalent to type1; findTypesRemovedFromUnions should not
1402
+ // treat this as different than type1
1403
+ const type1a = new GraphQLObjectType ( {
1404
+ name : 'Type1' ,
1405
+ fields : {
1406
+ field1 : { type : GraphQLString } ,
1407
+ }
1408
+ } ) ;
1409
+ const type2 = new GraphQLObjectType ( {
1410
+ name : 'Type2' ,
1411
+ fields : {
1412
+ field1 : { type : GraphQLString } ,
1413
+ }
1414
+ } ) ;
1415
+
1416
+ const oldUnionType = new GraphQLUnionType ( {
1417
+ name : 'UnionType1' ,
1418
+ types : [ type1 ] ,
1419
+ resolveType : ( ) => null ,
1420
+ } ) ;
1421
+ const newUnionType = new GraphQLUnionType ( {
1422
+ name : 'UnionType1' ,
1423
+ types : [ type1a , type2 ] ,
1424
+ resolveType : ( ) => null ,
1425
+ } ) ;
1426
+
1427
+ const oldSchema = new GraphQLSchema ( {
1428
+ query : queryType ,
1429
+ types : [
1430
+ oldUnionType ,
1431
+ ]
1432
+ } ) ;
1433
+ const newSchema = new GraphQLSchema ( {
1434
+ query : queryType ,
1435
+ types : [
1436
+ newUnionType ,
1437
+ ]
1438
+ } ) ;
1439
+
1440
+ expect ( findTypesAddedToUnions ( oldSchema , newSchema ) ) . to . eql ( [
1441
+ {
1442
+ type : DangerousChangeType . TYPE_ADDED_TO_UNION ,
1443
+ description : 'Type2 was added to union type UnionType1.' ,
1444
+ } ,
1445
+ ] ) ;
1446
+ } ) ;
1447
+
1393
1448
it ( 'should find all dangerous changes' , ( ) => {
1394
1449
const enumThatGainsAValueOld = new GraphQLEnumType ( {
1395
1450
name : 'EnumType1' ,
@@ -1422,6 +1477,29 @@ describe('findDangerousChanges', () => {
1422
1477
} ,
1423
1478
} ) ;
1424
1479
1480
+ const typeInUnion1 = new GraphQLObjectType ( {
1481
+ name : 'TypeInUnion1' ,
1482
+ fields : {
1483
+ field1 : { type : GraphQLString } ,
1484
+ }
1485
+ } ) ;
1486
+ const typeInUnion2 = new GraphQLObjectType ( {
1487
+ name : 'TypeInUnion2' ,
1488
+ fields : {
1489
+ field1 : { type : GraphQLString } ,
1490
+ }
1491
+ } ) ;
1492
+ const unionTypeThatGainsATypeOld = new GraphQLUnionType ( {
1493
+ name : 'UnionTypeThatGainsAType' ,
1494
+ types : [ typeInUnion1 ] ,
1495
+ resolveType : ( ) => null ,
1496
+ } ) ;
1497
+ const unionTypeThatGainsATypeNew = new GraphQLUnionType ( {
1498
+ name : 'UnionTypeThatGainsAType' ,
1499
+ types : [ typeInUnion1 , typeInUnion2 ] ,
1500
+ resolveType : ( ) => null ,
1501
+ } ) ;
1502
+
1425
1503
const newType = new GraphQLObjectType ( {
1426
1504
name : 'Type1' ,
1427
1505
fields : {
@@ -1441,15 +1519,17 @@ describe('findDangerousChanges', () => {
1441
1519
query : queryType ,
1442
1520
types : [
1443
1521
oldType ,
1444
- enumThatGainsAValueOld
1522
+ enumThatGainsAValueOld ,
1523
+ unionTypeThatGainsATypeOld
1445
1524
]
1446
1525
} ) ;
1447
1526
1448
1527
const newSchema = new GraphQLSchema ( {
1449
1528
query : queryType ,
1450
1529
types : [
1451
1530
newType ,
1452
- enumThatGainsAValueNew
1531
+ enumThatGainsAValueNew ,
1532
+ unionTypeThatGainsATypeNew
1453
1533
]
1454
1534
} ) ;
1455
1535
@@ -1461,6 +1541,11 @@ describe('findDangerousChanges', () => {
1461
1541
{
1462
1542
description : 'VALUE2 was added to enum type EnumType1.' ,
1463
1543
type : 'VALUE_ADDED_TO_ENUM' ,
1544
+ } ,
1545
+ {
1546
+ type : DangerousChangeType . TYPE_ADDED_TO_UNION ,
1547
+ description : 'TypeInUnion2 was added to union type ' +
1548
+ 'UnionTypeThatGainsAType.' ,
1464
1549
}
1465
1550
] ;
1466
1551
0 commit comments