ObservableQuery.refetch() returns rejected Promise instead of throwing when fetchPolicy === 'cache-only' #1592
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem / Situation
At the moment react-apollo (v1.0.1) sets unmounted components to cache-only (apollographql/react-apollo#622) and apollo-client will throw an error when calling
refetch()
on a query that is set to cache-only.In our app, this was having the effect of force closing during a pull to refresh / refresh on resume because we didn't guard against refetch() possibly throwing an error (but we did handle a failed refetch promise via .catch()).
This is a somewhat controversial code-style question, so I'll write out my reasoning -- If you/y'all disagree with my argument, feel free to close --
It seems wrong to me to throw an error instead of returning a rejected Promise when an internal state is misconfigured. For other cases like
fetchMore(fetchMoreOptions)
, it makes sense to throw an error because it's a developer-fault configuration error ("you're using this wrong"). The method will fail as soon as it's executed it because the usage of it is wrong and the developer can then fix the arguments. For refetch, however, it throws an error when the ObservableQuery has a misconfigured internal variable, which can (as shown in the react-apollo issue) change after the fact.Furthermore, by returning a promise, the developer should already know to handle the failure case in the .catch(). Without this PR, the developer needs to wrap every
refetch()
in a try/catch in addition to their .catch() on the off chance that a refetch might crash their app :)