Skip to content

Commit c3fbc2d

Browse files
author
Winsor, Daniel
committed
Union types must have at least 1 member, and members can be only of Object types, not scalars etc.
1 parent 50a4225 commit c3fbc2d

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

src/main/java/graphql/Assert.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package graphql;
22

3+
import java.util.Collection;
34

45
public class Assert {
56

@@ -9,4 +10,9 @@ public static void assertNotNull(Object object, String errorMessage) {
910
throw new AssertException(errorMessage);
1011
}
1112

13+
public static void assertNotEmpty(Collection<?> c, String errorMessage) {
14+
if (c == null || c.isEmpty()) throw new AssertException(errorMessage);
15+
return;
16+
}
17+
1218
}

src/main/java/graphql/schema/GraphQLUnionType.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@
44
import java.util.ArrayList;
55
import java.util.List;
66

7-
import static graphql.Assert.assertNotNull;
7+
import static graphql.Assert.*;
88

99
public class GraphQLUnionType implements GraphQLType, GraphQLOutputType, GraphQLCompositeType, GraphQLUnmodifiedType, GraphQLNullableType {
1010

1111
private final String name;
1212
private final String description;
13-
private List<GraphQLType> types = new ArrayList<GraphQLType>();
13+
private List<GraphQLObjectType> types = new ArrayList<GraphQLObjectType>();
1414
private final TypeResolver typeResolver;
1515

1616

17-
public GraphQLUnionType(String name, String description, List<GraphQLType> types, TypeResolver typeResolver) {
17+
public GraphQLUnionType(String name, String description, List<GraphQLObjectType> types, TypeResolver typeResolver) {
1818
assertNotNull(name, "name can't be null");
1919
assertNotNull(types, "types can't be null");
20+
assertNotEmpty(types, "A Union type must define one or more member types.");
2021
assertNotNull(typeResolver, "typeResolver can't be null");
2122
this.name = name;
2223
this.description = description;
@@ -25,8 +26,8 @@ public GraphQLUnionType(String name, String description, List<GraphQLType> types
2526
}
2627

2728

28-
public List<GraphQLType> getTypes() {
29-
return new ArrayList<GraphQLType>(types);
29+
public List<GraphQLObjectType> getTypes() {
30+
return new ArrayList<GraphQLObjectType>(types);
3031
}
3132

3233
public TypeResolver getTypeResolver() {
@@ -49,7 +50,7 @@ public static Builder newUnionType() {
4950
public static class Builder {
5051
private String name;
5152
private String description;
52-
private List<GraphQLType> types = new ArrayList<GraphQLType>();
53+
private List<GraphQLObjectType> types = new ArrayList<GraphQLObjectType>();
5354
private TypeResolver typeResolver;
5455

5556
public Builder name(String name) {
@@ -69,14 +70,14 @@ public Builder typeResolver(TypeResolver typeResolver) {
6970
}
7071

7172

72-
public Builder possibleType(GraphQLType type) {
73+
public Builder possibleType(GraphQLObjectType type) {
7374
assertNotNull(type, "possible type can't be null");
7475
types.add(type);
7576
return this;
7677
}
7778

78-
public Builder possibleTypes(GraphQLType... type) {
79-
for (GraphQLType graphQLType : type) {
79+
public Builder possibleTypes(GraphQLObjectType... type) {
80+
for (GraphQLObjectType graphQLType : type) {
8081
possibleType(graphQLType);
8182
}
8283
return this;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package graphql.schema
2+
3+
import spock.lang.Specification
4+
5+
import graphql.AssertException
6+
import graphql.schema.TypeResolverProxy
7+
8+
import static graphql.schema.GraphQLUnionType.newUnionType
9+
10+
import static graphql.Scalars.GraphQLString
11+
12+
13+
class GraphQLUnionTypeTest extends Specification {
14+
15+
def "no possible types in union fails"() {
16+
when:
17+
newUnionType()
18+
.name("TestUnionType")
19+
.typeResolver(new TypeResolverProxy())
20+
.build();
21+
then:
22+
thrown(AssertException)
23+
}
24+
}

0 commit comments

Comments
 (0)