Skip to content

Commit 429f6f4

Browse files
committed
Polishing
Show map-based batch loading first which is easier to implement without having to order results. Use JUnit named arguments in BatchMappingInvocationTests. See gh-130
1 parent fc04249 commit 429f6f4

File tree

3 files changed

+44
-40
lines changed

3 files changed

+44
-40
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ configure(moduleProjects) {
6363
mavenBom "org.springframework.security:spring-security-bom:5.5.2"
6464
mavenBom "org.jetbrains.kotlin:kotlin-bom:1.5.31"
6565
mavenBom "org.jetbrains.kotlinx:kotlinx-coroutines-bom:1.5.2"
66-
mavenBom "org.junit:junit-bom:5.7.2"
66+
mavenBom "org.junit:junit-bom:5.8.1"
6767
}
6868
dependencies {
6969
dependency "com.graphql-java:graphql-java:${graphQlJavaVersion}"

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -538,8 +538,8 @@ can be loaded together. For example:
538538
public class BookController {
539539
540540
public BookController(BatchLoaderRegistry registry) {
541-
registry.forTypePair(Long.class, Author.class).registerBatchLoader((authorIds, environment) -> {
542-
// how to load authors for the given author id's...
541+
registry.forTypePair(Long.class, Author.class).registerMappedBatchLoader((authorIds, env) -> {
542+
// return Map<Long, Author>
543543
});
544544
}
545545
@@ -561,7 +561,7 @@ boilerplate that can be avoided with a `@BatchMapping` method. For example:
561561
public class BookController {
562562
563563
@BatchMapping
564-
public Flux<Author> author(List<Book> books) {
564+
public Mono<Map<Book, Author>> author(List<Book> books) {
565565
// ...
566566
}
567567
}
@@ -582,23 +582,23 @@ the simple class name of the input `List` element type. Both can be customized t
582582
annotation attributes. The type name can also be inherited from a class level
583583
`@SchemaMapping`.
584584

585-
A `@BatchMapping` method can be a
586-
{javadoc}/org/springframework/graphql/execution/BatchLoaderRegistry.RegistrationSpec.html#registerMappedBatchLoader-java.util.function.BiFunction-[mapped batch loading] function:
585+
A `@BatchMapping` method can also return a sequence of instances, and that needs to match
586+
the order of the source/parent objects:
587587

588588
[source,java,indent=0,subs="verbatim,quotes"]
589589
----
590590
@Controller
591591
public class BookController {
592592
593593
@BatchMapping
594-
public Mono<Map<Book, Author>> author(List<Book> books) {
594+
public Flux<Author> author(List<Book> books) {
595595
// ...
596596
}
597597
}
598598
----
599599

600-
It is possible to use imperative method signatures too, i.e. returning `List<V>` or
601-
`Map<K, V>`, which can be useful when there are no remote calls to make.
600+
It is possible to use imperative method signatures too, i.e. returning `Map<K, V>` or
601+
`List<V>`, which can be useful when there are no remote calls to make.
602602

603603
`BatchMapping` methods support two types of arguments:
604604

@@ -741,8 +741,8 @@ method argument of type `DataLoader` and use it to load the entity:
741741
public class BookController {
742742
743743
public BookController(BatchLoaderRegistry registry) {
744-
registry.forTypePair(Long.class, Author.class).registerBatchLoader((authorIds, env) -> {
745-
// load authors
744+
registry.forTypePair(Long.class, Author.class).registerMappedBatchLoader((authorIds, env) -> {
745+
// return Map<Long, Author>
746746
});
747747
}
748748

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

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525
import java.util.Set;
2626
import java.util.function.Function;
2727
import java.util.stream.Collectors;
28+
import java.util.stream.Stream;
2829

2930
import graphql.ExecutionResult;
3031
import org.junit.jupiter.params.ParameterizedTest;
32+
import org.junit.jupiter.params.provider.Arguments;
3133
import org.junit.jupiter.params.provider.MethodSource;
3234
import reactor.core.publisher.Flux;
3335
import reactor.core.publisher.Mono;
@@ -47,6 +49,8 @@
4749
import org.springframework.stereotype.Controller;
4850

4951
import static org.assertj.core.api.Assertions.assertThat;
52+
import static org.junit.jupiter.api.Named.named;
53+
import static org.junit.jupiter.params.provider.Arguments.arguments;
5054

5155
/**
5256
* Test GraphQL requests handled through {@code @BatchMapping} methods.
@@ -91,13 +95,13 @@ public class BatchMappingInvocationTests {
9195
"}";
9296

9397

94-
private static Class<?>[] controllerClasses() {
95-
return new Class[] {
96-
BatchFluxController.class,
97-
BatchListController.class,
98-
BatchMonoMapController.class,
99-
BatchMapController.class
100-
};
98+
private static Stream<Arguments> controllerClasses() {
99+
return Stream.of(
100+
arguments(named("Returning Mono<Map<K,V>>", BatchMonoMapController.class)),
101+
arguments(named("Returning Map<K,V>", BatchMapController.class)),
102+
arguments(named("Returning Flux<V>", BatchFluxController.class)),
103+
arguments(named("Returning List<V>", BatchListController.class))
104+
);
101105
}
102106

103107
@ParameterizedTest
@@ -199,60 +203,60 @@ public Collection<Course> courses() {
199203
}
200204

201205
@Controller
202-
private static class BatchFluxController extends CourseController {
206+
private static class BatchMonoMapController extends CourseController {
203207

204208
@BatchMapping
205-
public Flux<Person> instructor(List<Course> courses) {
206-
return Flux.fromIterable(courses).map(Course::instructor);
209+
public Mono<Map<Course, Person>> instructor(List<Course> courses) {
210+
return Flux.fromIterable(Course.allCourses())
211+
.collect(Collectors.toMap(Function.identity(), Course::instructor));
207212
}
208213

209214
@BatchMapping
210-
public Flux<List<Person>> students(List<Course> courses) {
211-
return Flux.fromIterable(courses).map(Course::students);
215+
public Mono<Map<Course, List<Person>>> students(Set<Course> courses) {
216+
return Flux.fromIterable(courses).collect(Collectors.toMap(Function.identity(), Course::students));
212217
}
213218
}
214219

215220
@Controller
216-
private static class BatchListController extends CourseController {
221+
private static class BatchMapController extends CourseController {
217222

218223
@BatchMapping
219-
public List<Person> instructor(List<Course> courses) {
220-
return courses.stream().map(Course::instructor).collect(Collectors.toList());
224+
public Map<Course, Person> instructor(List<Course> courses) {
225+
return Course.allCourses().stream().collect(
226+
Collectors.toMap(Function.identity(), Course::instructor));
221227
}
222228

223229
@BatchMapping
224-
public List<List<Person>> students(List<Course> courses) {
225-
return courses.stream().map(Course::students).collect(Collectors.toList());
230+
public Map<Course, List<Person>> students(List<Course> courses) {
231+
return courses.stream().collect(Collectors.toMap(Function.identity(), Course::students));
226232
}
227233
}
228234

229235
@Controller
230-
private static class BatchMonoMapController extends CourseController {
236+
private static class BatchFluxController extends CourseController {
231237

232238
@BatchMapping
233-
public Mono<Map<Course, Person>> instructor(List<Course> courses) {
234-
return Flux.fromIterable(Course.allCourses())
235-
.collect(Collectors.toMap(Function.identity(), Course::instructor));
239+
public Flux<Person> instructor(List<Course> courses) {
240+
return Flux.fromIterable(courses).map(Course::instructor);
236241
}
237242

238243
@BatchMapping
239-
public Mono<Map<Course, List<Person>>> students(Set<Course> courses) {
240-
return Flux.fromIterable(courses).collect(Collectors.toMap(Function.identity(), Course::students));
244+
public Flux<List<Person>> students(List<Course> courses) {
245+
return Flux.fromIterable(courses).map(Course::students);
241246
}
242247
}
243248

244249
@Controller
245-
private static class BatchMapController extends CourseController {
250+
private static class BatchListController extends CourseController {
246251

247252
@BatchMapping
248-
public Map<Course, Person> instructor(List<Course> courses) {
249-
return Course.allCourses().stream().collect(
250-
Collectors.toMap(Function.identity(), Course::instructor));
253+
public List<Person> instructor(List<Course> courses) {
254+
return courses.stream().map(Course::instructor).collect(Collectors.toList());
251255
}
252256

253257
@BatchMapping
254-
public Map<Course, List<Person>> students(List<Course> courses) {
255-
return courses.stream().collect(Collectors.toMap(Function.identity(), Course::students));
258+
public List<List<Person>> students(List<Course> courses) {
259+
return courses.stream().map(Course::students).collect(Collectors.toList());
256260
}
257261
}
258262

0 commit comments

Comments
 (0)