Skip to content

Commit 74e8c60

Browse files
committed
Support coroutine @GraphQlExceptionHandler methods
Closes gh-750
1 parent fdb7051 commit 74e8c60

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

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

+24-13
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import org.springframework.context.ApplicationContext;
3535
import org.springframework.core.ExceptionDepthComparator;
36+
import org.springframework.core.KotlinDetector;
3637
import org.springframework.core.MethodIntrospector;
3738
import org.springframework.core.MethodParameter;
3839
import org.springframework.core.annotation.AnnotatedElementUtils;
@@ -370,7 +371,11 @@ private interface ReturnValueAdapter {
370371
*/
371372
static ReturnValueAdapter createFor(MethodParameter returnType) {
372373
Class<?> parameterType = returnType.getParameterType();
373-
if (parameterType == void.class) {
374+
Method method = returnType.getMethod();
375+
if (method != null && KotlinDetector.isSuspendingFunction(method)) {
376+
return createForMono(returnType);
377+
}
378+
else if (parameterType == void.class) {
374379
return forVoid;
375380
}
376381
else if (parameterType.equals(GraphQLError.class)) {
@@ -382,18 +387,7 @@ else if (Collection.class.isAssignableFrom(parameterType)) {
382387
}
383388
}
384389
else if (Mono.class.isAssignableFrom(parameterType)) {
385-
returnType = returnType.nested();
386-
Class<?> nestedType = returnType.getNestedParameterType();
387-
if (nestedType == Void.class) {
388-
return forMonoVoid;
389-
}
390-
if (Collection.class.isAssignableFrom(nestedType)) {
391-
returnType = returnType.nested();
392-
nestedType = returnType.getNestedParameterType();
393-
}
394-
if (nestedType.equals(GraphQLError.class) || nestedType.equals(Object.class)) {
395-
return forMono;
396-
}
390+
return createForMono(returnType.nested());
397391
}
398392
else if (parameterType.equals(Object.class)) {
399393
return forObject;
@@ -402,6 +396,23 @@ else if (parameterType.equals(Object.class)) {
402396
"Invalid return type for @GraphQlExceptionHandler method: " + returnType);
403397
}
404398

399+
private static ReturnValueAdapter createForMono(MethodParameter returnType) {
400+
Class<?> nestedType = returnType.getNestedParameterType();
401+
if (nestedType == Void.class) {
402+
return forMonoVoid;
403+
}
404+
if (Collection.class.isAssignableFrom(nestedType)) {
405+
returnType = returnType.nested();
406+
nestedType = returnType.getNestedParameterType();
407+
}
408+
if (nestedType.equals(GraphQLError.class) || nestedType.equals(Object.class)) {
409+
return forMono;
410+
}
411+
throw new IllegalStateException(
412+
"Invalid return type for @GraphQlExceptionHandler method: " + returnType);
413+
}
414+
415+
405416
/** Adapter for void */
406417
ReturnValueAdapter forVoid = (result, returnType, ex) -> Mono.just(Collections.emptyList());
407418

0 commit comments

Comments
 (0)