apollographql / apollo-client

:rocket:  A fully-featured, production ready caching GraphQL client for every UI framework and GraphQL server.
https://apollographql.com/client
MIT License
19.35k stars 2.66k forks source link

Refetch doesn't set `loading: true` #7080

Closed twittwer closed 4 years ago

twittwer commented 4 years ago

Intended outcome:

I was expecting that ObservableQuery.refetch always triggers a network request to ensure a real refetch. This seems to be achieved here: https://github.com/apollographql/apollo-client/blob/481b30d5f90132d60ac479ce23ab4fd61acb3db5/src/core/ObservableQuery.ts#L231-L240

In this process I would like to inform the user about the loading state. Therefore, I assumed loading & networkStatus would be updated.

Actual outcome:

In case of { fetchPolicy: 'cache-and-network', nextFetchPolicy: 'cache-first' } (related to issue 6760) a network request is made, but the loading flag never became true.

Is this in intended? Can it be changed/workaround in any way?

Versions

  System:
    OS: macOS 10.15.6
  Binaries:
    Node: 12.18.1 - ~/.nvm/versions/node/v12.18.1/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 6.14.5 - ~/.nvm/versions/node/v12.18.1/bin/npm
  Browsers:
    Chrome: 85.0.4183.121
    Safari: 14.0
  npmPackages:
    @apollo/client: 3.2.1 => 3.2.1 
    apollo-cache-persist: 0.1.1 => 0.1.1 
    apollo-server-express: 2.18.1 => 2.18.1 
benjamn commented 4 years ago

@twittwer Can you try setting notifyOnNetworkStatusChange: true in the options (along with fetchPolicy and nextFetchPolicy)?

Although loading states are desirable in many cases, refetching a query without rendering an intermediate loading state is also a pretty common desire, so notifyOnNetworkStatusChange allows you to select your desired loading state behavior.

twittwer commented 4 years ago

I already tried the flag but probably I had some other faulty configuration back then Now it seems to work as expected - Thanks 👍