apollographql / react-apollo

:recycle: React integration for Apollo Client
https://www.apollographql.com/docs/react/
MIT License
6.85k stars 790 forks source link

Query.onCompleted isn't fired after fetching data from the cache #3968

Open cyberwombat opened 4 years ago

cyberwombat commented 4 years ago

Reopening issue filed in https://github.com/apollographql/react-apollo/issues/2177 since that was closed without a fix. Issue is still present in 3.1.3.

Summary of issue: onCompleted is not fired if the same data is attempted to be fetched again right after initial fetch. For example: (A and B would be useLazyQuery calls)

Fetch A - onCompleted is fired Fetch B - onCompleted is fired Fetch A - onCompleted is fired Fetch A again - onCompleted is NOT fired

Perhaps related to this explicit blocking: https://github.com/apollographql/apollo-client/blob/master/src/react/data/QueryData.ts#L272 ?

Only solution right now is to bypass/disable cache.

hatchli commented 4 years ago

Is there any word on this?

rmlevangelio commented 4 years ago

+1. Any workaround available?

cubogdan commented 4 years ago

Adding fetchPolicy: "cache-and-network" to the Query makes it work, but it never fetches from cache in my case.

This is not a fix, it's an accidental hack.

Simple example:

const [fetchUsers, { loading }] = useLazyQuery(GET_USERS_PAGINATED, {
    fetchPolicy: "cache-and-network",
    variables: {
      pageSize: PAGE_SIZE,
    },
    onCompleted: (data) => {
      // ...
    },
}