Skip to content

Commit 7b30159

Browse files
committed
Remove ArgumentMapMethodArgumentResolver
Dropping this the dedicated resolver for @argument Map<String, Object> leaves it to ArgumentMethodArgumentResolver and ArgumentsMethodArgumentResolver to handle the case of Map<String, Object>, treating it either as a raw argument value for a named argument, or as the full raw arguments map. Closes gh-548
1 parent 4c3e75a commit 7b30159

File tree

8 files changed

+82
-185
lines changed

8 files changed

+82
-185
lines changed

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

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,34 +1183,41 @@ Schema mapping handler methods can have any of the following method arguments:
11831183

11841184
| `@Argument`
11851185
| For access to a named field argument bound to a higher-level, typed Object.
1186+
11861187
See <<controllers.schema-mapping.argument>>.
11871188

11881189
| `@Argument Map<String, Object>`
1189-
| For access to the raw map of arguments, where `@Argument` does not have a
1190-
`name` attribute.
1190+
| For access to the raw argument value.
1191+
1192+
See <<controllers.schema-mapping.argument>>.
11911193

11921194
| `ArgumentValue`
11931195
| For access to a named field argument bound to a higher-level, typed Object along
11941196
with a flag to indicate if the input argument was omitted vs set to `null`.
1197+
11951198
See <<controllers.schema-mapping.argument-value>>.
11961199

11971200
| `@Arguments`
11981201
| For access to all field arguments bound to a higher-level, typed Object.
1202+
11991203
See <<controllers.schema-mapping.arguments>>.
12001204

12011205
| `@Arguments Map<String, Object>`
12021206
| For access to the raw map of arguments.
12031207

12041208
| `@ProjectedPayload` Interface
12051209
| For access to field arguments through a project interface.
1210+
12061211
See <<controllers.schema-mapping.projectedpayload.argument>>.
12071212

12081213
| "Source"
12091214
| For access to the source (i.e. parent/container) instance of the field.
1215+
12101216
See <<controllers.schema-mapping.source>>.
12111217

12121218
| `DataLoader`
12131219
| For access to a `DataLoader` in the `DataLoaderRegistry`.
1220+
12141221
See <<controllers.schema-mapping.data-loader>>.
12151222

12161223
| `@ContextValue`
@@ -1290,8 +1297,26 @@ are enforced by GraphQL Java.
12901297
If binding fails, a `BindException` is raised with binding issues accumulated as field
12911298
errors where the `field` of each error is the argument path where the issue occurred.
12921299

1293-
You can use `@Argument` with a `Map<String, Object>` argument, to obtain the raw map of
1294-
all argument values. The name attribute on `@Argument` must not be set.
1300+
You can use `@Argument` with a `Map<String, Object>` argument, to obtain the raw value of
1301+
the argument. For example:
1302+
1303+
[source,java,indent=0,subs="verbatim,quotes"]
1304+
----
1305+
@Controller
1306+
public class BookController {
1307+
1308+
@MutationMapping
1309+
public Book addBook(@Argument Map<String, Object> bookInput) {
1310+
// ...
1311+
}
1312+
}
1313+
----
1314+
1315+
NOTE: Prior to 1.2, `@Argument Map<String, Object>` returned the full arguments map if
1316+
the annotation did not specify a name. After 1.2, `@Argument` with
1317+
`Map<String, Object>` always returns the raw argument value, matching either to the name
1318+
specified in the annotation, or to the parameter name. For access to the full arguments
1319+
map, please use <<controllers.schema-mapping.arguments>> instead.
12951320

12961321

12971322
[[controllers.schema-mapping.argument-value]]

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,12 +40,13 @@
4040
* {@code field} of each error is the argument path where the issue occurred.
4141
*
4242
* <p>If the method parameter is {@link java.util.Map Map&lt;String, Object&gt;}
43-
* and a parameter name is not specified, then the resolves value is the raw
44-
* {@link graphql.schema.DataFetchingEnvironment#getArguments() arguments} map.
43+
* then the raw argument value for the named argument is used. For access to the
44+
* full {@link graphql.schema.DataFetchingEnvironment#getArguments() arguments}
45+
* map, use {@link Arguments @Arguments} instead.
4546
*
46-
* <p>Note that this annotation has neither a "required" flag nor the option to
47-
* specify a default value, both of which can be specified at the GraphQL schema
48-
* level and are enforced by the GraphQL Java engine.
47+
* <p>This annotation has neither a "required" flag nor the option to specify a
48+
* default value, both of which can be specified at the GraphQL schema level
49+
* and are enforced by the GraphQL Java engine.
4950
*
5051
* @author Rossen Stoyanchev
5152
* @since 1.0.0

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ private HandlerMethodArgumentResolverComposite initArgumentResolvers() {
189189
// Must be ahead of ArgumentMethodArgumentResolver
190190
resolvers.addResolver(new ProjectedPayloadMethodArgumentResolver(obtainApplicationContext()));
191191
}
192-
resolvers.addResolver(new ArgumentMapMethodArgumentResolver());
193192
GraphQlArgumentBinder argumentBinder = new GraphQlArgumentBinder(this.conversionService);
194193
resolvers.addResolver(new ArgumentMethodArgumentResolver(argumentBinder));
195194
resolvers.addResolver(new ArgumentsMethodArgumentResolver(argumentBinder));

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

Lines changed: 0 additions & 66 deletions
This file was deleted.

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* Resolver for a method parameter that is annotated with
3131
* {@link Argument @Argument}. The specified raw argument value is obtained via
3232
* {@link DataFetchingEnvironment#getArgument(String)} and bound to a higher
33-
* level object, via {@link GraphQlArgumentBinder}, to match the target method
33+
* level object via {@link GraphQlArgumentBinder} to match the target method
3434
* parameter type.
3535
*
3636
* <p>This resolver also supports wrapping the target object with
@@ -45,9 +45,7 @@
4545
* @author Rossen Stoyanchev
4646
* @author Brian Clozel
4747
* @since 1.0.0
48-
* @see org.springframework.graphql.data.method.annotation.Argument
49-
* @see org.springframework.graphql.data.method.annotation.Arguments
50-
* @see org.springframework.graphql.data.GraphQlArgumentBinder
48+
* @see org.springframework.graphql.data.method.annotation.support.ArgumentsMethodArgumentResolver
5149
*/
5250
public class ArgumentMethodArgumentResolver implements HandlerMethodArgumentResolver {
5351

@@ -81,7 +79,8 @@ static String getArgumentName(MethodParameter parameter) {
8179
}
8280
}
8381
else if (parameter.getParameterType() != ArgumentValue.class) {
84-
throw new IllegalStateException("Expected @Argument annotation");
82+
throw new IllegalStateException(
83+
"Expected either @Argument or a method parameter of type ArgumentValue");
8584
}
8685

8786
String parameterName = parameter.getParameterName();

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

Lines changed: 0 additions & 101 deletions
This file was deleted.

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2022 the original author or authors.
2+
* Copyright 2020-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919

2020
import java.util.List;
21+
import java.util.Map;
2122

2223
import org.junit.jupiter.api.Test;
2324

@@ -52,6 +53,9 @@ void supportsParameter() {
5253
param = methodParam(BookController.class, "addBook", ArgumentValue.class);
5354
assertThat(this.resolver.supportsParameter(param)).isTrue();
5455

56+
param = methodParam(BookController.class, "rawArgumentValue", Map.class);
57+
assertThat(this.resolver.supportsParameter(param)).isTrue();
58+
5559
param = methodParam(BookController.class, "notSupported", String.class);
5660
assertThat(this.resolver.supportsParameter(param)).isFalse();
5761
}
@@ -113,6 +117,17 @@ void shouldResolveArgumentWithConversionService() throws Exception {
113117
assertThat(result).isNotNull().isInstanceOf(Keyword.class).hasFieldOrPropertyWithValue("term", "test");
114118
}
115119

120+
@Test
121+
void shouldResolveRawArgumentValue() throws Exception {
122+
Map<String, Object> result = (Map<String, Object>) this.resolver.resolveArgument(
123+
methodParam(BookController.class, "rawArgumentValue", Map.class),
124+
environment("{\"bookInput\": { \"name\": \"test name\", \"authorId\": 42} }"));
125+
126+
assertThat(result)
127+
.containsEntry("name", "test name")
128+
.containsEntry("authorId", 42);
129+
}
130+
116131

117132
@SuppressWarnings({"ConstantConditions", "unused"})
118133
@Controller
@@ -147,8 +162,13 @@ public List<Book> bookByKeyword(@Argument Keyword keyword) {
147162
return null;
148163
}
149164

165+
@MutationMapping
166+
public Book rawArgumentValue(@Argument Map<?, ?> bookInput) {
167+
return null;
168+
}
150169
}
151170

171+
152172
@SuppressWarnings({"NotNullFieldNotInitialized", "unused"})
153173
static class BookInput {
154174

0 commit comments

Comments
 (0)