Skip to content

Commit b22cef0

Browse files
committed
Support context parameters in @BatchMapping methods
Closes gh-172
1 parent 50aeaba commit b22cef0

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,10 +927,11 @@ the simple class name of the input `List` element type. Both can be customized t
927927
annotation attributes. The type name can also be inherited from a class level
928928
`@SchemaMapping`.
929929

930+
930931
[[controllers-batch-mapping-signature]]
931932
==== Method Signature
932933

933-
Batch mapping methods support two types of arguments:
934+
Batch mapping methods support the following arguments:
934935

935936
[cols="1,2"]
936937
|===
@@ -942,10 +943,19 @@ Batch mapping methods support two types of arguments:
942943
| `java.security.Principal`
943944
| Obtained from Spring Security context, if available.
944945

946+
| `@ContextValue`
947+
| For access to a value from the `GraphQLContext` of `BatchLoaderEnvironment`,
948+
which is the same context as the one from the `DataFetchingEnvironment`.
949+
950+
| `GraphQLContext`
951+
| For access to the context from the `BatchLoaderEnvironment`,
952+
which is the same context as the one from the `DataFetchingEnvironment`.
953+
945954
| `BatchLoaderEnvironment`
946955
| The environment that is available in GraphQL Java to a
947956
`org.dataloader.BatchLoaderWithContext`.
948957

958+
949959
|===
950960

951961
Batch mapping methods can return:

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Collection;
2121
import java.util.Map;
2222

23+
import graphql.GraphQLContext;
2324
import org.dataloader.BatchLoaderEnvironment;
2425
import reactor.core.publisher.Flux;
2526
import reactor.core.publisher.Mono;
@@ -28,6 +29,7 @@
2829
import org.springframework.core.MethodParameter;
2930
import org.springframework.graphql.data.method.HandlerMethod;
3031
import org.springframework.graphql.data.method.InvocableHandlerMethodSupport;
32+
import org.springframework.graphql.data.method.annotation.ContextValue;
3133
import org.springframework.lang.Nullable;
3234
import org.springframework.util.ClassUtils;
3335

@@ -119,6 +121,12 @@ private <K> Object resolveArgument(
119121
collection.addAll(keys);
120122
return collection;
121123
}
124+
else if (parameter.hasParameterAnnotation(ContextValue.class)) {
125+
return ContextValueMethodArgumentResolver.resolveContextValue(parameter, null, environment.getContext());
126+
}
127+
else if (parameterType.equals(GraphQLContext.class)) {
128+
return environment.getContext();
129+
}
122130
else if (parameterType.isInstance(environment)) {
123131
return environment;
124132
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public Object resolveArgument(MethodParameter parameter, DataFetchingEnvironment
5252
}
5353

5454
@Nullable
55-
private Object resolveContextValue(
55+
static Object resolveContextValue(
5656
MethodParameter parameter, @Nullable Object localContext, GraphQLContext graphQlContext) {
5757

5858
ContextValue annotation = parameter.getParameterAnnotation(ContextValue.class);
@@ -78,7 +78,7 @@ private Object resolveContextValue(
7878
return wrapAsOptionalIfNecessary(value, parameterType);
7979
}
8080

81-
private String getValueName(MethodParameter parameter, ContextValue annotation) {
81+
private static String getValueName(MethodParameter parameter, ContextValue annotation) {
8282
if (StringUtils.hasText(annotation.name())) {
8383
return annotation.name();
8484
}
@@ -92,7 +92,7 @@ private String getValueName(MethodParameter parameter, ContextValue annotation)
9292
}
9393

9494
@Nullable
95-
private Object wrapAsOptionalIfNecessary(@Nullable Object value, Class<?> type) {
95+
private static Object wrapAsOptionalIfNecessary(@Nullable Object value, Class<?> type) {
9696
return (type.equals(Optional.class) ? Optional.ofNullable(value) : value);
9797
}
9898

0 commit comments

Comments
 (0)