https://kotlinlang.org logo
#graphql-kotlin
Title
# graphql-kotlin
m

Martin Brehovsky

11/03/2020, 11:36 PM
I’m experimenting with graphq-kotlin and I wonder if it possible to get a response which combines errors and data. Currently when any of my data fetcher fails, the whole response fails and there is no
data
section in the response, only
errors
. Is it possible to get a response which would give me values for fetcher who did not fail and errors for the fetchers which fail? Something like this:
Copy code
{
    "data": {
        "item": {
            "foo": "foo"
        },
    }
    "errors": [
        {
            "message": "Exception while fetching data (error) : I wonder what happends",
            "locations": [
                {
                    "line": 5,
                    "column": 3
                }
            ],
            "path": [
                "bar"
            ],
            "extensions": {}
        }
    ]
}
s

Shane Myrick

11/03/2020, 11:58 PM
m

Martin Brehovsky

11/04/2020, 12:11 AM
I see - so it is not possible to use the framework in a way so I can just throw an exception in my method and make sure this exception gets rolled in into errors?
s

Shane Myrick

11/04/2020, 1:28 AM
Well using DataFetcherResult is using the framework. We have code to handle this return type and extract the info. Throwing an exception breaks the execution so the expectation in that's case is it is an error alone. If you have some other code that may throw an exception you should handle it then return the DataFetcherResult based on your logic
m

Martin Brehovsky

11/04/2020, 1:41 AM
Ok - specifically for my case - I’m using Spring Webflux and Reactor and I wanted to design in a way it returns a valid value for a value coming from Mono/Flux stream and it returns the error in the case the stream fails with error. At this point I’m doing the mapping by extending
SpringDataFetcher
and overriding
get
method to return a future:
Copy code
override fun get(environment: DataFetchingEnvironment): Any? = when (val result = super.get(environment)) {
        is Mono<*> -> result.toFuture()
        else -> result
    }
}
I’m wondering - would it be possible to extend this to do the conversion to DataFetcherResult right here?
s

Shane Myrick

11/04/2020, 6:22 PM
You can use the
willResolveMonad
hook to unwrap any types that you may have. But in the code it self you would still need to return either a
CompletableFuture<DataFetcherResult<T>>
or
DataFetcherResult<T>
https://expediagroup.github.io/graphql-kotlin/docs/schema-generator/execution/async-models#rxjavareactor
m

Martin Brehovsky

11/04/2020, 6:46 PM
Thanks, will try it out later today and let you know.
Ok, so I succeeded doing that - the only “gotcha” was in oder to do this one has to make sure the Mono type parameter is used with nullability, as in the case of an error, we need to make sure the field can be set to null …
4 Views