Unverified Commit 93c59f02 authored by Zafor Abdullah's avatar Zafor Abdullah Committed by GitHub

fix: error handling and loading states in with-apollo-and-redux example (#91457)

## For Contributors

### Fixing a bug

- Fixes #(open or reference an issue if one exists, otherwise omit)
- No tests required — this is an `examples/` change only
- No custom error links needed — failures are surfaced via Apollo's
built-in error state

---

## Summary

- `PostList` destructured `data` before checking `loading`/`error`, so
the component threw when `data` was `undefined` (e.g., on the initial
fetch or after a query error) instead of showing the loading/error UI.
- Move the `error`/`loading`/`!data` guard clauses above the `data`
destructuring and `loadMorePosts` definition, and add a `!data` check so
the component never accesses properties on an undefined value.
- Bump `graphql` (`14.5.8` → `16.13.1`) and `lodash` (`4.17.20` →
`4.17.23`) in the example's `package.json` to pick up fixes in
dependencies the example relies on.

## Note: 
Initially this PR suppose to target upstream DB issue
parent d6f2ca80
...@@ -35,6 +35,10 @@ export default function PostList() { ...@@ -35,6 +35,10 @@ export default function PostList() {
const loadingMorePosts = networkStatus === NetworkStatus.fetchMore; const loadingMorePosts = networkStatus === NetworkStatus.fetchMore;
if (error) return <ErrorMessage message="Error loading posts." />;
if (loading && !loadingMorePosts) return <div>Loading</div>;
if (!data) return null;
const { allPosts, _allPostsMeta } = data; const { allPosts, _allPostsMeta } = data;
const loadMorePosts = () => { const loadMorePosts = () => {
...@@ -45,9 +49,6 @@ export default function PostList() { ...@@ -45,9 +49,6 @@ export default function PostList() {
}); });
}; };
if (error) return <ErrorMessage message="Error loading posts." />;
if (loading && !loadingMorePosts) return <div>Loading</div>;
const areMorePosts = allPosts.length < _allPostsMeta.count; const areMorePosts = allPosts.length < _allPostsMeta.count;
return ( return (
......
...@@ -8,8 +8,8 @@ ...@@ -8,8 +8,8 @@
"dependencies": { "dependencies": {
"@apollo/client": "^3.0.0", "@apollo/client": "^3.0.0",
"deepmerge": "^4.2.2", "deepmerge": "^4.2.2",
"graphql": "14.5.8", "graphql": "16.13.1",
"lodash": "4.17.20", "lodash": "4.17.23",
"next": "latest", "next": "latest",
"prop-types": "^15.6.0", "prop-types": "^15.6.0",
"react": "^18.2.0", "react": "^18.2.0",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment