Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Ability to override auto-generated key #158

Open
maxdeviant opened this issue Jun 29, 2021 · 2 comments
Open

Feature Request: Ability to override auto-generated key #158

maxdeviant opened this issue Jun 29, 2021 · 2 comments

Comments

@maxdeviant
Copy link

maxdeviant commented Jun 29, 2021

  • 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
Copy link
Author

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.

This was referenced Feb 17, 2022
@ramiel
Copy link

ramiel commented Feb 18, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants