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

Patrick S.

03/29/2023, 4:42 PM
Huge thanks to @Dariusz Kuc and everyone else building and supporting graphql-kolin. We use it extensively and are looking to improve the error handling by providing an error code to the clients. We would like to use the GraphQL error extensions as specified here: https://spec.graphql.org/October2021/#sec-Errors.Error-result-format All Kotlin custom exceptions in our application which are thrown into the GQL layer have an additional field (errorCode) which should be exposed in a error/extensions/errorCode field. Therefore we would like to hook into the ExceptionResolver and write a custom
graphql.GraphQLError
which contains the extension in the error response. So far we have tried 1. Extend the Spring
DataFetcherExceptionResolverAdapter
however this bean is not directly related to kotlin-graphql and thus has no effect. (Followed this approach https://www.baeldung.com/spring-graphql-error-handling#2-graphql-response-with-handled-exception) 2. Tried to hook into
com.expediagroup.graphql.generator.federation.exception.FederatedRequestFailure
but I do not find the option to replace the RequestFailure —> ErrorCode mapping with a custom implementation. 3. Tried to understand
KotlinDataFetcherFactoryProvider
(link) as the docs make it sound like it may be possible to modify fetchers. Here I can’t find anything concerning exception handling though. Any advice on how set the error extension values is much appreciated! Keep up the amazing work! (I also can ask this question on StackOverflow to ensure long-term findability)
d

Dariusz Kuc

03/29/2023, 7:03 PM
hello 👋 if your thrown exceptions implement
graphql-java
graphql.GraphQLError
then exception will be returned as-is without the need for any transformation
actually you can also provide custom
graphql.execution.DataFetcherExceptionHandler
that could map your exceptions to
GraphQLError
(your bean will override the default one -> https://github.com/ExpediaGroup/graphql-kotlin/blob/master/servers/graphql-kotlin-[…]diagroup/graphql/server/spring/GraphQLExecutionConfiguration.kt)
p

Patrick S.

03/29/2023, 8:35 PM
Amazing, I appreciate it a lot @Dariusz Kuc! I used the second approach as it keeps GraphQL-specific dependencies out of the exceptions. It works extremely well! Generally I love how extensible this whole library is. Thank you for all your work!
d

Dariusz Kuc

03/29/2023, 8:37 PM
👍 🚀
14 Views