graphql.schema.GraphQLEnumValueDefinition Maven / Gradle / Ivy
package graphql.schema;
import graphql.DirectivesUtil;
import graphql.Internal;
import graphql.PublicApi;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import static graphql.Assert.assertNotNull;
import static graphql.Assert.assertValidName;
import static graphql.util.FpKit.getByName;
import static graphql.util.FpKit.valuesToList;
import static java.util.Collections.emptyList;
/**
* A graphql enumeration type has a limited set of values and this defines one of those unique values
*
* See http://graphql.org/learn/schema/#enumeration-types for more details
*
* @see graphql.schema.GraphQLEnumType
*/
@PublicApi
public class GraphQLEnumValueDefinition implements GraphQLDirectiveContainer {
private final String name;
private final String description;
private final Object value;
private final String deprecationReason;
private final List directives;
@Internal
public GraphQLEnumValueDefinition(String name, String description, Object value) {
this(name, description, value, null, emptyList());
}
@Internal
public GraphQLEnumValueDefinition(String name, String description, Object value, String deprecationReason) {
this(name, description, value, deprecationReason, emptyList());
}
@Internal
public GraphQLEnumValueDefinition(String name, String description, Object value, String deprecationReason, List directives) {
assertValidName(name);
assertNotNull(directives, "directives cannot be null");
this.name = name;
this.description = description;
this.value = value;
this.deprecationReason = deprecationReason;
this.directives = directives;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public Object getValue() {
return value;
}
public boolean isDeprecated() {
return deprecationReason != null;
}
public String getDeprecationReason() {
return deprecationReason;
}
public List getDirectives() {
return new ArrayList<>(directives);
}
public Map getDirectivesByName() {
return DirectivesUtil.directivesByName(directives);
}
public GraphQLDirective getDirective(String directiveName) {
return getDirectivesByName().get(directiveName);
}
/**
* This helps you transform the current GraphQLEnumValueDefinition 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 field based on calling build on that builder
*/
public GraphQLEnumValueDefinition transform(Consumer builderConsumer) {
Builder builder = newEnumValueDefinition(this);
builderConsumer.accept(builder);
return builder.build();
}
public static Builder newEnumValueDefinition() {
return new Builder();
}
public static Builder newEnumValueDefinition(GraphQLEnumValueDefinition existing) {
return new Builder(existing);
}
@PublicApi
public static class Builder {
private String name;
private String description;
private Object value;
private String deprecationReason;
private final Map directives = new LinkedHashMap<>();
public Builder() {
}
public Builder(GraphQLEnumValueDefinition existing) {
this.name = existing.getName();
this.description = existing.getDescription();
this.value = existing.getValue();
this.deprecationReason = existing.getDeprecationReason();
this.directives.putAll(getByName(existing.getDirectives(), GraphQLDirective::getName));
}
public Builder name(String name) {
this.name = name;
return this;
}
public Builder description(String description) {
this.description = description;
return this;
}
public Builder value(Object value) {
this.value = value;
return this;
}
public Builder deprecationReason(String deprecationReason) {
this.deprecationReason = deprecationReason;
return this;
}
public Builder withDirectives(GraphQLDirective... directives) {
assertNotNull(directives, "directives can't be null");
for (GraphQLDirective directive : directives) {
withDirective(directive);
}
return this;
}
public Builder withDirective(GraphQLDirective directive) {
assertNotNull(directive, "directive can't be null");
directives.put(directive.getName(), directive);
return this;
}
public Builder withDirective(GraphQLDirective.Builder builder) {
return withDirective(builder.build());
}
/**
* This is used to clear all the directives in the builder so far.
*
* @return the builder
*/
public Builder clearDirectives() {
directives.clear();
return this;
}
public GraphQLEnumValueDefinition build() {
return new GraphQLEnumValueDefinition(name, description, value, deprecationReason, valuesToList(directives));
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy