# @removeIfNull

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 GraphQL by PoP provides this functionality through the custom directive @removeIfNull.

Notice how, when running this query:

query {
  posts(limit:2) {
    id
    title
    featuredImageOrNothing: featuredImage @removeIfNull {
      id
      src
    }
    featuredImage {
      id
      src
    }
  }
}

...the response for the first item doesn't have field featuredImageOrNothing (since featuredImage is null), but the second item does:

{
  "data": {
    "posts": [
      {
        "id": 1153,
        "title": "Scheduled by Leo",
        "featuredImage": null
      },
      {
        "id": 1499,
        "title": "COPE with WordPress: Post demo containing plenty of blocks",
        "featuredImageOrNothing": {
          "id": 1647,
          "src": "https://nextapi.getpop.org/wp/wp-content/uploads/2019/08/IMG_2482.jpg"
        },
        "featuredImage": {
          "id": 1647,
          "src": "https://nextapi.getpop.org/wp/wp-content/uploads/2019/08/IMG_2482.jpg"
        }
      }
    ]
  }
}
Last Updated: 4/20/2020, 7:51:13 AM