graphql-codegen-plugin-typescript-swr
graphql-codegen-plugin-typescript-swr copied to clipboard
Feature Request: Ability to override auto-generated key
-
I'm submitting a ... [ ] bug report [x] feature request [ ] question about the decisions made in the repository [ ] question about how to use this project
-
Summary
We're using the autogenSWRKey
option (which is great!), but we have some situations where we need more fine-grained control over the key.
However, it seems onerous to disable autogenSWRKey
entirely just for a few edge-cases. It would be nice if there was a way to override the generated key on a case-by-case basis.
- Other information (e.g. detailed explanation, stack traces, related issues, suggestions how to fix, links for us to have context, eg. StackOverflow, personal fork, etc.)
I think something like this would do the trick:
useMyQuery(
variables: MyQueryQueryVariables,
config?: SWRConfigInterface<MyQueryQuery, ClientError>,
keyOverride?: SWRKeyInterface,
) {
return useSWR<MyQueryQuery, ClientError>(
keyOverride ?? genKey<MyQueryQueryVariables>('MyQuery', variables),
() => sdk.myQuery(variables),
config,
);
}
I'd be happy to open a PR for this.
I think this would potentially resolve #124 as well.
Thinking through this some more, I wonder if a better API would allow for extending the generation of a key in addition to overriding it?
In our particular case we needed to add an additional piece of data to the key, but we still wanted to keep the rest of the key intact.
While waiting for this feature to be implemented, I create a fork (published on npm) that let you customise the key (and other things). https://github.com/ramiel/graphql-codegen-plugin-typescript-swr
I also made a PR with a POC