graphql.schema.GraphQLSchema Maven / Gradle / Ivy
The newest version!
package graphql.schema;
import graphql.Directives;
import graphql.schema.validation.InvalidSchemaException;
import graphql.schema.validation.ValidationError;
import graphql.schema.validation.Validator;
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 static graphql.Assert.assertNotNull;
public class GraphQLSchema {
private final GraphQLObjectType queryType;
private final GraphQLObjectType mutationType;
private final GraphQLObjectType subscriptionType;
private final Map typeMap;
private Set additionalTypes;
public GraphQLSchema(GraphQLObjectType queryType) {
this(queryType, null, Collections.emptySet());
}
public GraphQLSchema(GraphQLObjectType queryType, GraphQLObjectType mutationType, Set additionalTypes) {
this(queryType, mutationType, null, additionalTypes);
}
public GraphQLSchema(GraphQLObjectType queryType, GraphQLObjectType mutationType, GraphQLObjectType subscriptionType, Set dictionary) {
assertNotNull(dictionary, "dictionary can't be null");
assertNotNull(queryType, "queryType can't be null");
this.queryType = queryType;
this.mutationType = mutationType;
this.subscriptionType = subscriptionType;
this.additionalTypes = dictionary;
typeMap = new SchemaUtil().allTypes(this, dictionary);
}
public Set getAdditionalTypes() {
return additionalTypes;
}
public GraphQLType getType(String typeName) {
return typeMap.get(typeName);
}
public List getAllTypesAsList() {
return new ArrayList<>(typeMap.values());
}
public GraphQLObjectType getQueryType() {
return queryType;
}
public GraphQLObjectType getMutationType() {
return mutationType;
}
public GraphQLObjectType getSubscriptionType() {
return subscriptionType;
}
public List 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;
private GraphQLObjectType subscriptionType;
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 GraphQLSchema build() {
return build(Collections.emptySet());
}
public GraphQLSchema build(Set additionalTypes) {
assertNotNull(additionalTypes, "additionalTypes can't be null");
GraphQLSchema graphQLSchema = new GraphQLSchema(queryType, mutationType, subscriptionType, additionalTypes);
new SchemaUtil().replaceTypeReferences(graphQLSchema);
Collection errors = new Validator().validateSchema(graphQLSchema);
if (errors.size() > 0) {
throw new InvalidSchemaException(errors);
}
return graphQLSchema;
}
}
}