Consistent access to raw arguments map

See gh-493
This commit is contained in:
rstoyanchev
2022-09-20 12:29:00 +01:00
parent 9abaa23cfc
commit dfed05a822
3 changed files with 130 additions and 6 deletions

View File

@@ -1275,11 +1275,18 @@ See <<controllers-schema-mapping-argument>>.
| For access to all field arguments bound to a higher-level, typed Object.
See <<controllers-schema-mapping-arguments>>.
| `@Argument Map<String, Object>`
| For access to the raw map of arguments, where `@Argument` does not have a
`name` attribute.
| `@Arguments Map<String, Object>`
| For access to the raw map of arguments.
| `@ProjectedPayload` Interface
| For access to field arguments through a project interface.
See <<controllers-schema-mapping-projectedpayload-argument>>.
| Source
| "Source"
| For access to the source (i.e. parent/container) instance of the field.
See <<controllers-schema-mapping-source>>.
@@ -1365,8 +1372,8 @@ are enforced by GraphQL Java.
If binding fails, a `BindException` is raised with binding issues accumulated as field
errors where the `field` of each error is the argument path where the issue occurred.
You can use `@Argument` on a `Map<String, Object>` argument, to obtain all argument
values. The name attribute on `@Argument` must not be set.
You can use `@Argument` with a `Map<String, Object>` argument, to obtain the raw map of
all argument values. The name attribute on `@Argument` must not be set.
@@ -1380,6 +1387,8 @@ For example, `@Argument BookInput bookInput` uses the value of the argument "boo
to initialize `BookInput`, while `@Arguments` uses the full arguments map and in that
case, top-level arguments are bound to `BookInput` properties.
You can use `@Arguments` with a `Map<String, Object>` argument, to obtain the raw map of
all argument values.
[[controllers-schema-mapping-projectedpayload-argument]]

View File

@@ -23,13 +23,18 @@ import graphql.schema.DataFetchingEnvironment;
import org.springframework.core.MethodParameter;
import org.springframework.graphql.data.method.HandlerMethodArgumentResolver;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.Arguments;
import org.springframework.util.StringUtils;
/**
* Resolves a {@link Map} method parameter annotated with an
* {@link Argument @Argument} by returning the GraphQL
* {@link DataFetchingEnvironment#getArguments() arguments} map.
* Resolves a {@link Map} method parameter for access to the raw arguments map.
* Supported with the following:
* <ul>
* <li>{@link Map} argument annotated with {@link Argument @Argument} where the
* annotation does not explicitly specify a name.
* <li>{@link Map} argument annotated with {@link Arguments @Arguments}.
* </ul>
*
* @author Rossen Stoyanchev
* @since 1.0.0
@@ -38,12 +43,21 @@ public class ArgumentMapMethodArgumentResolver implements HandlerMethodArgumentR
@Override
public boolean supportsParameter(MethodParameter parameter) {
return (checkArgumentMap(parameter) || checkArgumentsMap(parameter));
}
private static boolean checkArgumentMap(MethodParameter parameter) {
Argument argument = parameter.getParameterAnnotation(Argument.class);
return (argument != null &&
Map.class.isAssignableFrom(parameter.getParameterType()) &&
!StringUtils.hasText(argument.name()));
}
private static boolean checkArgumentsMap(MethodParameter parameter) {
Arguments argument = parameter.getParameterAnnotation(Arguments.class);
return (argument != null && Map.class.isAssignableFrom(parameter.getParameterType()));
}
@Override
public Object resolveArgument(MethodParameter parameter, DataFetchingEnvironment environment) {
return environment.getArguments();

View File

@@ -0,0 +1,101 @@
/*
* Copyright 2020-2022 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.graphql.data.method.annotation.support;
import java.util.Collections;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.springframework.core.MethodParameter;
import org.springframework.graphql.Book;
import org.springframework.graphql.data.method.HandlerMethodArgumentResolver;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.Arguments;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.stereotype.Controller;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Unit tests for {@link ArgumentMethodArgumentResolver}.
* @author Rossen Stoyanchev
*/
class ArgumentMapMethodArgumentResolverTests extends ArgumentResolverTestSupport {
private final HandlerMethodArgumentResolver resolver = new ArgumentMapMethodArgumentResolver();
@Test
void shouldSupportAnnotatedParameters() {
MethodParameter param = methodParam(BookController.class, "argumentMap", Map.class);
assertThat(this.resolver.supportsParameter(param)).isTrue();
param = methodParam(BookController.class, "argumentsMap", Map.class);
assertThat(this.resolver.supportsParameter(param)).isTrue();
param = methodParam(BookController.class, "argument", Long.class);
assertThat(this.resolver.supportsParameter(param)).isFalse();
param = methodParam(BookController.class, "namedArgumentMap", Map.class);
assertThat(this.resolver.supportsParameter(param)).isFalse();
param = methodParam(BookController.class, "notAnnotated", String.class);
assertThat(this.resolver.supportsParameter(param)).isFalse();
}
@Test
void shouldResolveRawArgumentsMap() throws Exception {
Object result = this.resolver.resolveArgument(
methodParam(BookController.class, "argumentMap", Map.class),
environment("{\"id\": 42 }"));
assertThat(result).isNotNull().isInstanceOf(Map.class).isEqualTo(Collections.singletonMap("id", 42));
}
@SuppressWarnings({"ConstantConditions", "unused"})
@Controller
static class BookController {
@QueryMapping
public Book argumentMap(@Argument Map<?, ?> args) {
return null;
}
@QueryMapping
public Book argumentsMap(@Arguments Map<?, ?> args) {
return null;
}
@QueryMapping
public Book argument(@Argument Long id) {
return null;
}
@QueryMapping
public Book namedArgumentMap(@Argument(name = "book") Map<?, ?> book) {
return null;
}
public void notAnnotated(String param) {
}
}
}