All Downloads are FREE. Search and download functionalities are using the official Maven repository.

graphql.schema.GraphQLArgument Maven / Gradle / Ivy

The newest version!
package graphql.schema;


import graphql.DirectivesUtil;
import graphql.PublicApi;
import graphql.language.InputValueDefinition;
import graphql.language.Value;
import graphql.util.TraversalControl;
import graphql.util.TraverserContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import static graphql.Assert.assertNotNull;
import static graphql.Assert.assertValidName;

/**
 * This defines an argument that can be supplied to a graphql field (via {@link graphql.schema.GraphQLFieldDefinition}.
 * 

* Fields can be thought of as "functions" that take arguments and return a value. *

* See http://graphql.org/learn/queries/#arguments for more details on the concept. *

* {@link graphql.schema.GraphQLArgument} is used in two contexts, one context is graphql queries where it represents the arguments that can be * set on a field and the other is in Schema Definition Language (SDL) where it can be used to represent the argument value instances * that have been supplied on a {@link graphql.schema.GraphQLDirective}. *

* The difference is the 'value' and 'defaultValue' properties. In a query argument, the 'value' is never in the GraphQLArgument * object but rather in the AST direct or in the query variables map and the 'defaultValue' represents a value to use if both of these are * not present. You can think of them like a descriptor of what shape an argument might have. *

* However with directives on SDL elements, the value is specified in AST only and transferred into the GraphQLArgument object and the * 'defaultValue' comes instead from the directive definition elsewhere in the SDL. You can think of them as 'instances' of arguments, their shape and their * specific value on that directive. */ @PublicApi public class GraphQLArgument implements GraphQLNamedSchemaElement, GraphQLInputValueDefinition { private final String name; private final String description; private final String deprecationReason; private final GraphQLInputType originalType; private final InputValueWithState defaultValue; private final InputValueWithState value; private final InputValueDefinition definition; private final DirectivesUtil.DirectivesHolder directives; private GraphQLInputType replacedType; public static final String CHILD_DIRECTIVES = "directives"; public static final String CHILD_TYPE = "type"; private GraphQLArgument(String name, String description, GraphQLInputType type, InputValueWithState defaultValue, InputValueWithState value, InputValueDefinition definition, List directives, String deprecationReason) { assertValidName(name); assertNotNull(type, () -> "type can't be null"); this.name = name; this.description = description; this.originalType = type; this.defaultValue = defaultValue; this.value = value; this.definition = definition; this.deprecationReason = deprecationReason; this.directives = new DirectivesUtil.DirectivesHolder(directives); } void replaceType(GraphQLInputType type) { this.replacedType = type; } @Override public String getName() { return name; } public GraphQLInputType getType() { return replacedType != null ? replacedType : originalType; } /** * The default value of this argument. * * @return */ public @NotNull InputValueWithState getArgumentDefaultValue() { return defaultValue; } public boolean hasSetDefaultValue() { return defaultValue.isSet(); } public boolean hasSetValue() { return value.isSet(); } /** * This is only used for applied directives. */ public @NotNull InputValueWithState getArgumentValue() { return value; } public String getDescription() { return description; } public String getDeprecationReason() { return deprecationReason; } public boolean isDeprecated() { return deprecationReason != null; } public InputValueDefinition getDefinition() { return definition; } @Override public List getDirectives() { return directives.getDirectives(); } @Override public Map getDirectivesByName() { return directives.getDirectivesByName(); } @Override public Map> getAllDirectivesByName() { return directives.getAllDirectivesByName(); } @Override public GraphQLDirective getDirective(String directiveName) { return directives.getDirective(directiveName); } @Override public List getChildren() { List children = new ArrayList<>(); children.add(getType()); children.addAll(directives.getDirectives()); return children; } @Override public SchemaElementChildrenContainer getChildrenWithTypeReferences() { return SchemaElementChildrenContainer.newSchemaElementChildrenContainer() .children(CHILD_DIRECTIVES, directives.getDirectives()) .child(CHILD_TYPE, originalType) .build(); } @Override public GraphQLArgument withNewChildren(SchemaElementChildrenContainer newChildren) { return transform(builder -> builder.type(newChildren.getChildOrNull(CHILD_TYPE)) .replaceDirectives(newChildren.getChildren(CHILD_DIRECTIVES))); } @Override public GraphQLSchemaElement copy() { return newArgument(this).build(); } /** * {@inheritDoc} */ @Override public final boolean equals(Object o) { return super.equals(o); } /** * {@inheritDoc} */ @Override public final int hashCode() { return super.hashCode(); } /** * This helps you transform the current GraphQLArgument 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 GraphQLArgument transform(Consumer builderConsumer) { Builder builder = newArgument(this); builderConsumer.accept(builder); return builder.build(); } public static Builder newArgument() { return new Builder(); } public static Builder newArgument(GraphQLArgument existing) { return new Builder(existing); } @Override public TraversalControl accept(TraverserContext context, GraphQLTypeVisitor visitor) { return visitor.visitGraphQLArgument(this, context); } @Override public String toString() { return "GraphQLArgument{" + "name='" + name + '\'' + ", value=" + value + ", defaultValue=" + defaultValue + ", type=" + getType() + '}'; } public static class Builder extends GraphqlTypeBuilder { private GraphQLInputType type; private InputValueWithState defaultValue = InputValueWithState.NOT_SET; private InputValueWithState value = InputValueWithState.NOT_SET; private String deprecationReason; private InputValueDefinition definition; private final List directives = new ArrayList<>(); public Builder() { } public Builder(GraphQLArgument existing) { this.name = existing.getName(); this.type = existing.originalType; this.value = existing.getArgumentValue(); this.defaultValue = existing.defaultValue; this.description = existing.getDescription(); this.definition = existing.getDefinition(); this.deprecationReason = existing.deprecationReason; DirectivesUtil.enforceAddAll(this.directives, existing.getDirectives()); } @Override public Builder name(String name) { super.name(name); return this; } @Override public Builder description(String description) { super.description(description); return this; } @Override public Builder comparatorRegistry(GraphqlTypeComparatorRegistry comparatorRegistry) { super.comparatorRegistry(comparatorRegistry); return this; } public Builder definition(InputValueDefinition definition) { this.definition = definition; return this; } public Builder deprecate(String deprecationReason) { this.deprecationReason = deprecationReason; return this; } public Builder type(GraphQLInputType type) { this.type = type; return this; } /** * @param defaultValue * * @return * * @deprecated use {@link #defaultValueLiteral(Value)} or {@link #defaultValueProgrammatic(Object)} */ @Deprecated public Builder defaultValue(Object defaultValue) { this.defaultValue = InputValueWithState.newInternalValue(defaultValue); return this; } /** * @param defaultValue can't be null as a `null` is represented a @{@link graphql.language.NullValue} Literal * * @return */ public Builder defaultValueLiteral(@NotNull Value defaultValue) { this.defaultValue = InputValueWithState.newLiteralValue(defaultValue); return this; } /** * @param defaultValue Can be null to represent null value * * @return */ public Builder defaultValueProgrammatic(@Nullable Object defaultValue) { this.defaultValue = InputValueWithState.newExternalValue(defaultValue); return this; } /** * Removes the defaultValue to represent a missing default value (which is different from null) * * @return */ public Builder clearDefaultValue() { this.defaultValue = InputValueWithState.NOT_SET; return this; } /** * @param value * * @return * * @deprecated use {@link #valueLiteral(Value)} or {@link #valueProgrammatic(Object)} */ @Deprecated public Builder value(@Nullable Object value) { this.value = InputValueWithState.newInternalValue(value); return this; } /** * @param value can't be null as a `null` is represented a @{@link graphql.language.NullValue} Literal */ public Builder valueLiteral(@NotNull Value value) { this.value = InputValueWithState.newLiteralValue(value); return this; } /** * @param value * * @return values can be null to represent null value */ public Builder valueProgrammatic(@Nullable Object value) { this.value = InputValueWithState.newExternalValue(value); return this; } /** * Removes the value to represent a missing value (which is different from null) * * @return */ public Builder clearValue() { this.value = InputValueWithState.NOT_SET; return this; } public Builder withDirectives(GraphQLDirective... directives) { assertNotNull(directives, () -> "directives can't be null"); this.directives.clear(); for (GraphQLDirective directive : directives) { withDirective(directive); } return this; } public Builder withDirective(GraphQLDirective directive) { assertNotNull(directive, () -> "directive can't be null"); DirectivesUtil.enforceAdd(this.directives, directive); return this; } public Builder replaceDirectives(List directives) { assertNotNull(directives, () -> "directive can't be null"); this.directives.clear(); DirectivesUtil.enforceAddAll(this.directives, directives); 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 GraphQLArgument build() { assertNotNull(type, () -> "type can't be null"); return new GraphQLArgument( name, description, type, defaultValue, value, definition, sort(directives, GraphQLArgument.class, GraphQLDirective.class), deprecationReason ); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy