Merge branch '1.0.x'

This commit is contained in:
rstoyanchev
2022-09-20 14:06:57 +01:00
5 changed files with 194 additions and 28 deletions

View File

@@ -339,10 +339,14 @@ parsed and merged together. That means schema files can be loaded from just abou
location.
By default, the Spring Boot starter
{spring-boot-ref-docs}/web.html#web.graphql.schema[finds schema files] from a
well-known classpath location, but you can change that to a location on the file system
via `FileSystemResource`, to byte content via `ByteArrayResource`, or implement a custom
`Resource` that loads schema files from a remote location or storage.
{spring-boot-ref-docs}/web.html#web.graphql.schema[looks for schema files] with extensions
".graphqls" or ".gqls" under the location `classpath:graphql/**`, which is typically
`src/main/resources/graphql`. You can also use a file system location, or any location
supported by the Spring `Resource` hierarchy, including a custom implementation that
loads schema files from remote locations, from storage, or from memory.
TIP: Use `classpath*:graphql/**/` to find schema files across multiple classpath
locations, e.g. across multiple modules.
[[execution-graphqlsource-schema-creation]]
@@ -1272,11 +1276,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>>.
@@ -1362,8 +1373,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.
@@ -1377,6 +1388,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

@@ -248,7 +248,7 @@ public class GraphQlArgumentBinder {
Object target;
Constructor<?> ctor = BeanUtils.getResolvableConstructor(targetType);
// Default constructor with data binding
// Default constructor + data binding via properties
if (ctor.getParameterCount() == 0) {
target = BeanUtils.instantiateClass(ctor);
@@ -284,6 +284,9 @@ public class GraphQlArgumentBinder {
if (rawValue == null && methodParam.isOptional()) {
args[i] = (paramTypes[i] == Optional.class ? Optional.empty() : null);
}
else if (paramTypes[i] == Object.class) {
args[i] = rawValue;
}
else if (isApproximableCollectionType(rawValue)) {
ResolvableType elementType = ResolvableType.forMethodParameter(methodParam);
args[i] = createCollection((Collection<Object>) rawValue, elementType, bindingResult, segments);

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

@@ -280,6 +280,21 @@ class GraphQlArgumentBinderTests {
});
}
@Test // gh-447
@SuppressWarnings("unchecked")
void primaryConstructorWithGenericObject() throws Exception {
Object result = this.binder.bind(
environment("{\"key\":{\"value\":[{\"name\":\"first\"},{\"name\":\"second\"}]}}"), "key",
ResolvableType.forClass(ObjectHolder.class));
assertThat(result).isNotNull().isInstanceOf(ObjectHolder.class);
List<Map<Object, Object>> list = (List<Map<Object, Object>>) ((ObjectHolder) result).getValue();
assertThat(list).hasSize(2).containsExactly(
Collections.singletonMap("name", "first"),
Collections.singletonMap("name", "second"));
}
@Test // gh-410
@SuppressWarnings("unchecked")
void coercionWithSingletonList() throws Exception {
@@ -332,6 +347,7 @@ class GraphQlArgumentBinderTests {
}
@SuppressWarnings("unused")
static class SimpleBean {
private String name;
@@ -434,6 +450,7 @@ class GraphQlArgumentBinderTests {
}
@SuppressWarnings("unused")
static class NoPrimaryConstructorBean {
NoPrimaryConstructorBean(String name) {
@@ -444,6 +461,7 @@ class GraphQlArgumentBinderTests {
}
@SuppressWarnings("unused")
static class ItemListHolder {
private List<Item> items;
@@ -458,6 +476,40 @@ class GraphQlArgumentBinderTests {
}
@SuppressWarnings("unused")
static class ItemSetHolder {
private Set<Item> items;
public ItemSetHolder(Set<Item> items) {
this.items = items;
}
public Set<Item> getItems() {
return items;
}
public void setItems(Set<Item> items) {
this.items = items;
}
}
static class ObjectHolder {
private final Object value;
ObjectHolder(Object value) {
this.value = value;
}
public Object getValue() {
return value;
}
}
@SuppressWarnings("unused")
static class Item {
private String name;
@@ -494,21 +546,4 @@ class GraphQlArgumentBinderTests {
}
}
static class ItemSetHolder {
private Set<Item> items;
public ItemSetHolder(Set<Item> items) {
this.items = items;
}
public Set<Item> getItems() {
return items;
}
public void setItems(Set<Item> items) {
this.items = items;
}
}
}

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) {
}
}
}