This module adds global field _fail and directive @fail to the GraphQL schema, which add an entry to the errors property in the response.
Field _fail adds the error always, and directive @fail whenever the condition under argument condition is met (either if the field value is null, or empty; by default, it is IS_NULL):
Both of them can also receive argument data, to provide contextual information in the error response.
These schema elements are useful to explicitly indicate that there is an error in the executed GraphQL query, whenever such an error does not happen on natural circumstances.
Then, in our application on the client side (such as JavaScript with a headless setup), we can check if entry errors exists and, based on that, either process the GraphQL response or show an error message to the user:
Retrieving a post with a non-existent ID will naturally return null. If we need to treat this condition as an error, we can use directive @fail:
In combination with the Multiple Query Execution extension, we can obtain the same results using _fail (notice that operation FailIfPostNotExists is not executed whenever $postExists is true):
We can use _fail to ensure that the user with given email does not yet exist:
We can also use _fail to check if retrieving data from an external API produced errors: