Error Link
Handle and inspect errors in your GraphQL network stack.
Use the onError
link to perform custom logic when a
import { onError } from "@apollo/client/link/error";// Log any GraphQL errors or network error that occurredconst errorLink = onError(({ graphQLErrors, networkError }) => {if (graphQLErrors)graphQLErrors.forEach(({ message, locations, path }) =>console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`));if (networkError) console.log(`[Network error]: ${networkError}`);});
This function is called after the GraphQL operation completes and execution is moving back up your
Options
The function you provide the onError
link is passed an object with the following fields:
Name / Type | Description |
---|---|
operation
| The details of the GraphQL operation that produced an error. |
response
| The (possibly modified) GraphQL result from the server, passed by the next link down the chain (i.e., the link closer to the terminating link). |
graphQLErrors
| An array of |
networkError
| A network error that occurred while attempting to execute the operation, if any. |
forward
| A function that calls the next link down the chain. Calling |
Error categorization
An error is passed as a networkError
if a link further down the chain called the error
callback on the observable. In most cases, graphQLErrors
is the errors
field of the result from the last next
call.
A networkError
can contain additional fields, such as a GraphQL object in the case of a failing HTTP status code. In this situation, graphQLErrors
is an alias for networkError.result.errors
if the property exists.