Best Practices for Using tanstack-react-query with GraphQL in a Monorepo Structure
Hey 👋🏽 ,
I am using tanstack-react-query in a monorepo. I have a package called
shared-features
. This package contains features, which uses react-query. I'm using GraphQL, so I can't just use a defaultQueryFn. I need a way to inject a query-fn.
Here's my current setup at the main-app, which should consume the shared-features
-package.
Here's how I make use of the queries:
In the package shared-features
I dream of hooks like this one
Do you have a solution for my issue?
Thanks!13 Replies
foreign-sapphireOP•2y ago
Hey @TkDodo 🔮,
Can you please help me here? https://discord.com/channels/719702312431386674/1225012341448376371/1225012341448376371
If possible, I would like to use react-query features and not use a custom context.
Thanks
protestant-coral•2y ago
What's the question here? What is
injectedQueryFn
supposed to be? I don't even see a real question... please do betterforeign-sapphireOP•2y ago
Oh, sorry. I mean, I look for a feature to achieve a
queryFn
injection, so I can share my custom useQuery
hooks in a monorepo structure. The pseudo code using injectedQueryFn
should just show my use-case.
I cannot use the defaultQueryFn
-ft, because I have to pass the gql-query, not just the queryKey.
So my question is: What's the recommended way to provide custom useQuery
- hooks via a package in a monorepo? Or, how do you solve this? Surely you also share features that use useQuery
in a monorepo and have different endpoints, or different ways to request the endpoints.
Adding a custom value to the QueryClient
would be a solution, but I could not achieve that so far.
@TkDodo 🔮 Can you follow my request now? Do I have to solve my problem outside of react-query, for example with a React context?protestant-coral•2y ago
sorry, no, I don't understand. Is this graphQL specific? Why can't you just add a static queryFn to your custom hook?
instead of:
do:
why do you need the queryClient for that ?
foreign-sapphireOP•2y ago
Cause
someFunction
is context-specific. someFunction
is different in the library showcase than in the consuming app.
hook in the shared-lib:
In app:
In the showcase of the shared lib:
protestant-coral•2y ago
then you just need to provide your own context and have
someFunction
come from useSomeContext()
rather than an import. This is standard dependency injectionforeign-sapphireOP•2y ago
I understand, I was hoping that I could manage without my own context.
How do you do this if you have different endpoints in different apps that consume the same shared custom
userQuery
hooks?protestant-coral•2y ago
you provide an api client to your app as a singleton, which has a baseURL set. Then your queryFns call
api.request(...)
foreign-sapphireOP•2y ago
and you inject that api client via react context at the apps?
protestant-coral•2y ago
that's the react way to provide things. Why does it sound like you think that's a bad thing?
useQueryClient()
also uses context ...foreign-sapphireOP•2y ago
I don't think, that this is a bad thing. React context is great.
I just thought it would be cool to include the api client in react-query. But the more I think about it, it's better outside of react-query anyway. That means your shared-features lib also provides the context. And you also use this context for userQuery-hooks directly in the app?
I just thought it would be cool to include the api client in react-query. But the more I think about it, it's better outside of react-query anyway. That means your shared-features lib also provides the context. And you also use this context for userQuery-hooks directly in the app?
protestant-coral•2y ago
this is hypothetical because I don't have such a structure
foreign-sapphireOP•2y ago
Good morning @TkDodo 🔮 ,
thanks for your help. I have now made an apiClient available via a custom react context in order to be able to use my library useQuery-hooks in the app.
I still have a problem with the QueryOptions-functions, because it is not a hook.
Do you have a solution for that?
Thanks