Skip to content

Commit b03097a

Browse files
committed
Remove attributes from Argument annotation
Prior to this commit, the `@Argument` annotation would support two attributes: `required` and `defaultValue`, specifying whether arguments should be considered as required and default values to use if they're not present. This can be problematic as this can already be specified at the schema level. To avoid duplication and invalid setups betweens schema and Controllers, this commit removes those attributes in favor of the schema definition. Closes gh-150
1 parent e0952a2 commit b03097a

File tree

5 files changed

+9
-63
lines changed

5 files changed

+9
-63
lines changed
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
management.endpoints.web.exposure.include=health,metrics,info
22

33
spring.graphql.schema.printer.enabled=true
4-
5-
spring.graph

spring-graphql-docs/src/docs/asciidoc/index.adoc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -577,8 +577,7 @@ values. The arguments are available as simple scalar values such as String, or a
577577
of values for more complex input, or a `List` of values.
578578

579579
Use `@Argument` to access an argument for the field that maps to the handler method. You
580-
can declare such a method parameter to be of any type. If necessary, Spring GraphQL
581-
converts the value by serializing it to JSON first and then to the target type.
580+
can declare such a method parameter to be of any type.
582581

583582
[source,java,indent=0,subs="verbatim,quotes"]
584583
----
@@ -601,8 +600,8 @@ You can explicitly specify the argument name, for example `@Argument("bookInput"
601600
it not specified, it defaults to the method parameter name, but this requires the
602601
`-parameters` compiler flag with Java 8+ or debugging information from the compiler.
603602

604-
By default, an `@Argument` is required, but you can make it optional by setting the
605-
`required` flag to false or by declaring the argument with `java.util.Optional`.
603+
The "required" character of an `@Argument` or its default value is controlled at the schema
604+
level.
606605

607606
You can use `@Argument` on a `Map<String, Object>` argument, to obtain all argument
608607
values. The name attribute on `@Argument` must not be set.

spring-graphql/src/main/java/org/springframework/graphql/data/method/annotation/Argument.java

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,20 @@
2121
import java.lang.annotation.RetentionPolicy;
2222
import java.lang.annotation.Target;
2323

24-
2524
import org.springframework.core.annotation.AliasFor;
2625

2726
/**
2827
* Annotation to bind a method parameter to a GraphQL input
2928
* {@link graphql.schema.DataFetchingEnvironment#getArgument(String) argument}.
3029
*
31-
* <p>If the method parameter is {@link java.util.Map Map&lt;String, Object&gt;} or
30+
* <p>If the method parameter is {@link java.util.Map Map&lt;String, Object&gt;}
3231
* and a parameter name is not specified, then the map parameter is populated
3332
* via {@link graphql.schema.DataFetchingEnvironment#getArguments()}.
3433
*
34+
* <p>This annotation does not specify whether the input argument is required
35+
* and if it should use a default value: this should be done at the schema
36+
* in order to be enforced by the GraphQL engine itself.
37+
*
3538
* @author Rossen Stoyanchev
3639
* @since 1.0.0
3740
*/
@@ -52,22 +55,4 @@
5255
@AliasFor("value")
5356
String name() default "";
5457

55-
/**
56-
* Whether the input argument is required.
57-
* <p>Defaults to {@code true}, leading to an exception being thrown
58-
* if the argument is missing. Switch this to {@code false} if you prefer
59-
* a {@code null} value when the parameter is not present.
60-
* <p>Alternatively, provide a {@link #defaultValue}, which implicitly
61-
* sets this flag to {@code false}.
62-
*/
63-
boolean required() default true;
64-
65-
/**
66-
* The default value to use as a fallback when an input argument is
67-
* not present or has an empty value.
68-
* <p>Supplying a default value implicitly sets {@link #required} to
69-
* {@code false}.
70-
*/
71-
String defaultValue() default ValueConstants.DEFAULT_NONE;
72-
7358
}

spring-graphql/src/main/java/org/springframework/graphql/data/method/annotation/support/ArgumentMethodArgumentResolver.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,10 @@ public Object resolveArgument(MethodParameter parameter, DataFetchingEnvironment
6464
}
6565
}
6666

67-
Object rawValue = (ValueConstants.DEFAULT_NONE.equals(annotation.defaultValue()) ?
68-
environment.getArgument(name) :
69-
environment.getArgumentOrDefault(name, annotation.defaultValue()));
70-
67+
Object rawValue = environment.getArgument(name);
7168
TypeDescriptor parameterType = new TypeDescriptor(parameter);
7269

7370
if (rawValue == null) {
74-
if (annotation.required()) {
75-
throw new MissingArgumentException(name, parameter);
76-
}
7771
return returnValue(rawValue, parameterType.getType());
7872
}
7973

spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/ArgumentMethodArgumentResolverTests.java

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -86,26 +86,6 @@ void shouldResolveJavaBeanArgument() throws Exception {
8686
.hasFieldOrPropertyWithValue("authorId", 42L);
8787
}
8888

89-
@Test
90-
void shouldResolveDefaultValue() throws Exception {
91-
Method findWithDefault = ClassUtils.getMethod(BookController.class, "findWithDefault", Long.class);
92-
String payload = "{\"name\": \"test\" }";
93-
DataFetchingEnvironment environment = initEnvironment(payload);
94-
MethodParameter methodParameter = getMethodParameter(findWithDefault, 0);
95-
Object result = resolver.resolveArgument(methodParameter, environment);
96-
assertThat(result).isNotNull().isInstanceOf(Long.class).isEqualTo(42L);
97-
}
98-
99-
@Test
100-
void shouldNotFailIfArgumentNotRequired() throws Exception {
101-
Method findByKeywords = ClassUtils.getMethod(BookController.class, "findByKeywords", List.class);
102-
String payload = "{ }";
103-
DataFetchingEnvironment environment = initEnvironment(payload);
104-
MethodParameter methodParameter = getMethodParameter(findByKeywords, 0);
105-
Object result = resolver.resolveArgument(methodParameter, environment);
106-
assertThat(result).isNull();
107-
}
108-
10989
@Test
11090
void shouldResolveListOfJavaBeansArgument() throws Exception {
11191
Method addBooks = ClassUtils.getMethod(BookController.class, "addBooks", List.class);
@@ -142,16 +122,6 @@ public Book bookById(@Argument Long id) {
142122
return null;
143123
}
144124

145-
@QueryMapping
146-
public Book findWithDefault(@Argument(defaultValue = "42") Long id) {
147-
return null;
148-
}
149-
150-
@QueryMapping
151-
public Book findByKeywords(@Argument(required = false) List<Keyword> keywords) {
152-
return null;
153-
}
154-
155125
@MutationMapping
156126
public Book addBook(@Argument BookInput bookInput) {
157127
return null;

0 commit comments

Comments
 (0)