Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Include cached data with error #548

Merged

Conversation

StevePotter
Copy link
Contributor

This PR includes data cached from the Apollo store when there is an error while fetching the query. Now useful data can be displayed along with any error message.

For example, the server is down. An error will be returned as it always was. But until now, the data prop was empty. A good UI for this situation would be to display an error message like "There was an error reaching our servers. Until the problem is fixed, you can still use the app. Your data might just be a bit off." and show the cached data. Now you can!

@stubailo
Copy link
Contributor

Can you add a test for this as well?

Copy link
Contributor

@helfer helfer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @StevePotter. I think this is a behavior we should change in Apollo Client, but sort of that, this might be a good PR to patch things up. A test is definitely needed though.

package.json Outdated
@@ -1,6 +1,6 @@
{
"name": "react-apollo",
"version": "1.0.0-rc.3",
"version": "1.0.0-rc.4",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll update the package.json ourselves, this should not be in here.

Changelog.md Outdated
@@ -4,6 +4,9 @@ Expect active development and potentially significant breaking changes in the `0

### vNext

### 1.0.0-rc.4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just put this under vNext, and we'll figure out which version we'll release it under.

src/graphql.tsx Outdated
if (error) {
let previousResult = this.queryObservable.getLastResult();
if (previousResult && !previousResult.error) {
dataToMerge = assign({}, previousResult.data, currentResult.data);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's pretty hard to predict what data you'd get here if there ever is any data in currentResult. I think it might be better to just return the previous result.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @helfer thanks for the review. I'm happy you're encouraged by this PR. Please give me a few days to update the PR as I'm swamped on a few higher priority issues. I'll get the appropriate testes in there and clean up the currentResult stuff and just use getLastResult().

Thanks again!

@StevePotter
Copy link
Contributor Author

@helfer I tightened up the code and added test coverage. This one is good to go.

The test:

  1. Run a successful query
  2. Refetch, receiving an error
  3. Verify the data from 1 is included alongside the error.

The test fails without my change to src/graphql.tsx and passes with the change.

@StevePotter
Copy link
Contributor Author

@helfer how about now? It's clean, tested, and green.

@StevePotter
Copy link
Contributor Author

Another PR I submitted to apollo-client that doesn't affect this one but it needed for a solid offline UX apollographql/apollo-client#1601

@helfer
Copy link
Contributor

helfer commented Apr 21, 2017

Thanks @StevePotter, the test looks good!

This could technically break apps if a component only checks for data but not error, but I'm inclined to include it in the next minor release because we've been printing a warning to the console for quite some time now for unhandled errors, and I think this is a feature that a lot of people want.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants