Skip to content

Commit 254d0c8

Browse files
committed
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
1 parent 038217a commit 254d0c8

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

spring-graphql/src/main/java/org/springframework/graphql/data/GraphQlArgumentBinder.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2023 the original author or authors.
2+
* Copyright 2020-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -142,6 +142,19 @@ public Object bind(
142142
Object rawValue = (name != null ? environment.getArgument(name) : environment.getArguments());
143143
boolean isOmitted = (name != null && !environment.getArguments().containsKey(name));
144144

145+
return bind(name, rawValue, isOmitted, targetType);
146+
}
147+
148+
/**
149+
* Variant of {@link #bind(DataFetchingEnvironment, String, ResolvableType)}
150+
* with a pre-extracted raw value to bind from.
151+
* @since 1.3
152+
*/
153+
@Nullable
154+
public Object bind(
155+
@Nullable String name, @Nullable Object rawValue, boolean isOmitted, ResolvableType targetType)
156+
throws BindException {
157+
145158
ArgumentsBindingResult bindingResult = new ArgumentsBindingResult(targetType);
146159

147160
Object value = bindRawValue(

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,8 +23,10 @@
2323
import org.springframework.graphql.data.GraphQlArgumentBinder;
2424
import org.springframework.graphql.data.method.HandlerMethodArgumentResolver;
2525
import org.springframework.graphql.data.method.annotation.Argument;
26+
import org.springframework.lang.Nullable;
2627
import org.springframework.util.Assert;
2728
import org.springframework.util.StringUtils;
29+
import org.springframework.validation.BindException;
2830

2931
/**
3032
* Resolver for a method parameter that is annotated with
@@ -58,6 +60,14 @@ public ArgumentMethodArgumentResolver(GraphQlArgumentBinder argumentBinder) {
5860
}
5961

6062

63+
/**
64+
* Return the configured {@link GraphQlArgumentBinder}.
65+
* @since 1.3
66+
*/
67+
public GraphQlArgumentBinder getArgumentBinder() {
68+
return this.argumentBinder;
69+
}
70+
6171
@Override
6272
public boolean supportsParameter(MethodParameter parameter) {
6373
return (parameter.getParameterAnnotation(Argument.class) != null ||
@@ -67,8 +77,19 @@ public boolean supportsParameter(MethodParameter parameter) {
6777
@Override
6878
public Object resolveArgument(MethodParameter parameter, DataFetchingEnvironment environment) throws Exception {
6979
String name = getArgumentName(parameter);
70-
ResolvableType resolvableType = ResolvableType.forMethodParameter(parameter);
71-
return this.argumentBinder.bind(environment, name, resolvableType);
80+
ResolvableType targetType = ResolvableType.forMethodParameter(parameter);
81+
return doBind(environment, name, targetType);
82+
}
83+
84+
/**
85+
* Perform the binding with the configured {@link #getArgumentBinder() binder}.
86+
* @since 1.3
87+
*/
88+
@Nullable
89+
protected Object doBind(
90+
DataFetchingEnvironment environment, String name, ResolvableType targetType) throws BindException {
91+
92+
return this.argumentBinder.bind(environment, name, targetType);
7293
}
7394

7495
static String getArgumentName(MethodParameter parameter) {

0 commit comments

Comments
 (0)