Skip to content

Commit

Permalink
Merge branch 'master' into dgs-federation-resolver-cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kilink authored Jan 22, 2024
2 parents 2c48eaa + 4dcd6d4 commit f61bf2a
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ internal class CompletableFutureWrapper(private val taskExecutor: AsyncTaskExecu
* Wrap the call to a data fetcher in CompletableFuture to enable parallel behavior.
* Used when virtual threads are enabled.
*/
fun wrapInCompletableFuture(function: () -> Any?): Any? {
return CompletableFuture.supplyAsync({
return@supplyAsync function.invoke()
}, taskExecutor)
fun wrapInCompletableFuture(function: () -> Any?): CompletableFuture<Any?> {
return CompletableFuture.supplyAsync({ function() }, taskExecutor)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import org.springframework.util.CollectionUtils
import org.springframework.util.ReflectionUtils
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method
import java.lang.reflect.UndeclaredThrowableException
import kotlin.reflect.KFunction
import kotlin.reflect.KParameter
import kotlin.reflect.full.callSuspendBy
Expand Down Expand Up @@ -58,12 +59,17 @@ class DataFetcherInvoker internal constructor(
ReflectionUtils.makeAccessible(bridgedMethod)
}

@Throws(Exception::class)
override fun get(environment: DataFetchingEnvironment): Any? {
if (methodParameters.isEmpty()) {
if (completableFutureWrapper.shouldWrapInCompletableFuture(bridgedMethod)) {
return completableFutureWrapper.wrapInCompletableFuture { ReflectionUtils.invokeMethod(bridgedMethod, dgsComponent) }
}
return ReflectionUtils.invokeMethod(bridgedMethod, dgsComponent)
return try {
bridgedMethod.invoke(dgsComponent)
} catch (exc: Exception) {
handleReflectionException(exc)
}
}

if (kotlinFunction != null) {
Expand All @@ -82,7 +88,11 @@ class DataFetcherInvoker internal constructor(
return if (completableFutureWrapper.shouldWrapInCompletableFuture(bridgedMethod)) {
completableFutureWrapper.wrapInCompletableFuture { ReflectionUtils.invokeMethod(bridgedMethod, dgsComponent, *args) }
} else {
ReflectionUtils.invokeMethod(bridgedMethod, dgsComponent, *args)
try {
bridgedMethod.invoke(dgsComponent, *args)
} catch (exc: Exception) {
handleReflectionException(exc)
}
}
}

Expand Down Expand Up @@ -113,19 +123,40 @@ class DataFetcherInvoker internal constructor(
kFunc.callSuspendBy(argsByName)
}.onErrorMap(InvocationTargetException::class.java) { it.targetException }
}
return try {
if (completableFutureWrapper.shouldWrapInCompletableFuture(kFunc)) {
completableFutureWrapper.wrapInCompletableFuture { kFunc.callBy(argsByName) }
} else {
return if (completableFutureWrapper.shouldWrapInCompletableFuture(kFunc)) {
completableFutureWrapper.wrapInCompletableFuture { kFunc.callBy(argsByName) }
} else {
try {
kFunc.callBy(argsByName)
} catch (exc: Exception) {
handleReflectionException(exc)
}
} catch (ex: Exception) {
ReflectionUtils.handleReflectionException(ex)
}
}

private fun formatArgumentError(param: MethodParameter, message: String): String {
return "Could not resolve parameter [${param.parameterIndex}] in " +
param.executable.toGenericString() + if (message.isNotEmpty()) ": $message" else ""
}

/**
* Handle the given reflection exception.
*
* Variant of [ReflectionUtils.handleReflectionException] that allows checked exceptions
* to propagate, but handles [NoSuchMethodException], [IllegalAccessException], and [InvocationTargetException]
* the same way as that helper does; the main difference is that this method that this method will never throw
* [UndeclaredThrowableException].
*/
private fun handleReflectionException(exc: Exception): Nothing {
if (exc is NoSuchMethodException) {
throw IllegalStateException("Method not found: ${exc.message}")
}
if (exc is IllegalAccessException) {
throw IllegalStateException("Could not access method or field: ${exc.message}")
}
if (exc is InvocationTargetException) {
throw exc.targetException
}
throw exc
}
}
Loading

0 comments on commit f61bf2a

Please sign in to comment.