Field Response Removal
Addition of the @remove
directive to the GraphQL schema, which removes the output of a field from the response.
Description
The GraphQL spec indicates that the GraphQL response needs to match exactly the shape of the query. However, in certain circumstances we'd rather avoid sending back the response of the field, because:
- We already know what it is, and by not sending it again we can boost performance
- It contains sensitive information (such as login credentials)
- An empty field can be distinguished from a
null
value
By adding @remove
to the field, it will not be printed in the response.
In the query below (that uses the PHP Functions via Schema and HTTP Client extensions), we generate the URL to send an HTTP request to, by concatenating the site domain and the REST API endpoint. As the values of these "components" are not of interest to us, there is no need to print them in the response, and we can @remove
them:
...producing (notice that fields siteURL
and requestURL
are not in the response):
We can also tell directive @remove
to conditionally remove the value, if a condition is met. Argument condition
can receive 3 possible values:
ALWAYS
(default value): Remove it alwaysIS_NULL
: Remove it whenever the value isnull
IS_EMPTY
: Remove it whenever the value is empty
For instance, in the query below, when a post does not have a featured image, field featuredImage
will have value null
. By adding @remove(condition: IS_NULL)
, this value will not be added to the response:
...producing:
Examples
Remove unneeded data from an external API
Let's say we want to retrieve some specific data from an external REST API endpoint, and we don't need the rest of the data. We can then use @remove
to make the response payload smaller, thus boosting performance:
- Use field
_sendJSONObjectItemHTTPRequest
(from the HTTP Client extension) to connect to the REST API - Process this data to extract the needed piece of information (via Field to Input and the
_objectProperty
field from PHP Function via Schema) @remove
the original data from the REST endpoint
This query ties everything together:
In the response to this query, field postData
has been removed:
Avoid printing user credentials
This example connects to the GitHub API to retrieve the artifacts available in a private repository, and avoids printing the user's credentials in the response:
GraphQL spec
This functionality is currently not part of the GraphQL spec, but it has been requested: