@@ -1051,7 +1051,9 @@ InterfaceTypeDefinition :
1051
1051
1052
1052
GraphQL interfaces represent a list of named fields and their arguments . GraphQL
1053
1053
objects and interfaces can then implement these interfaces which requires that
1054
- the implementing type will define all fields defined by those interfaces .
1054
+ the implementing type will define all fields defined by those interfaces . Unions
1055
+ can also implement interfaces , as long as each union member implements those
1056
+ interfaces .
1055
1057
1056
1058
Fields on a GraphQL interface have the same rules as fields on a GraphQL object ;
1057
1059
their type can be Scalar , Object , Enum , Interface , or Union , or any wrapping
@@ -1144,9 +1146,9 @@ interface. Querying for `age` is only valid when the result of `entity` is a
1144
1146
**Interfaces Implementing Interfaces **
1145
1147
1146
1148
When defining an interface that implements another interface , the implementing
1147
- interface must define each field that is specified by the implemented interface .
1148
- For example , the interface Resource must define the field id to implement the
1149
- Node interface :
1149
+ type must define each field that is specified by the implemented interface . For
1150
+ example , the interface Resource must define the field id to implement the Node
1151
+ interface :
1150
1152
1151
1153
```raw graphql example
1152
1154
interface Node {
@@ -1160,9 +1162,8 @@ interface Resource implements Node {
1160
1162
```
1161
1163
1162
1164
Transitively implemented interfaces (interfaces implemented by the interface
1163
- that is being implemented) must also be defined on an implementing type or
1164
- interface . For example , `Image ` cannot implement `Resource ` without also
1165
- implementing `Node `:
1165
+ that is being implemented) must also be defined on the implementing type . For
1166
+ example , `Image ` cannot implement `Resource ` without also implementing `Node `:
1166
1167
1167
1168
```raw graphql example
1168
1169
interface Node {
@@ -1181,6 +1182,8 @@ interface Image implements Resource & Node {
1181
1182
}
1182
1183
```
1183
1184
1185
+ Similar syntax for unions implementing interfaces is detailed below .
1186
+
1184
1187
Interface definitions must not contain cyclic references nor implement
1185
1188
themselves . This example is invalid because `Node ` and `Named ` implement
1186
1189
themselves and each other :
@@ -1293,8 +1296,8 @@ defined.
1293
1296
1294
1297
## Unions
1295
1298
1296
- UnionTypeDefinition : Description ? union Name Directives [ Const ] ?
1297
- UnionMemberTypes ?
1299
+ UnionTypeDefinition : Description ? union Name ImplementsInterfaces ?
1300
+ Directives [ Const ]? UnionMemberTypes ?
1298
1301
1299
1302
UnionMemberTypes :
1300
1303
@@ -1370,6 +1373,50 @@ union SearchResult =
1370
1373
| Person
1371
1374
```
1372
1375
1376
+ **Unions Implementing Interfaces **
1377
+
1378
+ When defining unions that implement interfaces , each union member must
1379
+ explicitly implement each interface implemented by the union . For example , the
1380
+ member types of union SearchResult must each explicitly implement the Resource
1381
+ interface :
1382
+
1383
+ ```raw graphql example
1384
+ interface Resource {
1385
+ url : String
1386
+ }
1387
+
1388
+ union SearchResult implements Resource = Photo | Article
1389
+
1390
+ type Article implements Resource {
1391
+ url : String
1392
+ title : String
1393
+ }
1394
+
1395
+ type Image implements Resource {
1396
+ url : String
1397
+ height : Int
1398
+ width : Int
1399
+ }
1400
+ ```
1401
+
1402
+ Transitively implemented interfaces (interfaces implemented by the interface
1403
+ that is being implemented) must also be defined on the implementing union . For
1404
+ example , `SearchResult ` cannot implement `Resource ` without also implementing
1405
+ `Node `:
1406
+
1407
+ ```raw graphql example
1408
+ interface Node {
1409
+ id : ID !
1410
+ }
1411
+
1412
+ interface Resource implements Node {
1413
+ id : ID !
1414
+ url : String
1415
+ }
1416
+
1417
+ union SearchResult implements Resource & Node = Photo | Article
1418
+ ```
1419
+
1373
1420
**Result Coercion **
1374
1421
1375
1422
The union type should have some way of determining which object a given result
@@ -1388,13 +1435,21 @@ Union types have the potential to be invalid if incorrectly defined.
1388
1435
2. The member types of a Union type must all be Object base types ; Scalar ,
1389
1436
Interface and Union types must not be member types of a Union . Similarly ,
1390
1437
wrapping types must not be member types of a Union .
1438
+ 3. An union type may declare that it implements one or more unique interfaces .
1439
+ 4. Each member of a union must be a super -set of all union -implemented
1440
+ interfaces :
1441
+ 1. Let this union type be {implementingType }.
1442
+ 2. For each member type of {implementingType }:
1443
+ 1. Let this member type be {memberType }.
1444
+ 2. For each interface declared implemented as {implementedType },
1445
+ {IsValidImplementation (memberType, implementedType)} must be {true }.
1391
1446
1392
1447
### Union Extensions
1393
1448
1394
1449
UnionTypeExtension :
1395
1450
1396
- - extend union Name Directives [Const ]? UnionMemberTypes
1397
- - extend union Name Directives [Const ]
1451
+ - extend union Name ImplementsInterfaces ? Directives [Const ]? UnionMemberTypes
1452
+ - extend union Name ImplementsInterfaces ? Directives [Const ]
1398
1453
1399
1454
Union type extensions are used to represent a union type which has been extended
1400
1455
from some original union type . For example , this might be used to represent
@@ -1414,6 +1469,8 @@ Union type extensions have the potential to be invalid if incorrectly defined.
1414
1469
the original Union type .
1415
1470
5. Any non -repeatable directives provided must not already apply to the original
1416
1471
Union type .
1472
+ 6. All member types of the resulting extended Union type must be a super -set of
1473
+ all Interfaces it implements .
1417
1474
1418
1475
## Enums
1419
1476
0 commit comments