Rethrow apollo errors and recommend error boundaries
See original GitHub issueRight now errors are returned as a part of the useQuery result. Maybe a better idea would be to throw them and recommend to use Error Boundaries instead (at least in the suspense mode).
So instead of this code:
const Hello = () => {
const { data, error } = useQuery(GET_HELLO);
if (error) return `Error! ${error.message}`;
return <p>{data.hello.world}</p>;
};
we could write:
const Hello = () => {
// we can be sure here that the error hasn't occurred
return <p>{data.hello.world}</p>;
};
and use an error boundary at the root of the react tree (or anywhere else above the component) - similar to how we use the <Suspense /> component.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:17
- Comments:24 (21 by maintainers)
Top Results From Across the Web
Handling operation errors - Apollo GraphQL Docs
Apollo Client helps you handle these errors according to their type, enabling you to show appropriate information to the user when an error...
Read more >React-Query With Graphql-Request: Error Boundary Not ...
Custom error pages in React with GraphQL and Error Boundaries that sends your team ... Ask questionsRethrow apollo errors and recommend error boundaries....
Read more >An Annotated Guide to React Server Components
What problems do React Server Components solve? ... in the tree on the client, an error is rethrown so that the client's error...
Read more >Custom error pages in React with GraphQL and ... - Medium
To use the GraphqlErrorHandler , we wrapped apollo-client's Query and Mutation ... Error boundaries are React components that can catch JavaScript errors ......
Read more >Automatically handling Apollo Client errors in your React UI
import ReactDOM from "react-dom" ;. import App from "./App" ; ·... import { onError } from "@apollo/client/link/error" ; · import { useState } ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I’d make it optional (but enabled by default) so if you wanted the old behavior you could use it like e. g.
const { data, error } = useQuery(MY_QUERY, { throw: false });.But the general idea is to make error management similar to what suspense does for data loading. So in most cases, it should be enough to put a single error boundary at the top of the React tree, but if you need it, you can also put it at the bottom, just above a component which requires a special behavior. Please compare:
and:
Of course, you can mix suspense and error boundaries. Please also note that you have access to the error instance inside of the error boundaries.
So I’d like support for error boundaries enabled by default, but not yet. As I said in https://github.com/trojanowski/react-apollo-hooks/issues/28#issuecomment-467191957 I’d like to have two hooks - the high-level one recommended for most cases (which will use suspense and error boundaries) and the second one - when you prefer to manage everything by yourself.