io.smallrye.graphql.schema.model.Schema Maven / Gradle / Ivy
Show all versions of smallrye-graphql-schema-model Show documentation
package io.smallrye.graphql.schema.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Represents a GraphQL Schema.
*
* This is the end result we are after and the object that will be passed to the
* implementation to create the actual endpoints and schema.
*
* @author Phillip Kruger ([email protected])
*/
public final class Schema implements Serializable {
private Set queries = new HashSet<>();
private Set mutations = new HashSet<>();
private Set subscriptions = new HashSet<>();
private Map> groupedQueries = new HashMap<>();
private Map> groupedMutations = new HashMap<>();
private Map> groupedSubscriptions = new HashMap<>();
private List directiveTypes = new ArrayList<>();
private Map inputs = new HashMap<>();
private Map types = new HashMap<>();
private Map interfaces = new HashMap<>();
private Map unions = new HashMap<>();
private Map enums = new HashMap<>();
private Map errors = new HashMap<>();
private Map wrappedDataFetchers = new HashMap<>();
private Map fieldDataFetchers = new HashMap<>();
public Schema() {
}
public Set getQueries() {
return queries;
}
public void setQueries(Set queries) {
this.queries = queries;
}
public void addQuery(Operation query) {
this.queries.add(query);
}
public boolean hasOperations() {
return hasQueries() || hasGroupedQueries() || hasMutations() || hasGroupedMutations();
}
public boolean hasQueries() {
return !this.queries.isEmpty();
}
public Set getMutations() {
return mutations;
}
public void setMutations(Set mutations) {
this.mutations = mutations;
}
public void addMutation(Operation mutation) {
this.mutations.add(mutation);
}
public boolean hasMutations() {
return !this.mutations.isEmpty();
}
public Set getSubscriptions() {
return subscriptions;
}
public void setSubscriptions(Set subscriptions) {
this.subscriptions = subscriptions;
}
public void addSubscription(Operation subscription) {
this.subscriptions.add(subscription);
}
public boolean hasSubscriptions() {
return !this.subscriptions.isEmpty();
}
public Map> getGroupedQueries() {
return groupedQueries;
}
public void setGroupedQueries(Map> groupedQueries) {
this.groupedQueries = groupedQueries;
}
public void addGroupedQuery(Group group, Operation query) {
addToOperationMap(this.groupedQueries, group, query);
}
public boolean hasGroupedQueries() {
return !this.groupedQueries.isEmpty();
}
public Map> getGroupedMutations() {
return groupedMutations;
}
public void setGroupedMutations(Map> groupedMutations) {
this.groupedMutations = groupedMutations;
}
public void addGroupedMutation(Group group, Operation mutation) {
addToOperationMap(this.groupedMutations, group, mutation);
}
public boolean hasGroupedMutations() {
return !this.groupedMutations.isEmpty();
}
public Map> getGroupedSubscriptions() {
return groupedSubscriptions;
}
public void setGroupedSubscriptions(Map> groupedSubscriptions) {
this.groupedSubscriptions = groupedSubscriptions;
}
public void addGroupedSubscription(Group group, Operation subscription) {
addToOperationMap(this.groupedSubscriptions, group, subscription);
}
public boolean hasGroupedSubscriptions() {
return !this.groupedSubscriptions.isEmpty();
}
public Map getInputs() {
return inputs;
}
public void setInputs(Map inputs) {
this.inputs = inputs;
}
public void addInput(InputType input) {
this.inputs.put(input.getName(), input);
}
public boolean containsInput(String name) {
return this.inputs.containsKey(name);
}
public boolean hasInputs() {
return !this.inputs.isEmpty();
}
public Map getTypes() {
return types;
}
public void setTypes(Map types) {
this.types = types;
}
public void addType(Type type) {
this.types.put(type.getName(), type);
}
public boolean containsType(String name) {
return this.types.containsKey(name);
}
public boolean hasTypes() {
return !this.types.isEmpty();
}
public Map getInterfaces() {
return interfaces;
}
public void setInterfaces(Map interfaces) {
this.interfaces = interfaces;
}
public void addInterface(Type interfaceType) {
if (interfaceType.getFields() != null && !interfaceType.getFields().isEmpty()) {
this.interfaces.put(interfaceType.getName(), interfaceType);
}
}
public boolean containsInterface(String name) {
return this.interfaces.containsKey(name);
}
public boolean hasInterfaces() {
return !this.interfaces.isEmpty();
}
public Map getUnions() {
return unions;
}
public void setUnions(Map unions) {
this.unions = unions;
}
public void addUnion(UnionType unionType) {
this.unions.put(unionType.getName(), unionType);
}
public boolean containsUnion(String name) {
return this.unions.containsKey(name);
}
public boolean hasUnions() {
return !this.unions.isEmpty();
}
public Map getEnums() {
return enums;
}
public void setEnums(Map enums) {
this.enums = enums;
}
public void addEnum(EnumType enumType) {
this.enums.put(enumType.getName(), enumType);
}
public boolean containsEnum(String name) {
return this.enums.containsKey(name);
}
public boolean hasEnums() {
return !this.enums.isEmpty();
}
public Map getErrors() {
return errors;
}
public void setErrors(Map errors) {
this.errors = errors;
}
public void addError(ErrorInfo error) {
this.errors.put(error.getClassName(), error);
}
public boolean containsError(String classname) {
return this.errors.containsKey(classname);
}
public boolean hasErrors() {
return !this.errors.isEmpty();
}
public Map getWrappedDataFetchers() {
return this.wrappedDataFetchers;
}
public void setWrappedDataFetchers(Map wrappedDataFetchers) {
this.wrappedDataFetchers = wrappedDataFetchers;
}
public void addWrappedDataFetcher(String forReturn, String className) {
this.wrappedDataFetchers.put(forReturn, className);
}
public boolean hasWrappedDataFetchers() {
return !this.wrappedDataFetchers.isEmpty();
}
public Map getFieldDataFetchers() {
return this.fieldDataFetchers;
}
public void setFieldDataFetchers(Map fieldDataFetchers) {
this.fieldDataFetchers = fieldDataFetchers;
}
public void addFieldDataFetcher(String forReturn, String className) {
this.fieldDataFetchers.put(forReturn, className);
}
public boolean hasFieldDataFetchers() {
return !this.fieldDataFetchers.isEmpty();
}
public List getBatchOperations() {
List batchOperations = new ArrayList<>();
if (hasTypes()) {
for (Type type : this.types.values()) {
if (type.hasBatchOperations()) {
batchOperations.addAll(type.getBatchOperations().values());
}
}
}
return batchOperations;
}
private void addToOperationMap(Map> map, Group group, Operation query) {
Set set;
if (map.containsKey(group)) {
set = map.get(group);
} else {
set = new HashSet<>();
}
set.add(query);
map.put(group, set);
}
public List getDirectiveTypes() {
return directiveTypes;
}
public void setDirectiveTypes(List directiveTypes) {
this.directiveTypes = directiveTypes;
}
public void addDirectiveType(DirectiveType directiveType) {
directiveTypes.add(directiveType);
}
public boolean hasDirectiveTypes() {
return !directiveTypes.isEmpty();
}
@Override
public String toString() {
return "Schema{" + "queries=" + queries + ", mutations=" + mutations + ", subscriptions=" + subscriptions
+ ", groupedQueries=" + groupedQueries + ", groupedMutations=" + groupedMutations + ", groupedSubscriptions="
+ groupedSubscriptions + ", directiveTypes=" + directiveTypes + ", inputs=" + inputs + ", types=" + types
+ ", interfaces=" + interfaces + ", enums=" + enums + ", errors=" + errors + '}';
}
}