Refactoring in argument binding

GraphQlArgumentBinder allows external determination of the raw value to use,
and ArgumentMethodArgumentResolver allows customizing the invocation of
the binder.

See gh-864
This commit is contained in:
rstoyanchev
2024-02-02 14:51:02 +00:00
parent 038217a686
commit 254d0c87c5
2 changed files with 38 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2020-2023 the original author or authors.
* Copyright 2020-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -142,6 +142,19 @@ public class GraphQlArgumentBinder {
Object rawValue = (name != null ? environment.getArgument(name) : environment.getArguments());
boolean isOmitted = (name != null && !environment.getArguments().containsKey(name));
return bind(name, rawValue, isOmitted, targetType);
}
/**
* Variant of {@link #bind(DataFetchingEnvironment, String, ResolvableType)}
* with a pre-extracted raw value to bind from.
* @since 1.3
*/
@Nullable
public Object bind(
@Nullable String name, @Nullable Object rawValue, boolean isOmitted, ResolvableType targetType)
throws BindException {
ArgumentsBindingResult bindingResult = new ArgumentsBindingResult(targetType);
Object value = bindRawValue(

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,8 +23,10 @@ import org.springframework.graphql.data.ArgumentValue;
import org.springframework.graphql.data.GraphQlArgumentBinder;
import org.springframework.graphql.data.method.HandlerMethodArgumentResolver;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindException;
/**
* Resolver for a method parameter that is annotated with
@@ -58,6 +60,14 @@ public class ArgumentMethodArgumentResolver implements HandlerMethodArgumentReso
}
/**
* Return the configured {@link GraphQlArgumentBinder}.
* @since 1.3
*/
public GraphQlArgumentBinder getArgumentBinder() {
return this.argumentBinder;
}
@Override
public boolean supportsParameter(MethodParameter parameter) {
return (parameter.getParameterAnnotation(Argument.class) != null ||
@@ -67,8 +77,19 @@ public class ArgumentMethodArgumentResolver implements HandlerMethodArgumentReso
@Override
public Object resolveArgument(MethodParameter parameter, DataFetchingEnvironment environment) throws Exception {
String name = getArgumentName(parameter);
ResolvableType resolvableType = ResolvableType.forMethodParameter(parameter);
return this.argumentBinder.bind(environment, name, resolvableType);
ResolvableType targetType = ResolvableType.forMethodParameter(parameter);
return doBind(environment, name, targetType);
}
/**
* Perform the binding with the configured {@link #getArgumentBinder() binder}.
* @since 1.3
*/
@Nullable
protected Object doBind(
DataFetchingEnvironment environment, String name, ResolvableType targetType) throws BindException {
return this.argumentBinder.bind(environment, name, targetType);
}
static String getArgumentName(MethodParameter parameter) {