Apollo Client - Query Deduplication

Hey folks,

I’m using Apollo Client version 3.4.9 and I’m having difficulties with client queryDeduplication.

I’m calling resetStore after user-interaction to refetch any active queries. This will work out just fine and the queries are getting refetched.

My issue comes down to the scenario when resetStore is called before the previous resetStore queries are resolved. This is an issue because behind the scenes I’m swapping one header and I want to refetch the query with this new header in place.

My understanding is that queryDeduplication will look for const { query, variables, operationName } = operation and will not compare if the headers are different from the previous query.

I wrote my own wrapper for refetching the active queries:

  refetchActiveQueries = ({ hash }: {hash: string} = { hash: '' }): Promise<ApolloQueryResult<any>>[] => {
    if (!this.client) throw new Error('Apollo client not available')

    // Check for reference:
    // https://github.com/apollographql/apollo-client/blob/a4b03cdb69ffb5164f0db2e34f842307ab95dba4/src/core/QueryManager.ts#L828
    const observableQueryPromises: Promise<ApolloQueryResult<any>>[] = []
    const queries = this.client.getObservableQueries('active')
    queries.forEach((observableQuery) => {
      const { fetchPolicy, variables } = observableQuery.options
      observableQuery.resetLastResults()
      if (fetchPolicy !== 'standby' && fetchPolicy !== 'cache-only') {
        // Add unique hash as variable to workaround queryDeduplication
        observableQueryPromises.push(observableQuery.refetch({ ...variables, hash }))
      }
    })
    return observableQueryPromises
  }

So in the end I want to tell QueryManager that the possible inflight queries are actually different and should be refetched. I’m currently using refetchActiveQueries in conjunction with clearStore.

In the ideal case I wouldn’t have to create this wrapper function for refetching the active queries and I could use custom ApolloLink to attach the hash as additional variable to the query but that didn’t seem to work with queryDeduplication enabled.

Any suggestions on how to resolve this using Apollo internal tooling?