Fix issue returning CompletableFuture

Closes gh-628
This commit is contained in:
rstoyanchev
2023-03-08 13:45:28 +00:00
parent 3586edea1c
commit 9066c4075f
2 changed files with 33 additions and 5 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -16,6 +16,7 @@
package org.springframework.graphql.data.method.annotation.support;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import graphql.schema.DataFetchingEnvironment;
@@ -133,11 +134,14 @@ public class DataFetcherHandlerMethod extends InvocableHandlerMethodSupport {
}) :
toArgsMono(args).flatMap(argValues -> {
Object result = validateAndInvoke(argValues, environment);
if (result instanceof Mono) {
if (result instanceof Mono<?>) {
return (Mono<?>) result;
}
else if (result instanceof Flux) {
return Flux.from((Flux<?>) result).collectList();
else if (result instanceof Flux<?>) {
return ((Flux<?>) result).collectList();
}
else if (result instanceof CompletableFuture<?>) {
return Mono.fromFuture((CompletableFuture<?>) result);
}
else {
return Mono.justOrEmpty(result);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -26,6 +26,7 @@ import graphql.schema.DataFetchingEnvironment;
import graphql.schema.DataFetchingEnvironmentImpl;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import reactor.core.publisher.Mono;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.graphql.data.GraphQlArgumentBinder;
@@ -35,6 +36,8 @@ import org.springframework.graphql.data.method.HandlerMethodArgumentResolverComp
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.lang.Nullable;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;
import org.springframework.util.ClassUtils;
import static org.assertj.core.api.Assertions.assertThat;
@@ -86,6 +89,23 @@ public class DataFetcherHandlerMethodTests {
assertThat(future.get()).isEqualTo("A");
}
@Test
void completableFutureReturnValue() {
HandlerMethodArgumentResolverComposite resolvers = new HandlerMethodArgumentResolverComposite();
resolvers.addResolver(new AuthenticationPrincipalArgumentResolver((beanName, context) -> null));
resolvers.addResolver(new ArgumentMethodArgumentResolver(new GraphQlArgumentBinder()));
DataFetcherHandlerMethod handlerMethod = new DataFetcherHandlerMethod(
handlerMethodFor(new TestController(), "handleAndReturnFuture"), resolvers,
null, null, false);
Object result = handlerMethod.invoke(DataFetchingEnvironmentImpl.newDataFetchingEnvironment().build());
assertThat(result).isInstanceOf(Mono.class);
assertThat(((Mono<String>) result).block()).isEqualTo("B");
}
private static HandlerMethod handlerMethodFor(Object controller, String methodName) {
Method method = ClassUtils.getMethod(controller.getClass(), methodName, (Class<?>[]) null);
return new HandlerMethod(controller, method);
@@ -112,6 +132,10 @@ public class DataFetcherHandlerMethodTests {
return () -> "A";
}
public CompletableFuture<String> handleAndReturnFuture(@AuthenticationPrincipal User user) {
return CompletableFuture.completedFuture("B");
}
}
}