We are used to return different status codes depending on the error, e.g. 401, 404, 500, etc.
Is it recommended to do this in GraphQL? If so, how to do that in express-graphql?
Thanks.
You can't set the response code arbitrarily in express-graphql. In general, I would recommend against trying to use HTTP status codes in GraphQL, because a GraphQL query might be partially successful and return some data, but not all the data you asked for. What status code would you send then?
Any GraphQL client can use the error field in the response to get much more detailed information about any errors in the response.
Got it. Let's say everything runs fine, but one query or one field returns null because of lack of permission.
Would you recommend returning an error saying why that query returned null? Or let it fail silently? I'm thinking of the format:
{
statusCode: 501,
field: "secretField",
message: "Not authorized"
}
@brunolemos That depends on whether the clients need to know (and do something with) the error. If they don't need to know, then you shouldn't return it. As for how to return it, you can just throw an error in the resolver, and let GraphQL.js include that in the response. Errors now also include a path, so it automatically tells you which field in the response was affected.
Ok, thanks for the recommendations :)
I would like to change the default 400 on error to ..
Most helpful comment
Got it. Let's say everything runs fine, but one query or one field returns null because of lack of permission.
Would you recommend returning an error saying why that query returned null? Or let it fail silently? I'm thinking of the format: