2

I am using express graphql in my node app . and it graphql always sends 500 Internal server error for any thrown error from resolver. Please suggest any solution so i will get proper response and status code

Sunny G
  • 191
  • 2
  • 9
  • I wondered why it behaved like this, while others said that express-graphql always returns 200 with possibly `errors` field. Turns out the problem was in the schema. When you specify that certain mutation (say, login) returns non-null result, you actually mean it. And if the user passes incorrect password and you throw error, you return nothing (null). The library will treat this discrepancy as a server error. So set type to nullable when an error can occur in the resolver – Sviatozar Petrenko Jul 06 '22 at 02:46

1 Answers1

2

The express-graphql sends the HTTP error 500 whenever detects there is no data returned. Definitely, in case of exception is thrown there won't be a returning data. Although it looks like an obvious bug the developers have their own opinion on that. They seemed to decide providing an option to disable the feature so that the server shouldn't set the 500 error. At least the open issue on that is still there.

Checking out the sources I've found these options to get around the problem:

  1. To set any other response code than 200 (e.g. 299 (custom)) in your resolver.
  2. To set the response code in your error formatting handler. This seems to be the most optimal solution.
  3. Just to pick up some other library =)