graphql-spring-boot icon indicating copy to clipboard operation
graphql-spring-boot copied to clipboard

Mapping annotations not respected at the resolver level

Open ackimwilliams opened this issue 6 years ago • 2 comments

Describe the bug Mapping annotation not respected at the resolver function level. You can influence the response from the controller level but not at the resolver level


@Component
class TestComponent(): GraphQLQueryResolver, GraphQLMutationResolver {

    @JsonInclude(JsonInclude.Include.NON_NULL)
    fun excludeNullProps(filter: Input? = null, environment: DataFetchingEnvironment): ResponseObj {...}

    fun includeNullProps(filter: Input? = null, environment: DataFetchingEnvironment): ResponseObj {...}
}

To Reproduce Steps to reproduce the behavior:

  1. Create resolver
  2. Create resolver function
  3. Add mapping annotation at the resolver function level, in this case remove nulls from the json response

Expected behavior Null responses remain in the json response

ackimwilliams avatar Nov 01 '20 20:11 ackimwilliams

Could you try setting that annotation on the return class itself (in this case ResponseObj) instead of on the method? These resolver methods all help in converting it into one Map<String,Object> response which is then serialized using Jackson (if I remember this correctly). Which means that Jackson will never see the annotation you've placed in this location on this method. That's why you'd have a better chance adding this to the actual class, because that should be one of the Objects inside the Map which in turn is actually handled by Jackson.

Having said that: by excluding null values from the response like this it could be that your GraphQL endpoint no longer is compliant with the specification: https://spec.graphql.org/June2018/#sec-Value-Completion. Your users are explicitly requesting certain fields from your GraphQL API. By including fields even though they're null it's clear to the user that the request was executed correctly and it's made explicit that the value in that case is null. That's why the DefaultGraphQLServletObjectMapperConfigurer in graphql-java-servlet explicitly configured Jackson to always include properties when serializing.

oliemansm avatar Nov 17 '20 16:11 oliemansm

I tried adding it to ResponseObj as well as properties of ResponseObj.

Thanks for reminding me of the Value Completion spec of graphql, if certain fields are expected to be null, then its incumbent on the client to not request them if the intent is to reduce the size of the payload by removing null.

Thanks for your response. The best course of action here is to modify the query to exclude expected null fields.

ackimwilliams avatar Nov 17 '20 18:11 ackimwilliams