Hi folks, i'm trying to figure out how DataFetcher...
# graphql
t
Hi folks, i'm trying to figure out how DataFetcherResult works when there is an error. Essentially I have something like this:
Copy code
fun testQuery(id: Id): DataFetcherResult<String> {
        return DataFetcherResult
            .newResult<String>()
            .error(Failure.App.Unknown)
            .build()
    }
But bc String is non nullable, I get a 'NullValueInNonNullableField' error when this result is returned to my graphql client. If I put
.data(null)
into the builder pattern, then I get an error about String being non nullable and then I get a slightly different error. I assume that if an error occurs, returning null is correct, but I'd prefer to not have nullable return values if I can avoid it?
d
I assume that if an error occurs, returning null is correct, but I’d prefer to not have nullable return values if I can avoid it?
unfortunately this breaks the schema safety
if you define a field as non-nullable and clients asks for it you cannot return null value for it
s
If you need to return just an error you can throw an exception in your function and it will be handled by the
KotlinDataFetcherExceptionHandler
But as mentioned above, if you return a
DataFetcherResult
it should fulfill the schema contract and return both data and errors, even if that data is null
DataFetcherResults
can be used at any level of the schema so you may have a field 10 levels deep in the schema and we need to know how to handle that result. Is just this field an error and should be null or should we throw an exception and fail the entire request
t
Okay got it. Thanks for the clarification 🙏