0
votes

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

1
API fatal error ... debug network request body to check what query (and variables) is problematic - try it in server playground or postman, debug server code - xadm
in postman i get the graphql errors fine but express-graphql returns the 500 status which apollo ios doesnt like, looking to either change the 500 return status if possible or be able to read it in apollo ios client - Dreamystify
it's a network error, not usual graphql error (always 200) - check other error props - xadm
its not a network error. express-graphql returns the error messages in postman with 500 status, its known to do this - Dreamystify
ok, not network error, but not a graphql (user presentable) error, too ;) show full response body - xadm

1 Answers

0
votes

The only time express-graphql will throw 500 errors is if the schema itself is invalid. It will not happen for errors that are thrown inside a resolver -- the status will still be 200 if those are the only errors you encounter.

If the schema is invalid, your server shouldn't start to begin with. You can manually call validateSchema on your schema before starting your server and exit your process with an error if any errors are returned.

const { validateSchema } = require("graphql");

const schemaValidationErrors = validateSchema(schema);
if (schemaValidationErrors.length) {
  console.log('Schema is not valid', schemaValidationErrors);
  process.exit(1);
}