Skip to content

Commit

Permalink
Refactoring in argument binding
Browse files Browse the repository at this point in the history
GraphQlArgumentBinder allows external determination of the raw value to use,
and ArgumentMethodArgumentResolver allows customizing the invocation of
the binder.

See gh-864
  • Loading branch information
rstoyanchev committed Feb 5, 2024
1 parent 038217a commit 254d0c8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -142,6 +142,19 @@ public Object bind(
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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -23,8 +23,10 @@
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
Expand Down Expand Up @@ -58,6 +60,14 @@ public ArgumentMethodArgumentResolver(GraphQlArgumentBinder argumentBinder) {
}


/**
* 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 ||
Expand All @@ -67,8 +77,19 @@ public boolean supportsParameter(MethodParameter parameter) {
@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) {
Expand Down

0 comments on commit 254d0c8

Please sign in to comment.