Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for constructor binding on input arguments #139

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions spring-graphql/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.5.31'
}
description = "GraphQL Support for Spring Applications"

dependencies {
Expand Down Expand Up @@ -34,6 +37,7 @@ dependencies {

testRuntimeOnly 'org.apache.logging.log4j:log4j-core'
testRuntimeOnly 'org.apache.logging.log4j:log4j-slf4j-impl'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}

test {
Expand All @@ -42,3 +46,16 @@ test {
events "passed", "skipped", "failed"
}
}
repositories {
mavenCentral()
}
compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.lang.reflect.Constructor;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Stack;
Expand Down Expand Up @@ -107,13 +108,32 @@ private Object convert(Object rawValue, Class<?> targetType) {
Object target;
if (rawValue instanceof Map) {
Constructor<?> ctor = BeanUtils.getResolvableConstructor(targetType);
target = BeanUtils.instantiateClass(ctor);
DataBinder dataBinder = new DataBinder(target);
Assert.isTrue(ctor.getParameterCount() == 0,
() -> "Argument of type [" + targetType.getName() +
"] cannot be instantiated because of missing default constructor.");
MutablePropertyValues mpvs = extractPropertyValues((Map) rawValue);
dataBinder.bind(mpvs);
MutablePropertyValues propertyValues = extractPropertyValues((Map) rawValue);

if (ctor.getParameterCount() == 0) {
target = BeanUtils.instantiateClass(ctor);
DataBinder dataBinder = new DataBinder(target);
dataBinder.bind(propertyValues);
} else {
// Data class constructor
DataBinder binder = new DataBinder(null);
String[] paramNames = BeanUtils.getParameterNames(ctor);
Class<?>[] paramTypes = ctor.getParameterTypes();
Object[] args = new Object[paramTypes.length];
for (int i = 0; i < paramNames.length; i++) {
String paramName = paramNames[i];
Object value = propertyValues.get(paramName);
value = (value instanceof List ? ((List<?>) value).toArray() : value);
MethodParameter methodParam = new MethodParameter(ctor, i);
if (value == null && methodParam.isOptional()) {
args[i] = (methodParam.getParameterType() == Optional.class ? Optional.empty() : null);
}
else {
args[i] = binder.convertIfNecessary(value, paramTypes[i], methodParam);
}
}
target = BeanUtils.instantiateClass(ctor, args);
}
}
else if (targetType.isAssignableFrom(rawValue.getClass())) {
return returnValue(rawValue, targetType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ void shouldResolveJavaBeanArgument() throws Exception {
.hasFieldOrPropertyWithValue("authorId", 42L);
}

@Test
void shouldResolveKotlinBeanArgument() throws Exception {
Method addBook = ClassUtils.getMethod(BookController.class, "ktAddBook", KotlinBookInput.class);
String payload = "{\"bookInput\": { \"name\": \"test name\", \"authorId\": 42} }";
DataFetchingEnvironment environment = initEnvironment(payload);
MethodParameter methodParameter = getMethodParameter(addBook, 0);
Object result = resolver.resolveArgument(methodParameter, environment);
assertThat(result).isNotNull().isInstanceOf(KotlinBookInput.class);
assertThat((KotlinBookInput) result).hasFieldOrPropertyWithValue("name", "test name")
.hasFieldOrPropertyWithValue("authorId", 42L);
}

@Test
void shouldResolveListOfJavaBeansArgument() throws Exception {
Method addBooks = ClassUtils.getMethod(BookController.class, "addBooks", List.class);
Expand Down Expand Up @@ -147,6 +159,11 @@ public Book addBook(@Argument BookInput bookInput) {
return null;
}

@MutationMapping
public Book ktAddBook(@Argument KotlinBookInput bookInput) {
return null;
}

@MutationMapping
public List<Book> addBooks(@Argument List<Book> books) {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package org.springframework.graphql.data.method.annotation.support;

data class KotlinBookInput(val name: String, val authorId: Long)