I’ve picked up a project that returns different entities depending if its an error or not.
I really like their errors within the data object (plus i know a few calls are being made with this set up) and lets developers loop through nested results and handle errors as and when they occur (specifically 404 errors)
My question is: Is this still a valid way of operating and if so is there documentation and use with data sources?
In the example below:
If there is a 404 or 401 error when querying the root the developer can quickly see/handle the error
If there is a user id stored in an array of organisation->userIds that does not have a corresponding user->id its is easy to handle / see where the error is
{
Organisation(id: "123456-12345678-123456") {
...on Error404 {
message
}
...on Error401 {
message
}
...on Organisation {
name
id
users {
...on Error404 {
# E.g. User with id 'qwerty', not found
message
id
}
...on Error404 {
message
}
...on User {
email
roles
}
}
}
}
}
Here is the article they used to base their existing error handling:
https://blog.logrocket.com/handling-graphql-errors-like-a-champ-with-unions-and-interfaces/