Return custom HTTP status codes #1874
-
Hi 👋, I start using your library to provide a GraphQL API endpoint. I am working on the error handling right now. I read your documentation Exceptions and Partial Data regarding this. Returning errors like described there works very well. I am coming from the REST world and there I am used to return corresponding HTTP Status codes in case that sth went wrong. So fe. when my code throws an IllegalArgumentException somewhere, I like to return fe. HTTP status code 400 (bad request) and an error json:
Can you please tell me whether sth similar is possible in graphql and with your library? Probably not with Can I throw somehow a custom graphql exception which produces a similar result like I would like it to have? Thanks in regards for any suggestion on this. Please be patient with me, it is my 1st GQL server project 🙈 |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
So to maybe wireframe my question a bit in code, if I have a mutation like this:
It would be nice if in case that ATM in case of an exception the server returns this and HTTP status code 200:
I am really thankful for any hint on this. I already read that it is not usual that graphql server return "error http status codes" in case of errors like fe. REST APIs would do. But I also saw Rust GQL server doing this fe. And you also do this in case that I try to read out headers within my mutation by using And also what I would like the server to do in case an exception is thrown within my code. |
Beta Was this translation helpful? Give feedback.
-
Hello 👋 As an FYI, there is a draft spec for implementing GraphQL over HTTP that attempts to add back some of the status codes back to the responses -> https://graphql.github.io/graphql-over-http/draft. It introduces new While we don't cover some of the optional behavior (related to new |
Beta Was this translation helpful? Give feedback.
-
Hi @dariuszkuc, thanks a lot for your answer and the explanations. That helped a lot. I reviewed them and make some thoughts about it. I understood the points and how to realize the error handling within a GraphQL library 🙂. So thanks a lot for this. My favorite solution from Guide to GraphQL Errors is the "Stage 6a: Error Union List + Interface" 🙂. I hope that this can be build with the |
Beta Was this translation helpful? Give feedback.
Hello 👋
In general, as long as GraphQL server can resolve your request and respond with well formed response then the return status should be 200. There are number of great talks/blog posts that cover this topic - I highly recommend to check out Guide to GraphQL Errors by Marc-Andre and 200 OK! talk from Sasha.
As an FYI, there is a draft spec for implementing GraphQL over HTTP that attempts to add back some of the status codes back to the responses -> https://graphql.github.io/graphql-over-http/draft. It introduces new
application/graphql-response+json
media type that allows you to specify custom status code and "should" become the "standard" starting Jan 1st 2025. Until then the standar…