Support context parameters in @BatchMapping methods

Closes gh-172
This commit is contained in:
Rossen Stoyanchev
2021-12-08 13:44:19 +00:00
parent 50aeaba63e
commit b22cef00ab
3 changed files with 22 additions and 4 deletions

View File

@@ -927,10 +927,11 @@ the simple class name of the input `List` element type. Both can be customized t
annotation attributes. The type name can also be inherited from a class level
`@SchemaMapping`.
[[controllers-batch-mapping-signature]]
==== Method Signature
Batch mapping methods support two types of arguments:
Batch mapping methods support the following arguments:
[cols="1,2"]
|===
@@ -942,10 +943,19 @@ Batch mapping methods support two types of arguments:
| `java.security.Principal`
| Obtained from Spring Security context, if available.
| `@ContextValue`
| For access to a value from the `GraphQLContext` of `BatchLoaderEnvironment`,
which is the same context as the one from the `DataFetchingEnvironment`.
| `GraphQLContext`
| For access to the context from the `BatchLoaderEnvironment`,
which is the same context as the one from the `DataFetchingEnvironment`.
| `BatchLoaderEnvironment`
| The environment that is available in GraphQL Java to a
`org.dataloader.BatchLoaderWithContext`.
|===
Batch mapping methods can return:

View File

@@ -20,6 +20,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import graphql.GraphQLContext;
import org.dataloader.BatchLoaderEnvironment;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@@ -28,6 +29,7 @@ import org.springframework.core.CollectionFactory;
import org.springframework.core.MethodParameter;
import org.springframework.graphql.data.method.HandlerMethod;
import org.springframework.graphql.data.method.InvocableHandlerMethodSupport;
import org.springframework.graphql.data.method.annotation.ContextValue;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
@@ -119,6 +121,12 @@ public class BatchLoaderHandlerMethod extends InvocableHandlerMethodSupport {
collection.addAll(keys);
return collection;
}
else if (parameter.hasParameterAnnotation(ContextValue.class)) {
return ContextValueMethodArgumentResolver.resolveContextValue(parameter, null, environment.getContext());
}
else if (parameterType.equals(GraphQLContext.class)) {
return environment.getContext();
}
else if (parameterType.isInstance(environment)) {
return environment;
}

View File

@@ -52,7 +52,7 @@ public class ContextValueMethodArgumentResolver implements HandlerMethodArgument
}
@Nullable
private Object resolveContextValue(
static Object resolveContextValue(
MethodParameter parameter, @Nullable Object localContext, GraphQLContext graphQlContext) {
ContextValue annotation = parameter.getParameterAnnotation(ContextValue.class);
@@ -78,7 +78,7 @@ public class ContextValueMethodArgumentResolver implements HandlerMethodArgument
return wrapAsOptionalIfNecessary(value, parameterType);
}
private String getValueName(MethodParameter parameter, ContextValue annotation) {
private static String getValueName(MethodParameter parameter, ContextValue annotation) {
if (StringUtils.hasText(annotation.name())) {
return annotation.name();
}
@@ -92,7 +92,7 @@ public class ContextValueMethodArgumentResolver implements HandlerMethodArgument
}
@Nullable
private Object wrapAsOptionalIfNecessary(@Nullable Object value, Class<?> type) {
private static Object wrapAsOptionalIfNecessary(@Nullable Object value, Class<?> type) {
return (type.equals(Optional.class) ? Optional.ofNullable(value) : value);
}