Skip to content

Commit 27da69d

Browse files
committed
bypass arguments parsing when target type is a map
This allows for unparsed forwarding of graphql arguments, in the case something application specific has to be done with the arguments.
1 parent 5109a11 commit 27da69d

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,19 @@ public Object resolveArgument(MethodParameter parameter, DataFetchingEnvironment
7474
if (CollectionFactory.isApproximableCollectionType(rawValue.getClass())) {
7575
Assert.isAssignable(Collection.class, parameterType.getType(),
7676
"Argument '" + name + "' is a Collection while the @Argument method parameter is " + parameterType.getType());
77+
78+
if (parameterType.getElementTypeDescriptor().getType() == Map.class) {
79+
return rawValue;
80+
}
81+
7782
Class<?> elementType = parameterType.getElementTypeDescriptor().getType();
7883
return this.instantiator.instantiateCollection(elementType, (Collection<Object>) rawValue);
7984
}
8085

86+
if (parameterType.isMap()) {
87+
return rawValue;
88+
}
89+
8190
MethodParameter nestedParameter = parameter.nestedIfOptional();
8291
Object value = convert(rawValue, nestedParameter.getNestedParameterType());
8392
return returnValue(value, parameterType.getType());

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919

2020
import java.lang.reflect.Method;
21+
import java.util.HashMap;
2122
import java.util.List;
2223
import java.util.Map;
2324

@@ -98,6 +99,32 @@ void shouldResolveListOfJavaBeansArgument() throws Exception {
9899
.extracting("name").containsExactly("first", "second");
99100
}
100101

102+
@Test
103+
void shouldPassArgumentAsMap() throws Exception {
104+
Method updateBook = ClassUtils.getMethod(BookController.class, "updateBook", Long.class, Map.class);
105+
String payload = "{\"id\": 43, \"input\": { \"name\": \"new name\", \"description\": null, \"authorId\": 42} }";
106+
DataFetchingEnvironment environment = initEnvironment(payload);
107+
MethodParameter methodParameter = getMethodParameter(updateBook, 1);
108+
Object result = resolver.resolveArgument(methodParameter, environment);
109+
assertThat(result).isNotNull().isInstanceOf(Map.class);
110+
assertThat(result)
111+
.hasFieldOrPropertyWithValue("name", "new name")
112+
.hasFieldOrPropertyWithValue("description", null)
113+
.hasFieldOrPropertyWithValue("authorId", 42);
114+
}
115+
116+
@Test
117+
void shouldPassArgumentAsList() throws Exception {
118+
Method updateBook = ClassUtils.getMethod(BookController.class, "updateBooks", List.class);
119+
String payload = "{\"input\": [{ \"id\": 1, \"name\": \"first\" }, { \"id\": 2, \"name\": \"second\" }] }";
120+
DataFetchingEnvironment environment = initEnvironment(payload);
121+
MethodParameter methodParameter = getMethodParameter(updateBook, 0);
122+
Object result = resolver.resolveArgument(methodParameter, environment);
123+
assertThat(result).isNotNull().isInstanceOf(List.class);
124+
assertThat(result).asList().allMatch(item -> item instanceof Map)
125+
.extracting("name").containsExactly("first", "second");
126+
}
127+
101128
private MethodParameter getMethodParameter(Method method, int index) {
102129
MethodParameter methodParameter = new MethodParameter(method, index);
103130
methodParameter.initParameterNameDiscovery(new DefaultParameterNameDiscoverer());
@@ -132,6 +159,16 @@ public List<Book> addBooks(@Argument List<Book> books) {
132159
return null;
133160
}
134161

162+
@MutationMapping
163+
public List<Book> updateBook(@Argument Long id, @Argument Map<String, ?> input) {
164+
return null;
165+
}
166+
167+
@MutationMapping
168+
public List<Book> updateBooks(@Argument List<Map<String, ?>> input) {
169+
return null;
170+
}
171+
135172
}
136173

137174
static class BookInput {

0 commit comments

Comments
 (0)