question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useQuery does not return data on refetch after error.

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:11
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
gregbtycommented, 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.

5reactions
maxguzenskicommented, 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])
Read more comments on GitHub >

github_iconTop Results From Across the Web

Refetching queries in Apollo Client - Apollo GraphQL Docs
If onQueryUpdated is not provided, the default implementation returns the result of calling observableQuery.refetch() . When onQueryUpdated is provided, it can ...
Read more >
react-query: Refetch if and only if there is no error
I am assuming you are trying to fetch the user data but if it fails you do not want to retry, is that...
Read more >
useQuery | TanStack Query Docs
Optional; Defaults to true; If set to true , the query will refetch on mount if the data is stale. If set to...
Read more >
React Query FAQs - TkDodo's blog
1const { data, refetch } = useQuery(['item'], () => fetchItem({ id: 1 })). 2. 3<button onClick={() => {. 4 // this is not...
Read more >
Queries - Redux Toolkit
RTK Query > Usage > Queries: fetching data from a server. ... See Customizing Queries if fetchBaseQuery does not handle your requirements.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found