|
18 | 18 |
|
19 | 19 |
|
20 | 20 | import java.lang.reflect.Method;
|
| 21 | +import java.util.HashMap; |
21 | 22 | import java.util.List;
|
22 | 23 | import java.util.Map;
|
23 | 24 |
|
@@ -98,6 +99,32 @@ void shouldResolveListOfJavaBeansArgument() throws Exception {
|
98 | 99 | .extracting("name").containsExactly("first", "second");
|
99 | 100 | }
|
100 | 101 |
|
| 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 | + |
101 | 128 | private MethodParameter getMethodParameter(Method method, int index) {
|
102 | 129 | MethodParameter methodParameter = new MethodParameter(method, index);
|
103 | 130 | methodParameter.initParameterNameDiscovery(new DefaultParameterNameDiscoverer());
|
@@ -132,6 +159,16 @@ public List<Book> addBooks(@Argument List<Book> books) {
|
132 | 159 | return null;
|
133 | 160 | }
|
134 | 161 |
|
| 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 | + |
135 | 172 | }
|
136 | 173 |
|
137 | 174 | static class BookInput {
|
|
0 commit comments