Apollo-client: [3.0.0-beta.27] Querying data that is already in the cache sets loading to true

Created on 28 Jan 2020  路  11Comments  路  Source: apollographql/apollo-client

This is a repeat of issue #5757 created by @od1k about this a few weeks back and marked as fixed in beta.23: unfortunately, it's still reproing in beta.27.

Intended outcome:
When running some query:

  const {
    loading,
    data
  } = useQuery(SOME_QUERY);

we expect that loading will be false if data is already in the cache (with default or cache-only fetchPolicy).

Actual outcome:
loading is true even when the requested data is in the cache.

I cannot tell if this is expected behaviour or not but it is definitely different from Apollo Client 2.6.

I've updated @od1k's original repro to beta.27 and published it as a code sandbox here for easier debugging.

The repro code, in essence, first fetches all data in ALL_DATA_QUERY and then fetches a subset in CLOCK_SPEED_QUERY. The expected behavior is that the second query (triggered when the button is clicked) only renders once with loading: false, but it actually renders twice (see console log).

Most helpful comment

When using {ssr:true} this issue is not fixed. The client is set to loading while the data is there.

All 11 comments

added details from original issue if it helps get more traction on the issue. For now, the mitigation is to stop relying on loading and just check for !data instead.

I think it's the same issue described in https://github.com/apollographql/apollo-client/issues/5835, where queries resolved with data have loading state during SSR (and subsequent first render on the client when the page hydrates).

Still an issue on 3.0.0-beta.34:

For a component containing a single useQuery hook with fetchPolicy: cache-only I'm seeing the following occur:

The component renders for the first time, the loading prop is true, the data is populated with the query result.
The component renders again, the loading prop is false, the same data is populated with the query result.

Seems like this component should only render once, with loading = false and the data being returned?

Same here. I also tested this on beta-35, and there the issue is still occurring.

Tried on 3.0.0-beta.44, still occurring

can also reproduce with

   "@apollo/client": "^3.0.0-beta.44",
    "@apollo/react-ssr": "^4.0.0-beta.1",

Any progress on this?

can also reproduce with

    "@apollo/client": "^3.0.0-rc.4",
    "@apollo/react-ssr": "^4.0.0-beta.1",

seems still be the case with the final version of @apollo/client !

I've verified this is no longer an issue with @apollo/[email protected]. Thanks!

When using {ssr:true} this issue is not fixed. The client is set to loading while the data is there.

Was this page helpful?
0 / 5 - 0 ratings