apollo-feature-requests icon indicating copy to clipboard operation
apollo-feature-requests copied to clipboard

Custom cache key for queries

Open richardwu opened this issue 4 years ago • 3 comments

It would be nice to be able to specify additional "keys" for the caching logic of a query. This was mentioned in https://github.com/apollographql/apollo-client/issues/1420 but was subsequently closed.

Concretely, if I'm passing additional information like target language in the header, it'd be convenient to not have to modify all my GraphQL endpoints to accept (and subsequently discard) the variable.

I've tried the following:


const FOO_QUERY = gql`
query foo {
  foo {
     ...
  }
}
`;

useQuery(FOO_QUERY, {variables: {lang: 'en'}})

But the above will still share the cache with other lang variables.

You need to include $lang as a variable to the GraphQL query:


const FOO_QUERY = gql`
query foo($lang: String) {
  foo {
     ...
  }
}
`;

But this will raise the error Variable $lang is never used in operation if $lang is not used. Doing the following:


const FOO_QUERY = gql`
query foo($lang: String) {
  foo(lang: $lang) {
     ...
  }
}
`;

works, but will require me to add the superfluous lang variable to my foo endpoint.

richardwu avatar Oct 24 '21 23:10 richardwu

+1

ghost avatar Jul 06 '23 14:07 ghost