|
17 | 17 |
|
18 | 18 | import java.lang.reflect.Method;
|
19 | 19 | import java.time.Duration;
|
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.List; |
20 | 22 | import java.util.Optional;
|
21 | 23 | import java.util.function.BiConsumer;
|
22 | 24 |
|
23 | 25 | import graphql.GraphQLContext;
|
24 | 26 | import graphql.schema.DataFetchingEnvironment;
|
25 | 27 | import graphql.schema.DataFetchingEnvironmentImpl;
|
| 28 | +import org.dataloader.BatchLoaderEnvironment; |
26 | 29 | import org.junit.jupiter.api.Test;
|
27 | 30 | import reactor.core.publisher.Mono;
|
28 | 31 | import reactor.test.StepVerifier;
|
29 | 32 |
|
30 | 33 | import org.springframework.core.DefaultParameterNameDiscoverer;
|
31 | 34 | import org.springframework.core.MethodParameter;
|
32 | 35 | import org.springframework.core.annotation.SynthesizingMethodParameter;
|
| 36 | +import org.springframework.graphql.Author; |
33 | 37 | import org.springframework.graphql.Book;
|
| 38 | +import org.springframework.graphql.BookSource; |
34 | 39 | import org.springframework.graphql.data.method.HandlerMethod;
|
35 | 40 | import org.springframework.graphql.data.method.HandlerMethodArgumentResolverComposite;
|
36 | 41 | import org.springframework.graphql.data.method.annotation.ContextValue;
|
@@ -134,6 +139,28 @@ void resolveMono() throws Exception {
|
134 | 139 | StepVerifier.create((Mono<String>) handlerMethod.invoke(environment)).verifyComplete();
|
135 | 140 | }
|
136 | 141 |
|
| 142 | + @Test // gh-562 |
| 143 | + void resolveFromParameterNameWithBatchMapping() throws Exception { |
| 144 | + |
| 145 | + TestController controller = new TestController(); |
| 146 | + |
| 147 | + BatchLoaderHandlerMethod handlerMethod = new BatchLoaderHandlerMethod( |
| 148 | + new HandlerMethod(controller, |
| 149 | + TestController.class.getMethod("getAuthors", List.class, Long.class)), null); |
| 150 | + |
| 151 | + GraphQLContext context = new GraphQLContext.Builder().build(); |
| 152 | + context.put("id", 123L); |
| 153 | + |
| 154 | + BatchLoaderEnvironment environment = BatchLoaderEnvironment.newBatchLoaderEnvironment().context(context).build(); |
| 155 | + List<Book> keys = Arrays.asList(BookSource.getBook(1L), BookSource.getBook(2L), BookSource.getBook(3L)); |
| 156 | + |
| 157 | + StepVerifier.create(handlerMethod.invokeForIterable(keys, environment)) |
| 158 | + .expectNextCount(3) |
| 159 | + .verifyComplete(); |
| 160 | + |
| 161 | + assertThat(controller.savedId).isEqualTo(context.get("id")); |
| 162 | + } |
| 163 | + |
137 | 164 | @Nullable
|
138 | 165 | private Object resolveValue(
|
139 | 166 | @Nullable GraphQLContext localContext, @Nullable GraphQLContext graphQLContext, int index) {
|
@@ -166,11 +193,17 @@ public void handle(
|
166 | 193 |
|
167 | 194 | private static class TestController {
|
168 | 195 |
|
| 196 | + private Long savedId; |
| 197 | + |
169 | 198 | @Nullable
|
170 | 199 | public String handleMono(@ContextValue Mono<String> stringMono) {
|
171 | 200 | return stringMono.block(Duration.ofSeconds(1));
|
172 | 201 | }
|
173 | 202 |
|
| 203 | + public List<Author> getAuthors(List<Book> books, @ContextValue Long id) { |
| 204 | + this.savedId = id; |
| 205 | + return books.stream().map(Book::getAuthor).toList(); |
| 206 | + } |
174 | 207 | }
|
175 | 208 |
|
176 | 209 | }
|
0 commit comments