apollo-client icon indicating copy to clipboard operation
apollo-client copied to clipboard

fetchMore doesn't trigger callback on graphql/network error

Open Necroskillz opened this issue 5 years ago • 8 comments

Intended outcome: When an error occurs while using fetchMore to get the next page (either response with errors or network error), the query should be notified of it when using errorPolicy: 'all'.

Actual outcome: GQL Request failed and the watch callback was not called.

How to reproduce the issue:

My case: Use relay pagination, have a query which uses { fetchPolicy: 'cache-and-network', nextFetchPolicy: 'cache-first', errorPolicy: 'all' }, have a callback which handles errors property. Load initial page, then either have invalid after cursor value in fetchMore to have grapql endpoint fail when parsing it, or go offline. Then try to load the next page.

Versions

  System:
    OS: Windows 10 10.0.18362
  Binaries:
    Node: 12.18.3 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.4 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
    npm: 6.14.6 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 84.0.4147.125
    Edge: Spartan (44.18362.449.0)
  npmPackages:
    @apollo/client: ^3.1.3 => 3.1.3
    apollo-angular: ^2.0.3 => 2.0.3

Necroskillz avatar Aug 18 '20 15:08 Necroskillz

I can confirm that this is reproducible on @apollo/client: 3.2.0 even with the default errorPolicy, fetchPolicy, nextFetchPolicy values

raduflp avatar Sep 15 '20 18:09 raduflp

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

hwillson avatar May 29 '21 11:05 hwillson

@hwillson Here is my reproduction (use branch fetchmore-error-callback): https://github.com/dylanwulf/react-apollo-error-template/tree/fetchmore-error-callback

If the error policy is none, then the fetchMore function will throw but onError will NOT be called. If the error policy is all, then fetchMore will NOT throw and the onError callback will STILL NOT be called.

I think part of the problem here is that the documentation is very unclear. When fetchMore results in an error, is it supposed to call onError? Is it supposed to throw? When the error policy is all, is onError supposed to be called or not? I have no idea.

dylanwulf avatar Jun 08 '21 20:06 dylanwulf

I can confirm that this is reproducible on @apollo/client: 3.7.3 with errorPolicy: default | 'none'

mohamedjkr avatar Jan 05 '23 14:01 mohamedjkr

Still happening

mathbalduino avatar May 06 '23 19:05 mathbalduino

Confirm the problem

malinindev avatar May 15 '23 11:05 malinindev

In my case, I am merging the previous and the new results. I had to throw the error first from withinupdateQuery

fetchMore({
        variables: {
          ...getTemplateVersionsQueryVars,
        },
        updateQuery: (previousQueryResult, { fetchMoreResult }) => {
          if (!fetchMoreResult) return previousQueryResult;

          if (fetchMoreResult instanceof ApolloError) {
            throw fetchMoreResult;
          }
          return {
            getTemplateVersions: {
              ...fetchMoreResult.getTemplateVersions,
              edges: [
                ...(previousQueryResult.getTemplateVersions.edges ?? []),
                ...(fetchMoreResult.getTemplateVersions.edges ?? []),
              ],
            },
          };
        },
      }).catch((error) => console.log({ fetchMoreError: error }));

https://stackoverflow.com/questions/54437901/how-to-error-handle-in-fetchmore-pagination/78734951#78734951

ossamaweb avatar Jul 11 '24 10:07 ossamaweb

Linking in this issue for anyone that ends up here, https://github.com/apollographql/apollo-client/issues/12352

keithfrazier25 avatar Feb 07 '25 21:02 keithfrazier25