I have created an express-graphql server where in the resolvers if it encounters and error, an Error object is thrown. This results in an error array with the error and a 500 http status.
Ive created an swiftui app using apollo for the graphql client. 500 statuses go into the failed function which i cant read the errors from the server.
I have looked into changing the http status in 500 to no avail. Has anyone else had this issue? How to solve it?
in postman i get:
{
"errors": [
{
"message": "Email invalid",
"locations": [
{
"line": 2,
"column": 5
}
],
"path": [
"recover"
]
}
],
"data": null,
"extensions": {
"runTime": 198
}
}
And in the resolver I throw
if (!email) {
throw new HTTP404Error('Email invalid');
}
The error class
class HTTP404Error extends HTTPClientError {
constructor(message = 'Not found') {
super(message);
this.statusCode = 404;
}
}
In my catch block is as follows
.catch((e) => {
if (e instanceof PrismaClientValidationError) {
throw new HTTP409Error('Validation error');
} else {
if (e.code === 'P2002') {
throw new HTTP409Error('Constraint violation');
}
throw new HTTP400Error(e.message);
}
})
As the app is a hybrid rest and graphql server, all thrown errors in the rest endpoints like 'metrics', 'healthcheck' are handled as expected. its just the graphql endpoint that express-graphql handles all errors as 500 with a result