Skip to content

Commit 424db66

Browse files
committed
Pass localContext only if available
See gh-1066
1 parent ad0e4c7 commit 424db66

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,15 @@ Batch mapping methods support the following arguments:
646646

647647
| `BatchLoaderEnvironment`
648648
| The environment that is available in GraphQL Java to a
649-
`org.dataloader.BatchLoaderWithContext`.
649+
`org.dataloader.BatchLoaderWithContext`.
650+
651+
The `context` property of `BatchLoaderEnvironment` returns the same
652+
`GraphQLContext` instance that is also available to `@SchemaMapping`
653+
methods through the `DataFetchingEnvironment`.
654+
655+
The `keyContexts` property of `BatchLoaderEnvironment` returns the
656+
localContext obtained from the `DataFetchingEnvironment` when the
657+
`DataLoader` was called for each key.
650658

651659

652660
|===

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,9 @@ public ResolvableType getReturnType() {
755755
public Object get(DataFetchingEnvironment env) {
756756
DataLoader<?, ?> dataLoader = env.getDataLoaderRegistry().getDataLoader(this.dataLoaderKey);
757757
Assert.state(dataLoader != null, "No DataLoader for key '" + this.dataLoaderKey + "'");
758-
return dataLoader.load(env.getSource(), (env.getLocalContext() != null) ? env.getLocalContext() : env.getGraphQlContext());
758+
return ((env.getLocalContext() != null) ?
759+
dataLoader.load(env.getSource(), env.getLocalContext()) :
760+
dataLoader.load(env.getSource()));
759761
}
760762
}
761763

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

Lines changed: 13 additions & 2 deletions
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

Lines changed: 5 additions & 1 deletion
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)