0

I am working on one server that resolve GraphQL queries. I used the graphql-kotlin library: https://github.com/ExpediaDotCom/graphql-kotlin.

I defined three resolve functions(could be viewed as three fields): getxxx(arguments...), getTarget(arguments...) and getSource(arguments...).

The problem is if one of my queried field failed, I will only get the 'errors' field. All other successful executed results(data) are dropped. If I try to catch the exception for the failed field, then I will not get the 'error' field at last.

This is the image that had exception and dropped all fetched data: This is the image that had exception and dropped all fetched data

The objective is returning both the successful fetched data in 'data' field and error message for failed field in 'errors' field. I have checked this: How to return both error and data in a graphql resolver? If I set a field that throw one error or exception intentionally, I will only get 'errors' field at last(Like the picture above).

In addition, many websites like this:https://itnext.io/the-definitive-guide-to-handling-graphql-errors-e0c58b52b5e1 suggest we should return partial result and error message, but in my case if I meet exception, only error message would be returned.

Jeff
  • 21
  • 2
  • 11

1 Answers1

2

In our example application we have two top level queries

{
  onlyCake(msg: "cake")
  generateNumber
}

If I run this code this will return a random number and onlyCake returns the string <3. However if I modify the input of onlyCake to something that is not the string cake I will get an error. This is expected because the field onlyCake has a directive to implement this behavior.

https://github.com/ExpediaDotCom/graphql-kotlin/blob/2961b64d6e4cceb4034aec198d667e5f965decd2/example/src/main/kotlin/com/expedia/graphql/sample/directives/CakeOnlyDirectiveWiring.kt#L18-L19

Screen Shot 2019-06-25 at 5 11 45 PM

The question is though, if I want to return the data still for generateNumber can I do that and have both errors and data in the response?


The answer is it's possible but not with the current schema. The issue is that our schema for onlyCake and generateNumber are both non-nullable fields. So as a client if I see that there is a data field I should expect to see both there otherwise I would have a parsing issue in my code. This is why we can't have this behaviour with the schema as is. If you want to implement this behaviour, the schema developer needs to decide where they can return null for some response and modify the errors field appropriately with a DataFetcherExceptionHandler

Or the other option is that we support the behaviour in graphql-java to return a DataFetcherResult<T> instead of just T from the kotlin functions. See the section Returning data and errors here: https://www.graphql-java.com/documentation/v13/execution/

I have created an issue to continue this discussion with other team members: https://github.com/ExpediaDotCom/graphql-kotlin/issues/244

daYooper
  • 86
  • 5