nuxt-modules / apollo

Nuxt.js module to use Vue-Apollo. The Apollo integration for GraphQL.
https://apollo.nuxtjs.org
MIT License
956 stars 198 forks source link

useAsyncQuery ignores cache as an input #536

Closed cRoberts2-18 closed 8 months ago

cRoberts2-18 commented 1 year ago

Environment


Describe the bug

The cache option in the type TAsyncQuery is not used in the prep function used as a part of useAsyncQuery and useLazyAsyncQuery is not used and the fetch policy on the query that is executed is hard coded to use no-cache

Expected behaviour

The cache boolean should have an effect on whether the query that is used is cached

Reproduction

This can be seen in apollo/runtime/composables.ts in the prep function

const prep = (...args: any) => {
  const { clients } = useApollo()

  const query = args?.[0]?.query || args?.[0]
  const cache = args?.[0]?.cache ?? true
  const variables = args?.[0]?.variables || (typeof args?.[1] !== 'string' && args?.[1]) || undefined
  let clientId = args?.[0]?.clientId || (typeof args?.[1] === 'string' && args?.[1]) || undefined

  if (!clientId || !clients?.[clientId]) {
    clientId = clients?.default ? 'default' : Object.keys(clients!)[0]
  }

  const key = args?.[0]?.key || hash({ query: print(query), variables, clientId })

  const fn = () => clients![clientId]?.query({ query, variables, fetchPolicy: 'no-cache' }).then(r => r.data)

  return { key, query, clientId, variables, fn }
}

As you can see the fetchPolicy is hard coded

Additional context

This is causing problems in an enterprise environment as it is slowing down the speed of our app in some cases quite dramatically.

Logs

No response