Open rostero1 opened 5 years ago
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. Only re-subscribe dont fix this issue https://github.com/apollographql/react-apollo/issues/2070
My work around now is rebuild client.watchQuery after a refetch with error.
I was able to solve the problem based on https://github.com/apollographql/react-apollo/blob/master/src/Query.tsx#L363
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
.