Multiple identical queries - only one is performed

Hi,

I have a case where I need to make the same graphql query multiple times, with the same variables, but with a different authorization token for each request. I am setting the token in the context options of the actual query

 const response = await apolloClient.query({
      query: myQuery,
      fetchPolicy: 'no-cache',
      context: { headers: { authorization: getToken() }}),
    });

However only one of the query requests I generate is actually sent, the rest are fulfilled with the response data of the first request. I realize this must have something to do with the caching - I’ve tried setting fetchPolicy to 'no-cache', and the resultCaching: false in the cache/InMemoryCache options, but still getting the same behaviour.

Was hoping someone could point me in the right direction?

Thanks!

Hey @disl0rn :wave: !

What you’re seeing is query deduplication at work. Query deduplication is enabled by default and will only issue a single query if an existing query is in flight with the same query string, operation name, and variables. You can choose to disable this 1 of 2 ways:

  1. Globally disable query deduplication in the ApolloClient constructor.
const client = new ApolloClient({
  queryDeduplication: false
})
  1. Disable it on a per query basis. You can do this with context.queryDeduplication if you’d like to leave this behavior on by default, but turn it off for select queries.
const response = await client.query({
  context: { queryDeduplication: false }
});

As an FYI, we are considering allowing headers to play a role in query deduplication. We are still figuring out the mechanics of this, but this behavior might be more in line with what you’re expecting.

Hope this helps!

2 Likes

Thanks @jerelmiller, that worked! :star:

1 Like