Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useLazyQuery doesn't work with cache-and-network fetch policy #7171

Open
Tracked by #8596
LauraBeatris opened this issue Oct 16, 2020 · 15 comments
Open
Tracked by #8596

useLazyQuery doesn't work with cache-and-network fetch policy #7171

LauraBeatris opened this issue Oct 16, 2020 · 15 comments

Comments

@LauraBeatris
Copy link

LauraBeatris commented Oct 16, 2020

Intended outcome:
After the lazy query be triggered, it should behave correctly according to the fetch policy chosen

Actual outcome:
The fetch policy cache-and-network isn't working when using lazy queries. It should look on the server and update the cache with the most recent data.

How to reproduce the issue:
Execute useLazyQuery hook using fetch-policy as cache-and-network on @apollo/client v3.1.3.

Also, I'm setting cache-and-network as the default fetch policy for all queries. Refer to the config:

const apolloClient = new ApolloClient({
  cache,
  link: ApolloLink.from([
    sentryErrorLink,
    authenticatedLink,
  ]),
  defaultOptions: {
    watchQuery: {
      fetchPolicy: "cache-and-network",
    },
  },
});

Versions

  System:
    OS: macOS 10.15.6
  Binaries:
    Node: 12.16.3 - ~/.nvm/versions/node/v12.16.3/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 6.14.4 - ~/.nvm/versions/node/v12.16.3/bin/npm
  Browsers:
    Chrome: 86.0.4240.80
    Safari: 14.0
  npmPackages:
    @apollo/client: 3.1.3 => 3.1.3 
    apollo-sentry-helper: ^2.0.3 => 2.0.3 
    apollo-utilities: ^1.3.4 => 1.3.4 
@pedro-lb
Copy link

Happening here too

@LauraBeatris
Copy link
Author

The useLazyQuery works if passing the option directly:

useLazyQuery({ fetchPolicy: "cache-and-network" })

But as mentioned on the docs, after triggered it should behave like a useQuery, so it should also use the fetch policy defined on the client, right?

 defaultOptions: {
    watchQuery: {
      fetchPolicy: "cache-and-network",
    },
  },

@sajadghawami
Copy link

i can confirm that setting fetchPolicy: 'network-only', will make it work!

@sannge
Copy link

sannge commented Jan 13, 2021

Happening here too. Any work around?

@LaurenceM10
Copy link

Same issue here.

@vatech-minsik
Copy link

vatech-minsik commented Dec 6, 2021

Not working also with fetchPolicy: 'no-cache'

@RogierdeRuijter
Copy link

Same issue, using cache-and-network doesn't behave like the docs say it does

@fauzanss
Copy link

fauzanss commented Jan 2, 2022

i can confirm that setting fetchPolicy: 'network-only', will make it work!

this is work for me

const client = new ApolloClient({
  link: authLink.concat(httpLink),
  cache: new InMemoryCache({}),
  defaultOptions: {
    watchQuery: {
      fetchPolicy: "network-only",
    },
  }
});

@alexghi
Copy link

alexghi commented Mar 3, 2022

it seems that the watchQuery key is the way where configs for lazy queries should go.
so you can try this, and it will all be fine:

const client = new ApolloClient({
    link: from([retryLink, errorLink, httpLink]),
    defaultOptions: {
	    query: {
		    errorPolicy: 'all',
		    // used for all queries
		    fetchPolicy: 'cache-and-network'
	    },
	    watchQuery: {
		    // this config applies on lazy queries as well
		    fetchPolicy: 'cache-and-network'
	    }
    }
  })

@hwillson hwillson added the 🔍 investigate Investigate further label May 31, 2022
@zirho
Copy link

zirho commented Jul 21, 2023

I have similar issue.
I am trying to set 'cache-and-network' for lazy query and getting null from response for the second request.
I've tried to set client config

    defaultOptions: {
      watchQuery: {
        fetchPolicy: 'cache-and-network',
      },
    },

didn't work for me.

When I switch to regular query, it works just fine with cache.
I am using "apollo/client": "3.7.1",

Thanks

@sm1thana
Copy link

Hi team,
thanks for the great tool!

But I'm facing this issue too.

I tried 3.5.0 version and the latest 3.10.0.
I tried to set network-only and no-cache policy to the useLazyQuery directly.
I tried to set cache-and-network to watchQuery at defaultOptions.

Nothing helped me.

Will try to change useLazyQuery to a common useQuery, or try to remove cached response.

@NiccoloCase
Copy link

I have the same problem with the latest version of Apollo Client!

@NiccoloCase
Copy link

For me, I noticed that using cache-and-network the server is not requested. For some reasons, adding a random @Local field for the type retuned by the server forces the request to the server. but I think that this is a workaround that shouldn't be trusted. So, anyone have a similar issue or has solved the issue? Thanks

@phryneas
Copy link
Member

@NiccoloCase could you please create a reproduction where this is not happening so we can investigate it?

@NiccoloCase
Copy link

@phryneas Hi, thanks for the reply! I spent the past couple of days trying to replicate the issue in a fresh React (web, for simplicity) project using the same API, but I wasn’t able to reproduce it. I don’t think if the fact that it's a React Native project and not a web project can influence test test, but I think it might be due to a unique combination of cache policies along with the cache persistor. As a temporary workaround, I’ve set the nextFetchPolicy to cache-and-network for the lazyQuery, but I’ll continue investigating.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests