How to respond with different status code in GraphQL?

Technology CommunityCategory: GraphQLHow to respond with different status code in GraphQL?
VietMX Staff asked 5 months ago

The way to return errors in GraphQL (at least in graphql-js) is to throw errors inside the resolve functions. Because HTTP status codes are specific to the HTTP transport and GraphQL doesn’t care about the transport, there’s no way for you to set the status code there. What you can do instead is throw a specific error inside your resolve function:

age: (person, args) => {
  try {
    return fetchAge(;
  } catch (e) {
    throw new Error("Could not connect to age service");

GraphQL errors get sent to the client in the response like so:

  "data": {
    "name": "John",
    "age": null
  "errors": [
    { "message": "Could not connect to age service" }

And if the message is not enough information, you could create a special error class for your GraphQL server which includes a status code.