question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No difference beetween `cache-and-network` and `network-only` strategies!

See original GitHub issue

I think the cache-and-network strategy doesn’t work properly and acts similar to network-only.

What To Expect

  1. apollo sends two queries to cache and network in parallel.
  2. if doesn’t exist in cache waiting for network and store in cache for next query.
  3. if exists, render data and wait for network in background for syncing data.

but in step 3 :

apollo doesn’t render cache data Immediately until network data is received. This is not fast and behaves like network-only strategy.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:14
  • Comments:6

github_iconTop GitHub Comments

4reactions
lassesteffencommented, Feb 23, 2021

I just saw that if you use notifyOnNetworkStatusChange: true it works for me and does return the cached data on the first return

2reactions
fforrescommented, Feb 11, 2021

This is the solution we are using to expose a refetching prop so we can control rendering behaviour better @wminshew @foad-salawati

import type { OperationVariables, QueryResult } from "@apollo/react-common";
import type { QueryHookOptions } from "@apollo/react-hooks";
import { useQuery as useApolloQuery } from "@apollo/react-hooks";
import type { DocumentNode } from "graphql";

export function useQuery<TData = any, TVariables = OperationVariables>(
  query: DocumentNode,
  options?: QueryHookOptions<TData, TVariables>,
): QueryResult<TData, TVariables> & {
  refetching: boolean;
} {
  const response = useApolloQuery<TData, TVariables>(query, options);
  return {
    ...response,
    refetching: response.networkStatus === 4,
  };
}

Then you can do

const { refetching } = useQuery(aQueryDocument);

I know it’s not a real solution to the issue, but maybe it helps as a patch

Read more comments on GitHub >

github_iconTop Results From Across the Web

Understanding Apollo Fetch Policies | by Galen Corey - Medium
The 'no-cache' policy is similar to 'network-only', but it skips the step of updating the cache. This might be appropriate if you don't...
Read more >
Queries - Apollo GraphQL Docs
If all data is available locally, useQuery returns that data and doesn't query your GraphQL server. This cache-first policy is Apollo Client's default...
Read more >
React Apollo: Understanding Fetch Policy with useQuery
There are six fetch policies that are available on useQuery . ... no-cache, Similar to network-only, except the query's result is not stored...
Read more >
reactjs - What is the difference between 'store-and-network' vs ...
With this distinction, the "network-only" fetch-policy will literally ignore the cache. It will not check whether the data the component ...
Read more >
Basics of Caching Data in GraphQL Apollo React Client
no-cache policy does not read, nor does it write to the cache with the response. It will always make a request using your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found