Lighthouse: [RFC] Utilize HTTP status codes

Created on 24 Aug 2018  路  4Comments  路  Source: nuwave/lighthouse

Currently, Lighthouse always returns responses with a status code of 200. While GraphQL works in such a way that responses contain error information as JSON, it may still be useful to have actual HTTP status codes.

Use cases:

  • Monitoring that tracks the status codes that are returned
  • Distinguish between different kinds of errors easily on the client side

I think it might not be too hard to bubble up errors to the Controller and return a corresponding status code. We would have to work out the semantics of when to return what.

Most helpful comment

I also thought about this, but this does pose a problem when perfoming multiple queries. Consider the following example:

query data {
  federationsPaginated(count: 1) {
    data {
      id
      name
    }
  }
  districtsPaginated(count: 1) {
    data {
      id
      name
    }
  }
}

Response:

{
    "errors": [
        {
            "category": "graphql",
            "message": "Not authorized to access this field.",
            "locations": null
        }
    ],
    "data": {
        "federationsPaginated": null,
        "districtsPaginated": {
            "data": [
                {
                    "id": "0c0679cc-a225-11e8-8d89-5254507c1b9f",
                    "name": "Name"
                }
            ]
        }
    },
    "extensions": []
}

The first query should return a 403, but the second should return 200.

All 4 comments

I also thought about this, but this does pose a problem when perfoming multiple queries. Consider the following example:

query data {
  federationsPaginated(count: 1) {
    data {
      id
      name
    }
  }
  districtsPaginated(count: 1) {
    data {
      id
      name
    }
  }
}

Response:

{
    "errors": [
        {
            "category": "graphql",
            "message": "Not authorized to access this field.",
            "locations": null
        }
    ],
    "data": {
        "federationsPaginated": null,
        "districtsPaginated": {
            "data": [
                {
                    "id": "0c0679cc-a225-11e8-8d89-5254507c1b9f",
                    "name": "Name"
                }
            ]
        }
    },
    "extensions": []
}

The first query should return a 403, but the second should return 200.

Great point! That is a strong argument for always returning 200. While for a single query it might make sense to have another status code, i think it will be too confusing to make an exception.

There might be queries with multiple fields that return a multitude of errors. There is just no reasonable way to put that into a single status code.

Or maybe lets just add up the status code and take the average? /sarcasm

An extension could be created here that holds the status code for each query as well

Closing this, as i feel that @maarten00 comment has clearly shown we can not have HTTP status codes.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vine1993 picture vine1993  路  3Comments

mikeerickson picture mikeerickson  路  3Comments

mehranabi picture mehranabi  路  3Comments

alexwhb picture alexwhb  路  4Comments

a-ssassi-n picture a-ssassi-n  路  3Comments