Support context parameters in @BatchMapping methods
Closes gh-172
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user