Search code examples
javaerror-handlinggraphqlgraphql-java

How to customize Java GraphQL error response


I'm trying to modify graphql error response. I would like to remove null attributes from response. Is it possible?

Current response

I created my GraphQL exception like ExceptionWhileDataFetching and my own definition for filter clients' errors. It works nice but ugly empty attributes are here and I want to remove them.

For example by GraphQL docs attribute extensions should be optional and on second example screen is only message attribute. It is what I want achieve but I cant find out how..

I found GraphqlErrorHelper#toSpecification.. I thought it is enough to override it and GraphQL library will proccess error attributes without other actions, but it didn't..

UPDATE:

I start thinking if I'm trying to do legit thing.. I found out that my ClientException will go correctly to getJacksonMapper().writeValueAsString in GraphQLObjectMapper#serializeResultAsJson and here is response somehow changed.. This part is bit magic for me.. :(

Debug screen


Solution

  • OK, I finally find out solution..

    JacksonSerialer use annotations for appending extra data.

    So if I use @JsonInclude(JsonInclude.Include.NON_NULL) in my GraphQLError class all works as I want..

    Finally result I wanted