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.4k stars 2.66k forks source link

fetchPolicy: 'standby' returns loading state as true before HTTP request start #12122

Open viico opened 2 weeks ago

viico commented 2 weeks ago

Issue Description

We want to use the standby fetch policy to call our API only when we use refetch.

The problem is that the loading field of ApolloQueryResult is true before any HTTP call. The issue was discussed here : #7564, but the solution is to use useLazyQuery which is not available for apollo-angular.

I don't understand why the field loading is initialized to true if there is no loading yet ?

Link to Reproduction

https://github.com/viico/apollo-fetch-policy-standby-loading

Reproduction Steps

We create a repo for reproduction here : https://github.com/viico/apollo-fetch-policy-standby-loading.

When you start the angular application you will see that there is no HTTP request before we refetch (that's ok) but the loading field is true anyway.

image

@apollo/client version

3.0.0

phryneas commented 1 week ago

As far as I understand it, standby was added at some point to enable the skip use case of useQuery, but no additional networkStatus was added for it - instead, the networkStatus with "no data and no error" was used for it - loading.

The React hooks work around that outside the core as you can see here: https://github.com/apollographql/apollo-client/blob/4115129ea88262740ee5fa5628d91f1e51c80fee/src/react/hooks/useQuery.ts#L817-L823

This is something that - while confusing - could break a lot of apps if we were to change it. I'll bring it up internally, but short-term I have little hope that we can change it.

Your best bet is probably to work around it in a similar fashion for now.