🚀 Added @removeIfNull directive to GraphQL by PoP

— 2 minute read

Directives allow to implement those features which are not natively-supported by the GraphQL spec. Then, if any implementor has come up with a compelling way to address a problem, it may be decided to incorporate it into the spec.

Being able to difference between null and omissions in the query response is a great example of a feature that can be tackled through directives. When retrieving data through GraphQL, we may sometimes want to remove a field from the response when its value is null. However, GraphQL currently does not support this feature.

So I decided to implement it as a directive: @removeIfNull. Its code barely occupies a few lines, with this logic:

  • Check the value of the field
  • If it is null, unset it from the object

Tadaaa, that's it! Check it out here: