Docs
Launch GraphOS Studio

Error Link

Handle and inspect errors in your GraphQL network stack.


We recommend reading Apollo Link overview before learning about individual links.

Use the onError link to perform custom logic when a GraphQL or network error occurs. You pass this link a function that's executed if an returns one or more errors:

import { onError } from "@apollo/client/link/error";
// Log any GraphQL errors or network error that occurred
const 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 completes and execution is moving back up your link chain. The function should not return a value unless you want to retry the operation.

Options

The function you provide the onError link is passed an object with the following :

Name /
Type
Description
operation

Operation

The details of the that produced an error.

See type definition

response

ExecutionResult

The (possibly modified) result from the server, passed by the next link down the chain (i.e., the link closer to the terminating link).

See type definition

graphQLErrors

ReadonlyArray<GraphQLError>

An array of GraphQL errors that occurred while executing the , if any.

See type definition

networkError

Error | ServerError | ServerParseError

A network error that occurred while attempting to execute the , if any.

forward

function

A function that calls the next link down the chain. Calling return forward(operation) in your onError callback retries the operation, returning a new observable for the upstream link to subscribe to.

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 of the result from the last next call.

A networkError can contain additional , such as a object in the case of a failing HTTP status code. In this situation, graphQLErrors is an for networkError.result.errors if the property exists.

Previous
Context
Next
Persisted Queries
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy

Company