Skip to content

Commit f973735

Browse files
committed
Add spec text for unions implementing interfaces
1 parent 78ccda7 commit f973735

File tree

1 file changed

+68
-11
lines changed

1 file changed

+68
-11
lines changed

spec/Section 3 -- Type System.md

+68-11
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,9 @@ InterfaceTypeDefinition :
10511051

10521052
GraphQL interfaces represent a list of named fields and their arguments. GraphQL
10531053
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.
10551057

10561058
Fields on a GraphQL interface have the same rules as fields on a GraphQL object;
10571059
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
11441146
**Interfaces Implementing Interfaces**
11451147

11461148
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:
11501152

11511153
```raw graphql example
11521154
interface Node {
@@ -1160,9 +1162,8 @@ interface Resource implements Node {
11601162
```
11611163

11621164
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`:
11661167

11671168
```raw graphql example
11681169
interface Node {
@@ -1181,6 +1182,8 @@ interface Image implements Resource & Node {
11811182
}
11821183
```
11831184

1185+
Similar syntax for unions implementing interfaces is detailed below.
1186+
11841187
Interface definitions must not contain cyclic references nor implement
11851188
themselves. This example is invalid because `Node` and `Named` implement
11861189
themselves and each other:
@@ -1293,8 +1296,8 @@ defined.
12931296

12941297
## Unions
12951298

1296-
UnionTypeDefinition : Description? union Name Directives[Const]?
1297-
UnionMemberTypes?
1299+
UnionTypeDefinition : Description? union Name ImplementsInterfaces?
1300+
Directives[Const]? UnionMemberTypes?
12981301

12991302
UnionMemberTypes :
13001303

@@ -1370,6 +1373,50 @@ union SearchResult =
13701373
| Person
13711374
```
13721375

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+
13731420
**Result Coercion**
13741421

13751422
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.
13881435
2. The member types of a Union type must all be Object base types; Scalar,
13891436
Interface and Union types must not be member types of a Union. Similarly,
13901437
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}.
13911446

13921447
### Union Extensions
13931448

13941449
UnionTypeExtension :
13951450

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]
13981453

13991454
Union type extensions are used to represent a union type which has been extended
14001455
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.
14141469
the original Union type.
14151470
5. Any non-repeatable directives provided must not already apply to the original
14161471
Union type.
1472+
6. All member types of the resulting extended Union type must be a super-set of
1473+
all Interfaces it implements.
14171474

14181475
## Enums
14191476

0 commit comments

Comments
 (0)