GraphQL Mesh v0 documentation (superseded by v1): Learn how to handle errors in GraphQL Mesh, including REST API errors by status code and authentication errors.
GraphQL Mesh forwards errors from downstream REST/HTTP sources to the GraphQL client by default.
This guide explains how to map HTTP error responses (such as 404 Not Found or 401 Unauthorized)
to proper GraphQL types and how to handle authentication errors.
REST API errors
When GraphQL Mesh calls an upstream REST API and receives a non-2xx HTTP status code for which no
mapping is configured, it returns a GraphQL error with the DOWNSTREAM_SERVICE_ERROR extension code
and the full HTTP response details.
Mapping responses by status code
Both the JSON Schema handler and the
OpenAPI handler support the responseByStatusCode operation option. It
lets you define a distinct response schema (or sample) for every HTTP status code that the upstream
API can return, so that error payloads become typed GraphQL fields instead of generic error
messages.
.meshrc.yaml
sources: - name: MyApi handler: jsonSchema: endpoint: https://api.example.com operations: - type: Query field: book path: /books/{args.id} method: GET # Default (2xx) response schema responseSchema: ./schemas/book.json responseByStatusCode: # Provide a typed response for 404 404: responseSample: ./samples/book-not-found.json responseTypeName: BookNotFound # Provide a typed response for 500 500: responseSample: ./samples/internal-error.json responseTypeName: InternalError
GraphQL Mesh will generate a union type that includes all the mapped response types:
type Query { book(id: ID): BookResult}union BookResult = Book | BookNotFound | InternalErrortype Book { id: ID! title: String}type BookNotFound { message: String!}type InternalError { message: String!}
Clients can then use inline fragments to handle each case:
query GetBook($id: ID!) { book(id: $id) { ... on Book { title } ... on BookNotFound { message } ... on InternalError { message } }}
Using a JSON Schema instead of a sample
You can point responseSchema at a JSON Schema file (or a $ref path within one) rather than
providing a sample:
The OpenAPI handler inherits all jsonSchema handler options, so responseByStatusCode works
identically. If your OpenAPI spec already defines response schemas per status code, GraphQL Mesh
will use them automatically; responseByStatusCode lets you add or override them:
Authentication (401 Unauthorized) and authorization (403 Forbidden) errors returned by upstream
APIs can be mapped with responseByStatusCode just like any other status code:
If you prefer upstream auth failures to surface as GraphQL-level errors (rather than typed union
members), you can use additionalResolvers to inspect the response and throw a GraphQLError: