Skip to content

Commit dfed05a

Browse files
committed
Consistent access to raw arguments map
See gh-493
1 parent 9abaa23 commit dfed05a

File tree

3 files changed

+130
-6
lines changed

3 files changed

+130
-6
lines changed

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

+12-3
Original file line numberDiff line numberDiff line change
@@ -1275,11 +1275,18 @@ See <<controllers-schema-mapping-argument>>.
12751275
| For access to all field arguments bound to a higher-level, typed Object.
12761276
See <<controllers-schema-mapping-arguments>>.
12771277

1278+
| `@Argument Map<String, Object>`
1279+
| For access to the raw map of arguments, where `@Argument` does not have a
1280+
`name` attribute.
1281+
1282+
| `@Arguments Map<String, Object>`
1283+
| For access to the raw map of arguments.
1284+
12781285
| `@ProjectedPayload` Interface
12791286
| For access to field arguments through a project interface.
12801287
See <<controllers-schema-mapping-projectedpayload-argument>>.
12811288

1282-
| Source
1289+
| "Source"
12831290
| For access to the source (i.e. parent/container) instance of the field.
12841291
See <<controllers-schema-mapping-source>>.
12851292

@@ -1365,8 +1372,8 @@ are enforced by GraphQL Java.
13651372
If binding fails, a `BindException` is raised with binding issues accumulated as field
13661373
errors where the `field` of each error is the argument path where the issue occurred.
13671374

1368-
You can use `@Argument` on a `Map<String, Object>` argument, to obtain all argument
1369-
values. The name attribute on `@Argument` must not be set.
1375+
You can use `@Argument` with a `Map<String, Object>` argument, to obtain the raw map of
1376+
all argument values. The name attribute on `@Argument` must not be set.
13701377

13711378

13721379

@@ -1380,6 +1387,8 @@ For example, `@Argument BookInput bookInput` uses the value of the argument "boo
13801387
to initialize `BookInput`, while `@Arguments` uses the full arguments map and in that
13811388
case, top-level arguments are bound to `BookInput` properties.
13821389

1390+
You can use `@Arguments` with a `Map<String, Object>` argument, to obtain the raw map of
1391+
all argument values.
13831392

13841393

13851394
[[controllers-schema-mapping-projectedpayload-argument]]

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

+17-3
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,18 @@
2323
import org.springframework.core.MethodParameter;
2424
import org.springframework.graphql.data.method.HandlerMethodArgumentResolver;
2525
import org.springframework.graphql.data.method.annotation.Argument;
26+
import org.springframework.graphql.data.method.annotation.Arguments;
2627
import org.springframework.util.StringUtils;
2728

2829

2930
/**
30-
* Resolves a {@link Map} method parameter annotated with an
31-
* {@link Argument @Argument} by returning the GraphQL
32-
* {@link DataFetchingEnvironment#getArguments() arguments} map.
31+
* Resolves a {@link Map} method parameter for access to the raw arguments map.
32+
* Supported with the following:
33+
* <ul>
34+
* <li>{@link Map} argument annotated with {@link Argument @Argument} where the
35+
* annotation does not explicitly specify a name.
36+
* <li>{@link Map} argument annotated with {@link Arguments @Arguments}.
37+
* </ul>
3338
*
3439
* @author Rossen Stoyanchev
3540
* @since 1.0.0
@@ -38,12 +43,21 @@ public class ArgumentMapMethodArgumentResolver implements HandlerMethodArgumentR
3843

3944
@Override
4045
public boolean supportsParameter(MethodParameter parameter) {
46+
return (checkArgumentMap(parameter) || checkArgumentsMap(parameter));
47+
}
48+
49+
private static boolean checkArgumentMap(MethodParameter parameter) {
4150
Argument argument = parameter.getParameterAnnotation(Argument.class);
4251
return (argument != null &&
4352
Map.class.isAssignableFrom(parameter.getParameterType()) &&
4453
!StringUtils.hasText(argument.name()));
4554
}
4655

56+
private static boolean checkArgumentsMap(MethodParameter parameter) {
57+
Arguments argument = parameter.getParameterAnnotation(Arguments.class);
58+
return (argument != null && Map.class.isAssignableFrom(parameter.getParameterType()));
59+
}
60+
4761
@Override
4862
public Object resolveArgument(MethodParameter parameter, DataFetchingEnvironment environment) {
4963
return environment.getArguments();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright 2020-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.graphql.data.method.annotation.support;
18+
19+
20+
import java.util.Collections;
21+
import java.util.Map;
22+
23+
import org.junit.jupiter.api.Test;
24+
25+
import org.springframework.core.MethodParameter;
26+
import org.springframework.graphql.Book;
27+
import org.springframework.graphql.data.method.HandlerMethodArgumentResolver;
28+
import org.springframework.graphql.data.method.annotation.Argument;
29+
import org.springframework.graphql.data.method.annotation.Arguments;
30+
import org.springframework.graphql.data.method.annotation.QueryMapping;
31+
import org.springframework.stereotype.Controller;
32+
33+
import static org.assertj.core.api.Assertions.assertThat;
34+
35+
/**
36+
* Unit tests for {@link ArgumentMethodArgumentResolver}.
37+
* @author Rossen Stoyanchev
38+
*/
39+
class ArgumentMapMethodArgumentResolverTests extends ArgumentResolverTestSupport {
40+
41+
private final HandlerMethodArgumentResolver resolver = new ArgumentMapMethodArgumentResolver();
42+
43+
44+
@Test
45+
void shouldSupportAnnotatedParameters() {
46+
MethodParameter param = methodParam(BookController.class, "argumentMap", Map.class);
47+
assertThat(this.resolver.supportsParameter(param)).isTrue();
48+
49+
param = methodParam(BookController.class, "argumentsMap", Map.class);
50+
assertThat(this.resolver.supportsParameter(param)).isTrue();
51+
52+
param = methodParam(BookController.class, "argument", Long.class);
53+
assertThat(this.resolver.supportsParameter(param)).isFalse();
54+
55+
param = methodParam(BookController.class, "namedArgumentMap", Map.class);
56+
assertThat(this.resolver.supportsParameter(param)).isFalse();
57+
58+
param = methodParam(BookController.class, "notAnnotated", String.class);
59+
assertThat(this.resolver.supportsParameter(param)).isFalse();
60+
}
61+
62+
@Test
63+
void shouldResolveRawArgumentsMap() throws Exception {
64+
Object result = this.resolver.resolveArgument(
65+
methodParam(BookController.class, "argumentMap", Map.class),
66+
environment("{\"id\": 42 }"));
67+
68+
assertThat(result).isNotNull().isInstanceOf(Map.class).isEqualTo(Collections.singletonMap("id", 42));
69+
}
70+
71+
72+
@SuppressWarnings({"ConstantConditions", "unused"})
73+
@Controller
74+
static class BookController {
75+
76+
@QueryMapping
77+
public Book argumentMap(@Argument Map<?, ?> args) {
78+
return null;
79+
}
80+
81+
@QueryMapping
82+
public Book argumentsMap(@Arguments Map<?, ?> args) {
83+
return null;
84+
}
85+
86+
@QueryMapping
87+
public Book argument(@Argument Long id) {
88+
return null;
89+
}
90+
91+
@QueryMapping
92+
public Book namedArgumentMap(@Argument(name = "book") Map<?, ?> book) {
93+
return null;
94+
}
95+
96+
public void notAnnotated(String param) {
97+
}
98+
99+
}
100+
101+
}

0 commit comments

Comments
 (0)