apollographql / apollo-feature-requests

🧑‍🚀 Apollo Client Feature Requests | (no 🐛 please).
Other
130 stars 7 forks source link

Custom cache key for queries #316

Open richardwu opened 3 years ago

richardwu commented 3 years ago

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.

ghost commented 1 year ago

+1

richardplatel commented 4 months ago

+1

yaredtsy commented 2 months ago

+1