graphql-go / graphql-go/graphql
Eliminating null fields in JSON output
- Dominant language
- Go
- Stars
- 10.1k
- Forks
- 845
- PR merge metrics
- No merged PRs in 30d
Description
The resolver pipeline seems to like explicit null fields. For example, if you do something like `query { things { id, name } }`, you might end up with something like:
```json
{
"results": {
"things": [
{"id": "1", "name": null},
{"id": "2", "name": null},
{"id": "3", "name": null},
{"id": "4", "name": null},
{"id": "5", "name": "a"}
]
}
}
```
but a frontend would generally be perfectly happy with:
```json
{
"results": {
"things": [
{"id": "1"},
{"id": "2"},
{"id": "3"},
{"id": "4"},
{"id": "5", "name": "a"}
]
}
}
```
Considering the `things` result only, that's a space saving of 20%. For large result sets, the aggregate size of all null values can be considerable. A real-world result set of ours is 273KB with nulls, but 189KB without nulls, which is a space saving factor of 41%.
I could do a pass over our results to eliminate nulls (possibly expensive, reflection-heavy and error-prone) or use a JSON encoder that eliminates nulls or allows filtering (not sure if that exists). But it would be a lot simpler if the resolver code could have a mode where object keys that are null wouldn't be emitted in the first place. Thoughts?
Contributor guide
Assessment
This issue has not been assessed yet.