Skip to content
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

Open
rostero1 opened this issue Feb 7, 2019 · 8 comments · May be fixed by #141
Open

useQuery does not return data on refetch after error. #74

rostero1 opened this issue Feb 7, 2019 · 8 comments · May be fixed by #141

Comments

@rostero1
Copy link

rostero1 commented Feb 7, 2019

Steps to reproduce:

  1. Make a successful request
  2. Take graphql server offline
  3. Make a new request
  4. We get an error now.
  5. Take graphql server online
  6. Invoke refetch.
  7. Note the successful network request in devtools, but we never get data back from useQuery.
@maxguzenski
Copy link

maxguzenski commented Feb 8, 2019

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

@maxguzenski
Copy link

My mistake.
Only re-subscribe dont fix this issue
apollographql/react-apollo#2070

My work around now is rebuild client.watchQuery after a refetch with error.

@maxguzenski
Copy link

maxguzenski commented Feb 16, 2019

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])

@trojanowski
Copy link
Owner

@maxguzenski Thank you for the info. Would you like to prepare a PR?

@pontusab
Copy link

pontusab commented Mar 1, 2019

@maxguzenski Would really want a PR for this too 💯

@maxguzenski
Copy link

I'm on vacation until March, 25 ... I can not do a PR until that date.

@pontusab
Copy link

pontusab commented Mar 1, 2019

@maxguzenski Have a great vacation, I will make a PR with your solution. Thanks 🥇

@gregbty
Copy link

gregbty commented Apr 22, 2019

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.

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

Successfully merging a pull request may close this issue.

5 participants