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.

2.0 not clearing error after refetch

See original GitHub issue

Intended outcome: data.error should be cleared after successful refetch.

Actual outcome: after error a successful refetch does not clear the error. (In fact I dont think query data is set to undefined on error either, it also retains previous response if there was one. Not sure if that caching and is possibly expected on network-only calls?)

How to reproduce the issue:

1. turn graphql server offline
2. as expected queries return an error { myData: undefined, loading: false, error: 'Error: Network error: Failed to fetch' }
3. turn graphql server back online and do a refetch
4. query returns data successfully but error is not cleared { myData: { uuid: abc123 }, loading: false, error: 'Error: Network error: Failed to fetch'  }

Version

  • apollo-client@2.0.0

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:32
  • Comments:32 (4 by maintainers)

github_iconTop GitHub Comments

26reactions
Wenzilcommented, Nov 21, 2017

This issue prevents letting users retry failed queries manually. I would assume this is a really common use case. Any ETA on a fix?

10reactions
wdimicelicommented, Jan 7, 2018

I am running into this issue as well… strangely enough it seems like the unit test for this scenario was removed some months ago.

The issue is fundamentally caused by ES6 Observables terminating the stream when an error is sent to an observer. In this case, any kind of error in the query causes the react-apollo component to get dropped (from here to here to here), and the subsequent teardown of the query is just a further consequence. The components themselves don’t contain much subscription logic beyond the mount/unmount lifecycle and thus remain unaware the queries were torn down.

It seems like the subscription logic for react components might need further consideration, or perhaps a different way to handle error conditions.

In either case, here is my own short-term workaround which should serve as a drop-in replacement for the normal graphql function:

const MyGraphQL = (...args) => Component =>
    class extends graphql(...args)(Component) {
        dataForChild() {
            const data = super.dataForChild();
            if (data.refetch && data.error) {
                data.refetch = (...args) => {
                    this.unsubscribeFromQuery();
                    const { lastError, lastResult } = this.queryObservable;
                    // If lastError is set, the observable will immediately
                    // send it, causing the stream to terminate on initialization.
                    // We clear everything here and restore it afterward to
                    // make sure the new subscription sticks.
                    this.queryObservable.resetLastResults();
                    this.subscribeToQuery();
                    Object.assign(this.queryObservable, { lastError, lastResult, isTornDown: false });
                    return this.queryObservable.refetch(...args);
                };
            }
            return data;
        }
    };
Read more comments on GitHub >

github_iconTop Results From Across the Web

Refetching queries in Apollo Client - Apollo GraphQL Docs
The client.refetchQueries method collects the TResult results returned by onQueryUpdated , defaulting to TResult = Promise<ApolloQueryResult<any>> if ...
Read more >
Refresh or reset Surface RT or Surface 2 - Microsoft Support
If you're having problems with your Surface RT or Surface 2, try refreshing it or resetting it to its factory settings.
Read more >
React-Query: How to useQuery when button is clicked
it starts fetching data from backend with this line of code const {status, data, error, refetch } = useQuery( myKey, fetchData(), ...
Read more >
useQuery | TanStack Query Docs
This function receives a retryAttempt integer and the actual Error and returns the delay to apply ... If set to false , the...
Read more >
Automated Re-fetching | Redux Toolkit
RTK Query > Usage > Automated Refetching: cache invalidation management. ... be undefined based on whether the query was successful or not.
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