-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
useQuery does not return data on refetch after error. #74
Comments
This issue happen on my own apollo hook implementation as well. The solution is re-subscribe to watched query after an error. I believe it is an apollo-client core issue. useEffect(() => {
const subscribe = watchedQuery.subscribe({...})
return () => subscribe.unsubscribe()
}, [...othersVariablesToCheck, error]) // <- this |
My mistake. My work around now is rebuild client.watchQuery after a refetch with error. |
I was able to solve the problem based on changing useEffect inside useQuery by: useEffect(() => {
if (!watchedQuery) {
return
}
let subscription
// if fetchPolicy='cache-and-network' and data is on cache
// notifyOnNetworkStatusChange is aways TRUE, why??
// this "if" fix it.
function invalidateCurrentResult(props) {
if (props.loading) return
setResponseID(x => x + 1)
}
// from: https://github.com/apollographql/react-apollo/blob/master/src/Query.tsx#L363
// after a error on refetch, without this fix, refetch never works again
function invalidateErrorResult() {
unsubscribe()
const lastError = watchedQuery.getLastError()
const lastResult = watchedQuery.getLastResult()
watchedQuery.resetLastResults()
subscribe()
Object.assign(watchedQuery, { lastError, lastResult })
setResponseID(x => x + 1)
}
function subscribe() {
subscription = watchedQuery.subscribe(
invalidateCurrentResult,
invalidateErrorResult
)
}
function unsubscribe() {
if (subscription) subscription.unsubscribe()
subscription = undefined
}
subscribe()
return unsubscribe
}, [watchedQuery]) |
@maxguzenski Thank you for the info. Would you like to prepare a PR? |
@maxguzenski Would really want a PR for this too 💯 |
I'm on vacation until March, 25 ... I can not do a PR until that date. |
@maxguzenski Have a great vacation, I will make a PR with your solution. Thanks 🥇 |
I'm assuming a PR is still needed for this? Running into this issue now and it is still unresolved in react-apollo so thought it would probably be easier to apply the workaround above in this repo. I can open one if needed. |
Steps to reproduce:
refetch
.The text was updated successfully, but these errors were encountered: