Skip to content

Commit

Permalink
Support coroutine @GraphQlExceptionHandler methods
Browse files Browse the repository at this point in the history
Closes gh-750
  • Loading branch information
rstoyanchev committed Jul 18, 2023
1 parent fdb7051 commit 74e8c60
Showing 1 changed file with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import org.springframework.context.ApplicationContext;
import org.springframework.core.ExceptionDepthComparator;
import org.springframework.core.KotlinDetector;
import org.springframework.core.MethodIntrospector;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.AnnotatedElementUtils;
Expand Down Expand Up @@ -370,7 +371,11 @@ private interface ReturnValueAdapter {
*/
static ReturnValueAdapter createFor(MethodParameter returnType) {
Class<?> parameterType = returnType.getParameterType();
if (parameterType == void.class) {
Method method = returnType.getMethod();
if (method != null && KotlinDetector.isSuspendingFunction(method)) {
return createForMono(returnType);
}
else if (parameterType == void.class) {
return forVoid;
}
else if (parameterType.equals(GraphQLError.class)) {
Expand All @@ -382,18 +387,7 @@ else if (Collection.class.isAssignableFrom(parameterType)) {
}
}
else if (Mono.class.isAssignableFrom(parameterType)) {
returnType = returnType.nested();
Class<?> nestedType = returnType.getNestedParameterType();
if (nestedType == Void.class) {
return forMonoVoid;
}
if (Collection.class.isAssignableFrom(nestedType)) {
returnType = returnType.nested();
nestedType = returnType.getNestedParameterType();
}
if (nestedType.equals(GraphQLError.class) || nestedType.equals(Object.class)) {
return forMono;
}
return createForMono(returnType.nested());
}
else if (parameterType.equals(Object.class)) {
return forObject;
Expand All @@ -402,6 +396,23 @@ else if (parameterType.equals(Object.class)) {
"Invalid return type for @GraphQlExceptionHandler method: " + returnType);
}

private static ReturnValueAdapter createForMono(MethodParameter returnType) {
Class<?> nestedType = returnType.getNestedParameterType();
if (nestedType == Void.class) {
return forMonoVoid;
}
if (Collection.class.isAssignableFrom(nestedType)) {
returnType = returnType.nested();
nestedType = returnType.getNestedParameterType();
}
if (nestedType.equals(GraphQLError.class) || nestedType.equals(Object.class)) {
return forMono;
}
throw new IllegalStateException(
"Invalid return type for @GraphQlExceptionHandler method: " + returnType);
}


/** Adapter for void */
ReturnValueAdapter forVoid = (result, returnType, ex) -> Mono.just(Collections.emptyList());

Expand Down

0 comments on commit 74e8c60

Please sign in to comment.