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

Conditional Fetching

Open raymclee opened this issue 3 years ago • 4 comments

  • I'm submitting a ... [ ] bug report [ ] feature request [ ] question about the decisions made in the repository [ X] question about how to use this project

  • Summary

  • how to use Conditional Fetching with the generated query?

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

  • https://swr.vercel.app/docs/conditional-fetching

raymclee avatar Jun 26 '21 03:06 raymclee

Interested in how people are going about this too!

fishactual avatar Dec 06 '21 23:12 fishactual

It's the same as a normal SWR hook:

const { data, mutate } = getSdkWithHooks().useMyQuery(isTrue ? 'QueryName' : null);

adarnon avatar Feb 16 '22 16:02 adarnon

This is when the key is provided by the user. Is there any alternative when the key is autogenerated? I think it's not possible at the moment. If I can propose some solutions:

  • Add a skip config so that the function that generates the key returns null in case skip is true
  • Implement #158 and this would be solved as well

ramiel avatar Feb 17 '22 11:02 ramiel

While waiting for this feature to be implemented, I create a fork (published on npm) that let you conditional fetch (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