Skip to main content

Module hasura

Module hasura 

Source
Expand description

The response envelope Hasura clients expect.

async-graphql answers in the shape the GraphQL specification describes: data is always present, errors carry locations and a path that is a list of segments. Hasura answers in a narrower shape of its own, and client code branches on it:

{"errors": [{"message": "...",
             "extensions": {"path": "$.selectionSet.author", "code": "validation-failed"}}]}

Two differences matter to a client. A failed request has no data key at all, rather than "data": null – code written as if (body.data) reads the two the same way, but code written as if ('data' in body) does not. And extensions.code is the machine-readable half: it is what a client switches on to tell a permission failure from a constraint violation, and the message text is only for a human.

The status code is 200 for almost all of this: a GraphQL error is a value in the response body, not a transport failure. Of the 468 cases in Hasura’s own corpus that this server is measured against, 465 answer 200 – including every constraint violation, every refusal to authenticate, and every permission failure at /v1/graphql.

The exception belongs to the endpoint, not to the error. All three cases that answer 400 were sent to /v1alpha1/graphql, the address Hasura served before /v1, and all three are a write refused by the check of an insert or update permission. The same refusal at /v1/graphql is a 200. Five cases prove the second half: identical bodies, identical codes, different status, and the only thing that differs is where they were sent.

This was read out of the corpus’s declared statuses once and read wrongly: taking it for a rule about the error meant answering 400 at /v1/graphql, which is three cases’ worth of wrong. status_for takes the endpoint.

Enums§

Endpoint
Which address the request came in on.

Functions§

allow_unused_variables
Drop variable definitions the document never uses.
denied
A request refused before it was read.
envelope
Convert an async-graphql response into Hasura’s envelope.
malformed
A refusal about the request itself rather than about what it asked for.
not_supported
Hasura’s refusal for one, which is the whole answer.
persisted_query
Whether the request is an Apollo persisted-query handshake.
prepare
Ready a request for execution, and refuse it where async-graphql would not.
status_for
The HTTP status a body is answered with.