From 254d0c87c50f2467dc3c80a8c49e6fc4c2d3f1c8 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Fri, 2 Feb 2024 14:51:02 +0000 Subject: [PATCH] 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 --- .../graphql/data/GraphQlArgumentBinder.java | 15 ++++++++++- .../ArgumentMethodArgumentResolver.java | 27 ++++++++++++++++--- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/spring-graphql/src/main/java/org/springframework/graphql/data/GraphQlArgumentBinder.java b/spring-graphql/src/main/java/org/springframework/graphql/data/GraphQlArgumentBinder.java index 4bf0d5459..e54efab29 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/data/GraphQlArgumentBinder.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/data/GraphQlArgumentBinder.java @@ -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 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( diff --git a/spring-graphql/src/main/java/org/springframework/graphql/data/method/annotation/support/ArgumentMethodArgumentResolver.java b/spring-graphql/src/main/java/org/springframework/graphql/data/method/annotation/support/ArgumentMethodArgumentResolver.java index 854591d4e..4ff10edbe 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/data/method/annotation/support/ArgumentMethodArgumentResolver.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/data/method/annotation/support/ArgumentMethodArgumentResolver.java @@ -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.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 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 || @@ -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) {