Error Handling in Client Libraries
You'll learn how to gracefully handle GraphQL errors, network failures, and partial data responses in your UI.
The Three Types of Errors
When working with GraphQL client libraries like Apollo Client, you'll encounter three distinct error scenarios:
GraphQL Errors occur when your query executes but the server reports problems with specific fields (like permission denied or validation failures). These arrive in the response's errors array alongside potentially valid data.
Network Errors happen when the request fails completely—no internet connection, server down, or timeout. No data comes back at all.
Partial Data scenarios arise when some fields succeed but others fail. GraphQL's strength (and complexity) is that it can return both data and errors simultaneously.
Handling Errors in useQuery
The useQuery hook returns an error object you can check:
const { loading, error, data } = useQuery(GET_USER);
if (loading) return <Spinner />;
if (error) return <ErrorMessage error={error} />;
The error object contains:
networkErrorfor connection problemsgraphQLErrorsarray for server-reported issuesmessagecombining both into a readable string
Partial Data Strategy
Always check both error AND data:
if (error) {
// Log for debugging
console.error(error.graphQLErrors);
}
if (data?.user) {
// Render what succeeded
return <UserProfile user={data.user} />;
}
Think of it like ordering a pizza: sometimes the delivery arrives but your extra toppings are missing. You still got food (data), but something went wrong (error).
Key Takeaway: Always handle both error and data in GraphQL clients—errors don't necessarily mean complete failure, and successful responses might contain warnings you shouldn't ignore.