Apollo-client: useLazyQuery doesn't work with cache-and-network fetch policy

Created on 16 Oct 2020  路  4Comments  路  Source: apollographql/apollo-client

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 

Most helpful comment

Happening here too

All 4 comments

Happening here too

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",
    },
  },

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

Happening here too. Any work around?

Was this page helpful?
0 / 5 - 0 ratings