apollo icon indicating copy to clipboard operation
apollo copied to clipboard

Loading is false using 'no-cache'

Open eddivalen opened this issue 6 years ago • 15 comments

Why when I reload the page loading is false? When I use fetchMore, loading changes to true, but on the first load, loading is false I'm using 'no-cache' option

defaultOptions: {
    $query: {
      fetchPolicy: 'no-cache',
    },
  },

eddivalen avatar Feb 05 '19 17:02 eddivalen

I have the same issue...

internalfx avatar Feb 14 '19 20:02 internalfx

It seems to be an issue with Apollo Client.

Akryum avatar Sep 30 '19 11:09 Akryum

@Akryum Is this not an issue caused by the referenced commit (82d6c02f7b0202d7682007449dfce56de0f5f243)?

govert-w3s avatar Oct 15 '19 13:10 govert-w3s

Hey @Akryum, what is the news with this issue? what do you suggest for those who use the no-cache option

eddivalen avatar Nov 05 '19 15:11 eddivalen

Related to https://github.com/vuejs/vue-apollo/issues/263?

tobiasfuhlroth avatar Nov 13 '19 10:11 tobiasfuhlroth

I reproduced the issue with the vue-apollo e2e test. If you add the :fetch-policy="'no-cache'" to the ApolloQuery here

After this change, running the npm run test:e2e:dev:client will show this error: Screenshot 2020-04-24 at 10 12 25

So this check mentionned here by michael-harrison on "no-cache" was added here: https://github.com/vuejs/vue-apollo/pull/280 and discussed here https://github.com/vuejs/vue-apollo/commit/82d6c02f7b0202d7682007449dfce56de0f5f243 . I'm still not sure it this is the cause of the issue, but disabling the loading state to prevent an issue with optimistic response of no-cache queries seems strange.

Edit: commenting the mentionned condition is enough to fix the isLoading in the ApolloQuery component but the result.loading is still always false.

idflood avatar Apr 24 '20 08:04 idflood

Starting to wonder if this is an issue with apollo client.

The shouldNotifyIfLoading condition here don't seem to set the loading property if the fetch policy is no-cache or network-only.

Setting notifyOnNetworkStatusChange does indeed seems to "fix" the issue.

Reading a bit more it seems that the loading state is a recurring issue with apollo-client (https://github.com/apollographql/react-apollo/issues/727#issuecomment-306082518 and https://github.com/apollographql/react-apollo/issues/2238 for example), which all suggest setting the notifyOnNetworkStatusChange to fix this.

idflood avatar May 01 '20 06:05 idflood

Any update?

MJoshua25 avatar Dec 18 '20 12:12 MJoshua25

any luck?

vlodko avatar Feb 02 '21 12:02 vlodko

I am using nuxtjs/apollo with ssr and I have this issue as well Has there been any updates? Is there anything I can do to help? How can I prevent the user from interacting while they are waiting for a response from the server?

fafamnzm avatar Feb 26 '21 14:02 fafamnzm

If you don't mind caching the data for no good reasons and wasting memory, network-only can be used instead of no-cache as it has the expected behaviour. Another workaround is to set notifyOnNetworkStatusChange to true on the smart query which may waste cpu instead of memory but the loading status is also set correctly. The if looks like this:

if (this.options.fetchPolicy !== 'no-cache' || this.options.notifyOnNetworkStatusChange) {
  var currentResult = this.maySetLoading();

You can specify the option by default:

const apolloProvider = new VueApollo({
  ...
  defaultOptions: {
	...
    $query: {
      ...
      notifyOnNetworkStatusChange: true,
      ...
    },
	...
  },
  ...
});

I also think the no-cache fetch policy should set the loading status by default. If some components don't expect a loading status when using a no-cache fetch policy, they could be fixed or an option could be added to still support their use case.

fungiboletus avatar Mar 03 '21 12:03 fungiboletus

@fungiboletus Thank you for your response. I have used this, however, this will not work as I hoped. this will only trigger apollo loading in the given component. What I need is a this.$apollo.loading that will be true all throughout the application when any query or mutation is running. For example if the app is fetching something from the server, I want the page to be blurry or if a mutation is triggered, I want the whole app to be frozen and blurry until confirmation or rejection is returned from the server. This approach only set the loading key to true in the component where the query is being run and it is false for the rest of the application .

fafamnzm avatar Mar 04 '21 07:03 fafamnzm

The workaround I found was to add a dummy data to the query prop, so when the fetch is finished (loading finished) the prop will be either empty or filled

For example:

export default {
  apollo: {
    yourData: {
      query: GET_YOUR_DATA,
      fetchPolicy: 'no-cache'
    },
  data: () => ({
    yourData: ['dummy'] // Any dummy value you want
  }),
  computed: {
    // Here you put your loading check logic
    isQueryLoading () {
      return this.yourData[0] === 'dummy'
    } 
  }
}

As soon as your query finish loading, it'll full or empty, but never yourData[0] === 'dummy'. You can check for errors and change the dummy data for another thing to avoid keep loading on error etc...

JoaoHCopetti avatar Jun 23 '23 19:06 JoaoHCopetti

Any update on this issue?

astyltsvig avatar Jul 07 '23 13:07 astyltsvig