Repository navigation
Expand file tree
/
Copy pathGraphQLSchema.java
More file actions
118 lines (92 loc) · 3.47 KB
/
Copy pathGraphQLSchema.java
File metadata and controls
118 lines (92 loc) · 3.47 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
package graphql.schema;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import graphql.Assert;
import graphql.Directives;
import graphql.schema.validation.InvalidSchemaException;
import graphql.schema.validation.ValidationError;
import graphql.schema.validation.Validator;
import static graphql.Assert.assertNotNull;
public class GraphQLSchema {
private final GraphQLObjectType queryType;
private final GraphQLObjectType mutationType;
private final Map<String, GraphQLType> typeMap;
private Set<GraphQLType> dictionary;
public GraphQLSchema(GraphQLObjectType queryType) {
this(queryType, null, Collections.<GraphQLType>emptySet());
}
public Set<GraphQLType> getDictionary() {
return dictionary;
}
public GraphQLSchema(GraphQLObjectType queryType, GraphQLObjectType mutationType, Set<GraphQLType> dictionary) {
assertNotNull(dictionary, "dictionary can't be null");
assertNotNull(queryType, "queryType can't be null");
this.queryType = queryType;
this.mutationType = mutationType;
this.dictionary = dictionary;
typeMap = new SchemaUtil().allTypes(this, dictionary);
}
public GraphQLType getType(String typeName) {
return typeMap.get(typeName);
}
public List<GraphQLType> getAllTypesAsList() {
return new ArrayList<GraphQLType>(typeMap.values());
}
public GraphQLObjectType getQueryType() {
return queryType;
}
public GraphQLObjectType getMutationType() {
return mutationType;
}
public List<GraphQLDirective> getDirectives() {
return Arrays.asList(Directives.IncludeDirective, Directives.SkipDirective);
}
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 static Builder newSchema() {
return new Builder();
}
public static class Builder {
private GraphQLObjectType queryType;
private GraphQLObjectType mutationType;
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 GraphQLSchema build() {
return build(Collections.<GraphQLType>emptySet());
}
public GraphQLSchema build(Set<GraphQLType> dictionary) {
Assert.assertNotNull(dictionary, "dictionary can't be null");
GraphQLSchema graphQLSchema = new GraphQLSchema(queryType, mutationType, dictionary);
new SchemaUtil().replaceTypeReferences(graphQLSchema);
Collection<ValidationError> errors = new Validator().validateSchema(graphQLSchema);
if (errors.size() > 0) {
throw new InvalidSchemaException(errors);
}
return graphQLSchema;
}
}
}