Skip to content

Commit 88d7ea7

Browse files
committed
Pass localContext only if available
See gh-1066
1 parent 58cce01 commit 88d7ea7

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

spring-graphql-docs/modules/ROOT/pages/controllers.adoc

+9-1
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,15 @@ Batch mapping methods support the following arguments:
743743

744744
| `BatchLoaderEnvironment`
745745
| The environment that is available in GraphQL Java to a
746-
`org.dataloader.BatchLoaderWithContext`.
746+
`org.dataloader.BatchLoaderWithContext`.
747+
748+
The `context` property of `BatchLoaderEnvironment` returns the same
749+
`GraphQLContext` instance that is also available to `@SchemaMapping`
750+
methods through the `DataFetchingEnvironment`.
751+
752+
The `keyContexts` property of `BatchLoaderEnvironment` returns the
753+
localContext obtained from the `DataFetchingEnvironment` when the
754+
`DataLoader` was called for each key.
747755

748756
|===
749757

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,9 @@ 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(), (env.getLocalContext() != null) ? env.getLocalContext() : env.getGraphQlContext());
606+
return ((env.getLocalContext() != null) ?
607+
dataLoader.load(env.getSource(), env.getLocalContext()) :
608+
dataLoader.load(env.getSource()));
607609
}
608610

609611
@Override

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

+13-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.graphql.data.method.annotation.support;
1818

19+
import java.util.Collection;
1920
import java.util.List;
2021
import java.util.Map;
2122
import java.util.Set;
@@ -25,6 +26,7 @@
2526
import java.util.stream.Stream;
2627

2728
import graphql.GraphQLContext;
29+
import graphql.execution.DataFetcherResult;
2830
import org.dataloader.BatchLoaderEnvironment;
2931
import org.junit.jupiter.api.Test;
3032
import org.junit.jupiter.params.ParameterizedTest;
@@ -36,6 +38,7 @@
3638
import org.springframework.graphql.ExecutionGraphQlResponse;
3739
import org.springframework.graphql.ResponseHelper;
3840
import org.springframework.graphql.data.method.annotation.BatchMapping;
41+
import org.springframework.graphql.data.method.annotation.QueryMapping;
3942
import org.springframework.stereotype.Controller;
4043

4144
import static org.assertj.core.api.Assertions.assertThat;
@@ -143,7 +146,8 @@ void shouldBindKeyContextsToEnvironment() {
143146
" }" +
144147
"}";
145148

146-
Mono<ExecutionGraphQlResponse> responseMono = createGraphQlService(new BatchKeyContextsController()).execute(document);
149+
Mono<ExecutionGraphQlResponse> responseMono = createGraphQlService(
150+
BatchKeyContextsController.class, new BatchKeyContextsController()).execute(document);
147151

148152
List<Course> actualCourses = ResponseHelper.forResponse(responseMono).toList("courses", Course.class);
149153
List<Course> courses = Course.allCourses();
@@ -228,7 +232,14 @@ public Callable<Map<Course, List<Person>>> students(List<Course> courses) {
228232
}
229233

230234
@Controller
231-
private static class BatchKeyContextsController extends CourseController {
235+
private static class BatchKeyContextsController {
236+
237+
@QueryMapping
238+
public DataFetcherResult<Collection<Course>> courses() {
239+
return DataFetcherResult.<Collection<Course>>newResult().data(courseMap.values())
240+
.localContext(GraphQLContext.newContext().build())
241+
.build();
242+
}
232243

233244
@BatchMapping
234245
public List<Person> instructor(List<Course> courses, BatchLoaderEnvironment environment) {

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,14 @@ public class BatchMappingTestSupport {
8282

8383

8484
protected TestExecutionGraphQlService createGraphQlService(CourseController controller) {
85+
return createGraphQlService(CourseController.class, controller);
86+
}
87+
88+
protected <T> TestExecutionGraphQlService createGraphQlService(Class<T> controllerClass, T controller) {
8589
BatchLoaderRegistry registry = new DefaultBatchLoaderRegistry();
8690

8791
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
88-
context.registerBean(CourseController.class, () -> controller);
92+
context.registerBean(controllerClass, () -> controller);
8993
context.registerBean(BatchLoaderRegistry.class, () -> registry);
9094
context.refresh();
9195

0 commit comments

Comments
 (0)