Skip to content

Commit fe4af8a

Browse files
committed
Propagate context to Dataloader in @BatchMapping
Prior to this commit, `@BatchMapping` controller methods would be automatically registered as date fetchers delegating to data loader calls. Those calls would not include the current local context or main context. As a result, injecting the `BatchLoaderEnvironment` in the controller method signature would not contain the `getKeyContext()`. This commit ensures that dataloader calls not only use the current source, but also the current local context/main context so that it will be present in the key contexts map. Fixes gh-1066
1 parent a210f77 commit fe4af8a

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ public ResolvableType getReturnType() {
603603
public Object get(DataFetchingEnvironment env) {
604604
DataLoader<?, ?> dataLoader = env.getDataLoaderRegistry().getDataLoader(this.dataLoaderKey);
605605
Assert.state(dataLoader != null, "No DataLoader for key '" + this.dataLoaderKey + "'");
606-
return dataLoader.load(env.getSource());
606+
return dataLoader.load(env.getSource(), (env.getLocalContext() != null) ? env.getLocalContext() : env.getGraphQlContext());
607607
}
608608

609609
@Override

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

+44-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,9 @@
2424
import java.util.stream.Collectors;
2525
import java.util.stream.Stream;
2626

27+
import graphql.GraphQLContext;
28+
import org.dataloader.BatchLoaderEnvironment;
29+
import org.junit.jupiter.api.Test;
2730
import org.junit.jupiter.params.ParameterizedTest;
2831
import org.junit.jupiter.params.provider.Arguments;
2932
import org.junit.jupiter.params.provider.MethodSource;
@@ -126,12 +129,33 @@ void oneToMany(CourseController controller) {
126129
}
127130
}
128131

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+
129153

130154
@Controller
131155
private static class BatchMonoMapController extends CourseController {
132156

133157
@BatchMapping
134-
public Mono<Map<Course, Person>> instructor(List<Course> courses) {
158+
public Mono<Map<Course, Person>> instructor(List<Course> courses, BatchLoaderEnvironment environment) {
135159
return Flux.fromIterable(courses).collect(Collectors.toMap(Function.identity(), Course::instructor));
136160
}
137161

@@ -203,5 +227,23 @@ public Callable<Map<Course, List<Person>>> students(List<Course> courses) {
203227

204228
}
205229

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+
206248

207249
}

0 commit comments

Comments
 (0)