Skip to content

Commit 2e229fe

Browse files
committed
Polishing contribution
Closes gh-604
1 parent 3450523 commit 2e229fe

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

spring-graphql/src/main/java/org/springframework/graphql/data/method/annotation/support/DataFetcherHandlerMethod.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,18 @@ public Object invoke(DataFetchingEnvironment environment) {
126126
}) :
127127
toArgsMono(args).flatMap(argValues -> {
128128
Object result = validateAndInvoke(argValues, environment);
129-
if (result instanceof Mono) {
130-
return (Mono<?>) result;
129+
if (result instanceof Mono<?> mono) {
130+
return mono;
131131
}
132-
133-
if (result instanceof Flux) {
134-
return Flux.from((Flux<?>) result).collectList();
132+
else if (result instanceof Flux<?> flux) {
133+
return Flux.from(flux).collectList();
135134
}
136-
137-
if (result instanceof CompletableFuture<?>) {
138-
return Mono.fromFuture((CompletableFuture<?>) result);
135+
else if (result instanceof CompletableFuture<?> future) {
136+
return Mono.fromFuture(future);
137+
}
138+
else {
139+
return Mono.justOrEmpty(result);
139140
}
140-
141-
return Mono.justOrEmpty(result);
142141
});
143142
}
144143

spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/DataFetcherHandlerMethodTests.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@
2626
import graphql.schema.DataFetchingEnvironmentImpl;
2727
import org.junit.jupiter.api.Test;
2828
import org.mockito.Mockito;
29+
import reactor.core.publisher.Mono;
2930

3031
import org.springframework.core.task.SimpleAsyncTaskExecutor;
3132
import org.springframework.graphql.data.GraphQlArgumentBinder;
@@ -35,8 +36,9 @@
3536
import org.springframework.graphql.data.method.annotation.Argument;
3637
import org.springframework.graphql.data.method.annotation.QueryMapping;
3738
import org.springframework.lang.Nullable;
39+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
40+
import org.springframework.security.core.userdetails.User;
3841
import org.springframework.util.ClassUtils;
39-
import reactor.core.publisher.Mono;
4042

4143
import static org.assertj.core.api.Assertions.assertThat;
4244

@@ -87,24 +89,20 @@ void callableReturnValue() throws Exception {
8789
}
8890

8991
@Test
90-
void completableFutureReturnValue() throws Exception {
92+
void completableFutureReturnValue() {
9193

9294
HandlerMethodArgumentResolverComposite resolvers = new HandlerMethodArgumentResolverComposite();
95+
resolvers.addResolver(new AuthenticationPrincipalArgumentResolver((beanName, context) -> null));
9396
resolvers.addResolver(new ArgumentMethodArgumentResolver(new GraphQlArgumentBinder()));
9497

9598
DataFetcherHandlerMethod handlerMethod = new DataFetcherHandlerMethod(
96-
handlerMethodFor(new TestController(), "handleAndReturnsCompletableFuture"), resolvers, null,
97-
new SimpleAsyncTaskExecutor(), false);
99+
handlerMethodFor(new TestController(), "handleAndReturnFuture"), resolvers,
100+
null, null, false);
98101

99-
DataFetchingEnvironment environment = DataFetchingEnvironmentImpl
100-
.newDataFetchingEnvironment()
101-
.build();
102-
103-
Object result = handlerMethod.invoke(environment);
102+
Object result = handlerMethod.invoke(DataFetchingEnvironmentImpl.newDataFetchingEnvironment().build());
104103

105104
assertThat(result).isInstanceOf(Mono.class);
106-
Mono<String> mono = (Mono<String>) result;
107-
assertThat(mono.block()).isEqualTo("B");
105+
assertThat(((Mono<String>) result).block()).isEqualTo("B");
108106
}
109107

110108
private static HandlerMethod handlerMethodFor(Object controller, String methodName) {
@@ -133,9 +131,10 @@ public Callable<String> handleAndReturnCallable() {
133131
return () -> "A";
134132
}
135133

136-
public CompletableFuture<String> handleAndReturnsCompletableFuture() {
134+
public CompletableFuture<String> handleAndReturnFuture(@AuthenticationPrincipal User user) {
137135
return CompletableFuture.completedFuture("B");
138136
}
137+
139138
}
140139

141140
}

0 commit comments

Comments
 (0)