Say I have a class Foo that looks like this (Kotlin code):
class Foo {
fun ok(): Boolean = true
fun notOk(): Boolean = throw IllegalArgumentException("test")
}
Now say I have a schema where I define the following:
type Query {
getFoo: Foo!
}
type Foo {
ok: Boolean!
notOk: Boolean!
}
Assume that getFoo is in a resolver somewhere. Now if I do the following query:
{
getFoo {
ok
notOk
}
}
The whole thing just fails (as expected), but what I want to achieve is that only the notOk field should turn up in the error part of the response and the ok field should turn up as part of the data part of the response.
For reference, I am using: https://github.com/excitement-engineer/ktor-graphql along with ktor.
As far as I can understand, I need to make a DataFetcherExceptionHandler object, but I don't understand how I can both fulfill the non-erroring fields and the erroring field. Any ideas? All I have achieved so far, is to be able to have errors and data in the same query as long as they are top-level fields.
val exceptionHandler = DataFetcherExceptionHandler { handlerParameters ->
val exception = handlerParameters.exception
val sourceLocation = handlerParameters.sourceLocation
val path = handlerParameters.path
val error = object : GraphQLError {
override fun getMessage() = exception.message
override fun getLocations() = mutableListOf(sourceLocation)
override fun getErrorType() = ErrorType.DataFetchingException
}
DataFetcherExceptionHandlerResult.newResult().error(error).build()
}