Apollo-client: After refetch fails, successful fetchMore sends updateQuery undefined previousResults

Created on 14 Aug 2019  路  11Comments  路  Source: apollographql/apollo-client

Intended outcome:
After server downtime, during which a call to refetch fails, I would expect future successful queries using fetchMore to call the updateQuery function with previousResults from before the downtime.

Actual outcome:
After failed calls to refetch, successful calls to fetchMore call the updateQuery function with a first argument (previousResults) value of undefined. At that point, there is no way to use the existing results.

A log looks something like this:

Server online, screen loading
Component rendering with data: {loading: true, networkStatus: 1, error: undefined}
Component rendering with data: {names: ["a", "b", "c", "d", "e"], loading: false, networkStatus: 7, error: undefined}
Server going offline
Attempting to sort (using `refetch` with variables as a promise)
Component rendering with data: {names: ["a", "b", "c", "d", "e"], loading: true, networkStatus: 2, error: undefined}
Component rendering with data: {names: ["a", "b", "c", "d", "e"], loading: false, networkStatus: 7, error: "Error: GraphQL error: ..."}
Sort failed
Server going online
Attempting to paginate
fetchMore's updateQuery method called with previousResults: undefined

How to reproduce the issue:
Set up a repo that uses both refetch for sorting and fetchMore for pagination.

  1. Load the initial results successfully
  2. Go offline
  3. Try sorting the results (which should fail)
  4. Go online
  5. Try paginating
    The updateQuery method is called with a first argument, previousResults, of undefined.

I've created a branch to demonstrate the behavior:
https://github.com/treyp/react-apollo-error-template/tree/previousresults-undefined

This can also be found on CodeSandbox:
https://codesandbox.io/s/github/treyp/react-apollo-error-template/tree/previousresults-undefined

Follow the steps above, just like the screen recording here, to reproduce.

apollo-updatequery

Versions

  System:
    OS: macOS 10.14.6
  Binaries:
    Node: 10.15.3 - ~/.nvm/versions/node/v10.15.3/bin/node
    npm: 6.10.3 - ~/.nvm/versions/node/v10.15.3/bin/npm
  Browsers:
    Chrome: 76.0.3809.100
    Safari: 12.1.2
  npmPackages:
    apollo-cache-inmemory: ^1.6.3 => 1.6.3 
    apollo-client: ^2.6.4 => 2.6.4 
    apollo-link-batch-http: ^1.2.12 => 1.2.12 
    react-apollo: ^3.0.0 => 3.0.0 

Most helpful comment

i have the same issue, that fetchMore is for some queries very buggy.

when I fetch more directly it's undefined.
after ignoring this and run refetch the and then fetchMore/updateQuery it works fine. 馃

All 11 comments

I ran into the same issue - would appreciate to know how to properly handle occasional network-outages (= error states). Since reloading (using refetch) and after a successful reload to paginate (using fetchMore) does not work.

Having a similar issue here. Apollo throws the exact same error when running a local @client query in between a same server-side query using fetchMore. The callback parameter previousResults of updateQuery is then undefined. Running [email protected]

Got a very similar problem. I noticed that my previous result is undefined when the result of the initial query comes from the cache. When I use a fetch-policy of network-only, it works without a problem.

Same problem here. I'm using fetchMore and updateQuery. Ref return undefined

I've noticed that errors from fetchMore aren't returned as an error in the hook, e.g.

const { networkStatus, error, data, fetchMore } = useQuery(QUERY)

console.log(error); // error only exists if the initial query fails, not if fetchMore fails?

<button onClick={() => fetchMore({ ... })}>

Not sure if this is expected behaviour?

same error +1, anyone has fix for this?

similar issue, but not exactly the same. in my case, for some of my queries, the fetchMore works fine, but there are one or two, the prevResult is undefined.

Can anyone provide what can be the cause to make prevResult undefined? eg: update the query result object directly (mutate queryResult)?

i have the same issue, that fetchMore is for some queries very buggy.

when I fetch more directly it's undefined.
after ignoring this and run refetch the and then fetchMore/updateQuery it works fine. 馃

same error after call fetchMore 2 times with different queries;

Same problem after it query return over 20 products, either during useQuery or refetch

still happens with @apollo/client:3.0.2 馃槩

Was this page helpful?
0 / 5 - 0 ratings