Error handling
Querying for errors
When calling a query or mutation, things might go wrong. The input might be invalid, the action you want to perform is invalid for the current state et cetera.
GraphQL allows codifying possible errors in the schema, so you can see in advance what errors to expect. You can query these errors in your queries and mutations, using a so-called inline fragment: ... on Type
.
For example, the requestDeal
mutation can result in a DealAlreadyRequestedError
, which means the deal was requested previously.
You can query for errors individually:
mutation RequestDeal($input: RequestDealInput!) {
requestDeal(input: $input) {
... on RequestDealSuccess {
deal {
id
}
}
... on DealAlreadyRequestedError {
message
code
}
.. on InvalidEmailAddressError {
message
code
}
}
}
Alternatively, you can query the interface all errors share, UserError
:
mutation RequestDeal($input: RequestDealInput!) {
requestDeal(input: $input) {
... on RequestDealSuccess {
deal {
id
}
}
... on UserError {
message
code
}
}
}