1

I want to remove fields from output JSON with default values (0, false) to reduce size of response, but can't find a way how to do it in HotChocolate/GraphQl. I didn't find a settings to control it (JsonFormatter), "Optional" also not for this purposes, and there is not options to do it via query syntax.

After query to graphQl I receive a long list, with many default values in JSON output, simple example, pay attention to "sortOrder" and "isDeleted":

"values": [{
    "id": 448,
    "name": "Dr.",
    "isDeleted": false,
    "sortOrder": 0
}, {
    "id": 449,
    "name": "Mr.",
    "isDeleted": true,
    "sortOrder": 0
}, {
    "id": 450,
    "name": "Mrs.",
    "isDeleted": false,
    "sortOrder": 1
}]

Expecting result:

"values": [{
    "id": 448,
    "name": "Dr.",
}, {
    "id": 449,
    "name": "Mr.",
    "isDeleted": true,
}, {
    "id": 450,
    "name": "Mrs.",
    "sortOrder": 1
}]

Query:

getLookups(withDeleted: true) {
    id
    name
    isDeleted
    values {
      id
      name
      isDeleted
      sortOrder
    }
}

Type description:

descriptor.BindFields(BindingBehavior.Explicit);
descriptor.Interface<ILookupValueType>();
descriptor.Field(f => f.Id).Type<NonNullType<IntType>>();
descriptor.Field(f => f.Name).Type<StringType>();
descriptor.Field(f => f.Image).Type<StringType>();
descriptor.Field(f => f.IsDeleted).Type<BooleanType>();
descriptor.Field(f => f.IsDefault).Type<BooleanType>();
descriptor.Field(f => f.SortOrder).Type<IntType>();

P.S. Please, don't offer pagination, it is not what I need.

And thank you for your help.

Pavel Samoylenko
  • 491
  • 7
  • 14
  • Can you file an issue? I might be able to include it into 12.7 – Michael Ingmar Staib Feb 03 '22 at 07:41
  • @MichaelIngmarStaib wouldn't this violate the spec? I imagine a simple client, maybe even Relay, will have an issue with missing properties on the deserialized Javascript objects. Or are you talking about simply offering the ability to exclude default values for highly controlled scenarios? - I would assume so... – Tobias Tengler Feb 03 '22 at 08:09
  • 1
    @TobiasTengler not really. Since our result map will include the data as per spec. But we give people a hook into the serialization to omit data. You will still need to add some code. You technically can do this already if you implement your own result map serializer. The serialization format is not specified by the spec and the spec is explicit transport agnostic. – Michael Ingmar Staib Feb 04 '22 at 10:00

0 Answers0