forked from graphql-java/graphql-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphQLUnionType.java
More file actions
172 lines (140 loc) · 5.58 KB
/
Copy pathGraphQLUnionType.java
File metadata and controls
172 lines (140 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package graphql.schema;
import graphql.Internal;
import graphql.PublicApi;
import graphql.language.UnionTypeDefinition;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static graphql.Assert.assertNotEmpty;
import static graphql.Assert.assertNotNull;
import static graphql.Assert.assertValidName;
import static java.util.Collections.emptyList;
/**
* A union type is a polymorphic type that dynamically represents one of more concrete object types.
*
* At runtime a {@link graphql.schema.TypeResolver} is used to take an union object value and decide what {@link graphql.schema.GraphQLObjectType}
* represents this union of types.
*
* Note that members of a union type need to be concrete object types; you can't create a union type out of interfaces or other unions.
*
* See http://graphql.org/learn/schema/#union-types for more details on the concept.
*/
@PublicApi
public class GraphQLUnionType implements GraphQLType, GraphQLOutputType, GraphQLCompositeType, GraphQLUnmodifiedType, GraphQLNullableType, GraphQLDirectiveContainer {
private final String name;
private final String description;
private List<GraphQLOutputType> types = new ArrayList<>();
private final TypeResolver typeResolver;
private final UnionTypeDefinition definition;
private final List<GraphQLDirective> directives;
@Internal
public GraphQLUnionType(String name, String description, List<GraphQLOutputType> types, TypeResolver typeResolver) {
this(name, description, types, typeResolver, emptyList(), null);
}
@Internal
public GraphQLUnionType(String name, String description, List<GraphQLOutputType> types, TypeResolver typeResolver, List<GraphQLDirective> directives, UnionTypeDefinition definition) {
assertValidName(name);
assertNotNull(types, "types can't be null");
assertNotEmpty(types, "A Union type must define one or more member types.");
assertNotNull(typeResolver, "typeResolver can't be null");
assertNotNull(directives, "directives cannot be null");
this.name = name;
this.description = description;
this.types = types;
this.typeResolver = typeResolver;
this.definition = definition;
this.directives = directives;
}
void replaceTypeReferences(Map<String, GraphQLType> typeMap) {
this.types = this.types.stream()
.map(type -> (GraphQLOutputType) new SchemaUtil().resolveTypeReference(type, typeMap))
.collect(Collectors.toList());
}
/**
* @return This returns GraphQLObjectType or GraphQLTypeReference instances, if the type
* references are not resolved yet. After they are resolved it contains only GraphQLObjectType.
* Reference resolving happens when a full schema is built.
*/
public List<GraphQLOutputType> getTypes() {
return new ArrayList<>(types);
}
public TypeResolver getTypeResolver() {
return typeResolver;
}
@Override
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public UnionTypeDefinition getDefinition() {
return definition;
}
@Override
public List<GraphQLDirective> getDirectives() {
return new ArrayList<>(directives);
}
public static Builder newUnionType() {
return new Builder();
}
@PublicApi
public static class Builder {
private String name;
private String description;
private final List<GraphQLOutputType> types = new ArrayList<>();
private TypeResolver typeResolver;
private UnionTypeDefinition definition;
private final List<GraphQLDirective> directives = new ArrayList<>();
public Builder name(String name) {
this.name = name;
return this;
}
public Builder description(String description) {
this.description = description;
return this;
}
public Builder definition(UnionTypeDefinition definition) {
this.definition = definition;
return this;
}
public Builder typeResolver(TypeResolver typeResolver) {
this.typeResolver = typeResolver;
return this;
}
public Builder possibleType(GraphQLObjectType type) {
assertNotNull(type, "possible type can't be null");
types.add(type);
return this;
}
public Builder possibleType(GraphQLTypeReference reference) {
assertNotNull(reference, "reference can't be null");
types.add(reference);
return this;
}
public Builder possibleTypes(GraphQLObjectType... type) {
for (GraphQLObjectType graphQLType : type) {
possibleType(graphQLType);
}
return this;
}
public Builder possibleTypes(GraphQLTypeReference... references) {
for (GraphQLTypeReference reference : references) {
possibleType(reference);
}
return this;
}
public boolean containType(String name) {
return types.stream().anyMatch(type -> type.getName().equals(name));
}
public Builder withDirectives(GraphQLDirective... directives) {
Collections.addAll(this.directives, directives);
return this;
}
public GraphQLUnionType build() {
return new GraphQLUnionType(name, description, types, typeResolver, directives, definition);
}
}
}