|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2023 the original author or authors. |
| 2 | + * Copyright 2002-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
24 | 24 | import java.util.stream.Collectors;
|
25 | 25 | import java.util.stream.Stream;
|
26 | 26 |
|
| 27 | +import graphql.GraphQLContext; |
| 28 | +import org.dataloader.BatchLoaderEnvironment; |
| 29 | +import org.junit.jupiter.api.Test; |
27 | 30 | import org.junit.jupiter.params.ParameterizedTest;
|
28 | 31 | import org.junit.jupiter.params.provider.Arguments;
|
29 | 32 | import org.junit.jupiter.params.provider.MethodSource;
|
@@ -126,12 +129,33 @@ void oneToMany(CourseController controller) {
|
126 | 129 | }
|
127 | 130 | }
|
128 | 131 |
|
| 132 | + @Test |
| 133 | + void shouldBindKeyContextsToEnvironment() { |
| 134 | + String document = "{ " + |
| 135 | + " courses { " + |
| 136 | + " id" + |
| 137 | + " name" + |
| 138 | + " students {" + |
| 139 | + " id" + |
| 140 | + " firstName" + |
| 141 | + " lastName" + |
| 142 | + " }" + |
| 143 | + " }" + |
| 144 | + "}"; |
| 145 | + |
| 146 | + Mono<ExecutionGraphQlResponse> responseMono = createGraphQlService(new BatchKeyContextsController()).execute(document); |
| 147 | + |
| 148 | + List<Course> actualCourses = ResponseHelper.forResponse(responseMono).toList("courses", Course.class); |
| 149 | + List<Course> courses = Course.allCourses(); |
| 150 | + assertThat(actualCourses).hasSize(courses.size()); |
| 151 | + } |
| 152 | + |
129 | 153 |
|
130 | 154 | @Controller
|
131 | 155 | private static class BatchMonoMapController extends CourseController {
|
132 | 156 |
|
133 | 157 | @BatchMapping
|
134 |
| - public Mono<Map<Course, Person>> instructor(List<Course> courses) { |
| 158 | + public Mono<Map<Course, Person>> instructor(List<Course> courses, BatchLoaderEnvironment environment) { |
135 | 159 | return Flux.fromIterable(courses).collect(Collectors.toMap(Function.identity(), Course::instructor));
|
136 | 160 | }
|
137 | 161 |
|
@@ -203,5 +227,23 @@ public Callable<Map<Course, List<Person>>> students(List<Course> courses) {
|
203 | 227 |
|
204 | 228 | }
|
205 | 229 |
|
| 230 | + @Controller |
| 231 | + private static class BatchKeyContextsController extends CourseController { |
| 232 | + |
| 233 | + @BatchMapping |
| 234 | + public List<Person> instructor(List<Course> courses, BatchLoaderEnvironment environment) { |
| 235 | + assertThat(environment.getKeyContexts().keySet()).containsAll(Course.allCourses()); |
| 236 | + assertThat(environment.getKeyContexts().values()).allSatisfy(value -> assertThat(value).isInstanceOf(GraphQLContext.class)); |
| 237 | + return courses.stream().map(Course::instructor).collect(Collectors.toList()); |
| 238 | + } |
| 239 | + |
| 240 | + @BatchMapping |
| 241 | + public List<List<Person>> students(List<Course> courses, BatchLoaderEnvironment environment) { |
| 242 | + assertThat(environment.getKeyContexts().keySet()).containsAll(Course.allCourses()); |
| 243 | + assertThat(environment.getKeyContexts().values()).allSatisfy(value -> assertThat(value).isInstanceOf(GraphQLContext.class)); |
| 244 | + return courses.stream().map(Course::students).collect(Collectors.toList()); |
| 245 | + } |
| 246 | + } |
| 247 | + |
206 | 248 |
|
207 | 249 | }
|
0 commit comments