forked from graphql-java/graphql-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphQLSchema.java
More file actions
308 lines (260 loc) · 11.5 KB
/
GraphQLSchema.java
File metadata and controls
308 lines (260 loc) · 11.5 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package graphql.schema;
import graphql.Directives;
import graphql.PublicApi;
import graphql.schema.validation.InvalidSchemaException;
import graphql.schema.validation.SchemaValidationError;
import graphql.schema.validation.SchemaValidator;
import graphql.schema.visibility.GraphqlFieldVisibility;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import static graphql.Assert.assertNotNull;
import static graphql.Assert.assertShouldNeverHappen;
import static graphql.Assert.assertTrue;
import static graphql.schema.visibility.DefaultGraphqlFieldVisibility.DEFAULT_FIELD_VISIBILITY;
import static java.lang.String.format;
import static java.util.Arrays.asList;
/**
* The schema represents the combined type system of the graphql engine. This is how the engine knows
* what graphql queries represent what data.
*
* See http://graphql.org/learn/schema/#type-language for more details
*/
@PublicApi
public class GraphQLSchema {
private final GraphQLObjectType queryType;
private final GraphQLObjectType mutationType;
private final GraphQLObjectType subscriptionType;
private final Map<String, GraphQLType> typeMap;
private final Set<GraphQLType> additionalTypes;
private final Set<GraphQLDirective> directives;
private final GraphqlFieldVisibility fieldVisibility;
private final Map<String, List<GraphQLObjectType>> byInterface;
public GraphQLSchema(GraphQLObjectType queryType) {
this(queryType, null, Collections.emptySet());
}
public GraphQLSchema(GraphQLObjectType queryType, GraphQLObjectType mutationType, Set<GraphQLType> additionalTypes) {
this(queryType, mutationType, null, additionalTypes);
}
public GraphQLSchema(GraphQLObjectType queryType, GraphQLObjectType mutationType, GraphQLObjectType subscriptionType, Set<GraphQLType> additionalTypes) {
this(queryType, mutationType, subscriptionType, additionalTypes, Collections.emptySet(), DEFAULT_FIELD_VISIBILITY);
}
public GraphQLSchema(GraphQLObjectType queryType, GraphQLObjectType mutationType, GraphQLObjectType subscriptionType, Set<GraphQLType> additionalTypes, Set<GraphQLDirective> directives, GraphqlFieldVisibility fieldVisibility) {
assertNotNull(additionalTypes, "additionalTypes can't be null");
assertNotNull(queryType, "queryType can't be null");
assertNotNull(directives, "directives can't be null");
assertNotNull(fieldVisibility, "fieldVisibility can't be null");
SchemaUtil schemaUtil = new SchemaUtil();
this.queryType = queryType;
this.mutationType = mutationType;
this.subscriptionType = subscriptionType;
this.fieldVisibility = fieldVisibility;
this.additionalTypes = additionalTypes;
this.directives = new LinkedHashSet<>(
asList(Directives.IncludeDirective, Directives.SkipDirective, Directives.DeferDirective)
);
this.directives.addAll(directives);
this.typeMap = schemaUtil.allTypes(this, additionalTypes);
this.byInterface = schemaUtil.groupImplementations(this);
}
public Set<GraphQLType> getAdditionalTypes() {
return additionalTypes;
}
public GraphQLType getType(String typeName) {
return typeMap.get(typeName);
}
/**
* Called to return a named {@link graphql.schema.GraphQLObjectType} from the schema
*
* @param typeName the name of the type
*
* @return a graphql object type or null if there is one
*
* @throws graphql.GraphQLException if the type is NOT a object type
*/
public GraphQLObjectType getObjectType(String typeName) {
GraphQLType graphQLType = typeMap.get(typeName);
if (graphQLType != null) {
assertTrue(graphQLType instanceof GraphQLObjectType,
format("You have asked for named object type '%s' but its not an object type but rather a '%s'", typeName, graphQLType.getClass().getName()));
}
return (GraphQLObjectType) graphQLType;
}
public Map<String, GraphQLType> getTypeMap() {
return Collections.unmodifiableMap(typeMap);
}
public List<GraphQLType> getAllTypesAsList() {
return new ArrayList<>(typeMap.values());
}
/**
* This will return the list of {@link graphql.schema.GraphQLObjectType} types that implement the given
* interface type.
*
* @param type interface type to obtain implementations of.
*
* @return list of types implementing provided interface
*/
public List<GraphQLObjectType> getImplementations(GraphQLInterfaceType type) {
List<GraphQLObjectType> implementations = byInterface.get(type.getName());
return (implementations == null)
? Collections.emptyList()
: Collections.unmodifiableList(implementations);
}
/**
* Returns true if a specified concrete type is a possible type of a provided abstract type.
* If the provided abstract type is:
* - an interface, it checks whether the concrete type is one of its implementations.
* - a union, it checks whether the concrete type is one of its possible types.
*
* @param abstractType abstract type either interface or union
* @param concreteType concrete type
* @return true if possible type, false otherwise.
*/
public boolean isPossibleType(GraphQLType abstractType, GraphQLObjectType concreteType) {
if (abstractType instanceof GraphQLInterfaceType) {
return getImplementations((GraphQLInterfaceType) abstractType).stream()
.map(GraphQLType::getName)
.anyMatch(name -> concreteType.getName().equals(name));
} else if (abstractType instanceof GraphQLUnionType) {
return ((GraphQLUnionType) abstractType).getTypes().stream()
.map(GraphQLType::getName)
.anyMatch(name -> concreteType.getName().equals(name));
}
return assertShouldNeverHappen("Unsupported abstract type %s. Abstract types supported are Union and Interface.", abstractType.getName());
}
public GraphQLObjectType getQueryType() {
return queryType;
}
public GraphQLObjectType getMutationType() {
return mutationType;
}
public GraphQLObjectType getSubscriptionType() {
return subscriptionType;
}
public GraphqlFieldVisibility getFieldVisibility() {
return fieldVisibility;
}
public List<GraphQLDirective> getDirectives() {
return new ArrayList<>(directives);
}
public GraphQLDirective getDirective(String name) {
for (GraphQLDirective directive : getDirectives()) {
if (directive.getName().equals(name)) return directive;
}
return null;
}
public boolean isSupportingMutations() {
return mutationType != null;
}
public boolean isSupportingSubscriptions() {
return subscriptionType != null;
}
/**
* This helps you transform the current GraphQLSchema object into another one by starting a builder with all
* the current values and allows you to transform it how you want.
*
* @param builderConsumer the consumer code that will be given a builder to transform
*
* @return a new GraphQLSchema object based on calling build on that builder
*/
public GraphQLSchema transform(Consumer<Builder> builderConsumer) {
Builder builder = newSchema(this);
builderConsumer.accept(builder);
return builder.build();
}
/**
* @return a new schema builder
*/
public static Builder newSchema() {
return new Builder();
}
/**
* This allows you to build a schema from an existing schema. It copies everything from the existing
* schema and then allows you to replace them.
*
* @param existingSchema the existing schema
*
* @return a new schema builder
*/
public static Builder newSchema(GraphQLSchema existingSchema) {
return new Builder()
.query(existingSchema.getQueryType())
.mutation(existingSchema.getMutationType())
.subscription(existingSchema.getSubscriptionType())
.fieldVisibility(existingSchema.getFieldVisibility())
.additionalDirectives(existingSchema.directives)
.additionalTypes(existingSchema.additionalTypes);
}
public static class Builder {
private GraphQLObjectType queryType;
private GraphQLObjectType mutationType;
private GraphQLObjectType subscriptionType;
private GraphqlFieldVisibility fieldVisibility = DEFAULT_FIELD_VISIBILITY;
private Set<GraphQLType> additionalTypes = new HashSet<>();
private Set<GraphQLDirective> additionalDirectives = new HashSet<>();
public Builder query(GraphQLObjectType.Builder builder) {
return query(builder.build());
}
public Builder query(GraphQLObjectType queryType) {
this.queryType = queryType;
return this;
}
public Builder mutation(GraphQLObjectType.Builder builder) {
return mutation(builder.build());
}
public Builder mutation(GraphQLObjectType mutationType) {
this.mutationType = mutationType;
return this;
}
public Builder subscription(GraphQLObjectType.Builder builder) {
return subscription(builder.build());
}
public Builder subscription(GraphQLObjectType subscriptionType) {
this.subscriptionType = subscriptionType;
return this;
}
public Builder fieldVisibility(GraphqlFieldVisibility fieldVisibility) {
this.fieldVisibility = fieldVisibility;
return this;
}
public Builder additionalTypes(Set<GraphQLType> additionalTypes) {
this.additionalTypes.addAll(additionalTypes);
return this;
}
public Builder additionalType(GraphQLType additionalType) {
this.additionalTypes.add(additionalType);
return this;
}
public Builder additionalDirectives(Set<GraphQLDirective> additionalDirectives) {
this.additionalDirectives.addAll(additionalDirectives);
return this;
}
public Builder additionalDirective(GraphQLDirective additionalDirective) {
this.additionalDirectives.add(additionalDirective);
return this;
}
public GraphQLSchema build() {
return build(additionalTypes, additionalDirectives);
}
public GraphQLSchema build(Set<GraphQLType> additionalTypes) {
return build(additionalTypes, Collections.emptySet());
}
public GraphQLSchema build(Set<GraphQLType> additionalTypes, Set<GraphQLDirective> additionalDirectives) {
assertNotNull(additionalTypes, "additionalTypes can't be null");
assertNotNull(additionalDirectives, "additionalDirectives can't be null");
GraphQLSchema graphQLSchema = new GraphQLSchema(queryType, mutationType, subscriptionType, additionalTypes, additionalDirectives, fieldVisibility);
new SchemaUtil().replaceTypeReferences(graphQLSchema);
Collection<SchemaValidationError> errors = new SchemaValidator().validateSchema(graphQLSchema);
if (errors.size() > 0) {
throw new InvalidSchemaException(errors);
}
return graphQLSchema;
}
}
}