graphql-codegen-plugin-typescript-swr icon indicating copy to clipboard operation
graphql-codegen-plugin-typescript-swr copied to clipboard

Feature Request: Ability to override auto-generated key

Open maxdeviant opened this issue 3 years ago • 2 comments

  • 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.

maxdeviant avatar Jun 29 '21 14:06 maxdeviant

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.

maxdeviant avatar Jul 02 '21 13:07 maxdeviant

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

ramiel avatar Feb 18 '22 12:02 ramiel