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 stops updating after first network error #6759

Closed
gajus opened this issue Aug 1, 2020 · 4 comments
Closed

useQuery stops updating after first network error #6759

gajus opened this issue Aug 1, 2020 · 4 comments

Comments

@gajus
Copy link

gajus commented Aug 1, 2020

Intended outcome:

I want to get updates about every time that a query fails.

Actual outcome:

Only the first error propagates to query results.

How to reproduce the issue:

const getSnackMeetingInvitationQuery = useQuery(GET_SNACK_MEETING_INVITATION, {
  errorPolicy: 'all',
  fetchPolicy: 'network-only',
  pollInterval: 1000,
  variables: {
    uid,
  },
});

console.log('getSnackMeetingInvitationQuery', {
  data: Boolean(getSnackMeetingInvitationQuery.data),
  error: Boolean(getSnackMeetingInvitationQuery.error),
  loading: Boolean(getSnackMeetingInvitationQuery.loading),
});

Screen Shot 2020-08-01 at 4 59 48 PM

If you look at the screenshot, it is clear that only the first time that an error occurs that it propagates to the results.

The expected result is that we should see:

getSnackMeetingInvitationQuery {data: true, error: true, loading: false}

after the last error in the console log.

Versions

  System:
    OS: macOS 10.15.5
  Binaries:
    Node: 14.5.0 - ~/.nvm/versions/node/v14.5.0/bin/node
    npm: 6.14.5 - ~/.nvm/versions/node/v14.5.0/bin/npm
  Browsers:
    Chrome: 84.0.4147.105
    Safari: 13.1.1
  npmPackages:
    @apollo/client: ^3.1.1 => 3.1.1
    @apollo/react-hooks: ^4.0.0 => 4.0.0

@gajus
Copy link
Author

gajus commented Aug 1, 2020

Just in case, setting errorPolicy: 'none' has no effect.

@gajus
Copy link
Author

gajus commented Sep 16, 2020

I worked around this by implementing a custom hook:

const useLatest = (uid: string) => {
  const [data, setData] = useState(null);
  const [loading, setLoading] = useState(true);
  const [stopPolling, setStopPolling] = useState(() => {
    return () => {};
  });
  const [error, setError] = useState(null);

  const apolloClient = useApolloClient();

  useEffect(() => {
    const watchQuery = apolloClient.watchQuery({
      errorPolicy: 'all',
      fetchPolicy: 'no-cache',
      notifyOnNetworkStatusChange: true,
      pollInterval: 1000,
      query: LATEST_QUERY,
      variables: {
        uid,
      },
    });

    const subscriber = {
      error: (apolloError) => {
        setError(apolloError);

        setTimeout(() => {
          watchQuery.resetLastResults();
          watchQuery.subscribe(subscriber);
        }, 1000);
      },
      next: (state) => {
        if (!deepEqual(state.data, data)) {
          setData(state.data);
        }

        setLoading(state.loading);
        setError(null);
      },
    };

    watchQuery.subscribe(subscriber);

    setStopPolling(() => {
      return () => {
        watchQuery.stopPolling();
      };
    });
  }, [apolloClient, data, uid]);

  return {
    data,
    error,
    loading,
    stopPolling,
  };
};

@gajus
Copy link
Author

gajus commented Sep 16, 2020

If you were to just use notifyOnNetworkStatusChange: true,, it would fix this problem, but it would cause re-render every time network status changes.

Somewhat related issues:

@hwillson
Copy link
Member

If someone can provide a small runnable reproduction that demonstrates this happening with @apollo/client@latest, we'll take a closer look. Thanks!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants