blog/the-query-options-api #102
Replies: 13 comments 20 replies
-
That |
Beta Was this translation helpful? Give feedback.
-
Wow this is epic! Can't wait to start using |
Beta Was this translation helpful? Give feedback.
-
Could you expand a little more on what the query-factory achieves and how it differs from "define keys in one central place while having the functions far a way from them in our custom hooks". What was the default patter before react query v5 and how has that changed with the options api? I'm having trouble fitting all the pieces together. Thanks. |
Beta Was this translation helpful? Give feedback.
-
I've been using the query definition pattern for quite a while now and |
Beta Was this translation helpful? Give feedback.
-
You have ads for Query.gg all over the place, but no way to actually purchase it yet. 😢 Eagerly awaiting that email & Tweet saying it's ready and available to purchase. |
Beta Was this translation helpful? Give feedback.
-
Great to see typescript improvements coming with v5.
I guess we can replace that after migrating to v5 with the new |
Beta Was this translation helpful? Give feedback.
-
This is a great addition to an already great library! I'm wondering if you have a recommended pattern for when context provided clients are used (or maybe I'm using it wrong to begin with)? I'm using generated clients (NSwag) that I provide to my application through contexts. This has served me well as an abstraction and allows me to relatively easily set up clients for different environments and use them in my application. My custom query hooks look something like this: export const useUserInformation = (userId: string) => {
const { userInformationClient } = useUserInformationClient()
return useQuery({
queryKey: ["users", "byId", id],
queryFn: () => userInformationClient.getUserInformation(userId),
... // other options
})
} From my components I can then simply use the hook without knowing about the underlying dependency: const { data: userInformation } = useUserInformation(userId) With Pure queryOptions: const userInformationQueries = {
byId: (client: UserInformationClient, userId: string) => queryOptions({
queryKey: ["users", "byId", id],
queryFn: () => client.getUserInformation(userId),
... // other options
})
}
// Usage from a component (works, but must repeat a lot of stuff)
const { userInformationClient } = useUserInformationClient()
const { data: userInformation } = useQuery(userInformationQueries.byId(userInformationClient, userId)) In closure (seems like a better approach if combined with context): const userInformationQueries = (client: UserInformationClient) => ({
byId: (client: UserInformationClient, userId: string) => queryOptions({
queryKey: ["users", "byId", id],
queryFn: () => client.getUserInformation(userId),
... // other options
})
})
// In a component (could be abstracted to a separate context and provided)
const { userInformationClient } = useUserInformationClient()
const { data: userInformation } = useQuery(userInformationQueries(userInformationClient).byId(userId))
// Or if provided from context (still have multiple lines, but at least it hides the client dependency):
const { byId } = useUserInformationQueries()
const {data: userInformation } = useQuery(byId(userId)) |
Beta Was this translation helpful? Give feedback.
-
Thanks for the article, great read! How would you recommend to handle the possibility to add (extend) queryOptions for each query? Let's say I want to disable a certain query in some scenarios, is there any pattern here that handles it in the query factory? |
Beta Was this translation helpful? Give feedback.
-
Thanks for this great article! What would be the best way to set up a query factory in this situation? I'm thinking that systematically retrieving the value as a parameter from the factory would be very cumbersome, and it would force each component to retrieve the value from the store before calling the query. I'm thinking I could do a wrap with, say, useUserQuery and useSuspenseUserQuery, which take care of retrieving the value from the store and injecting it, but Typescript won't be happy about that, as I'm using the QueryFunctionContext to determine the parameters of my API helpers... Sorry if I'm asking a lot, but I'm sure you've come across this situation before 😄 Once again, thank you very much for your work! |
Beta Was this translation helpful? Give feedback.
-
So cool as always ! For the query factory would you still recommend the queryKey as an array of one object like in your article "Leveraging the Query Function Context" ? I love the fact that it forces me to put in the queryKey what I need in my queryFn. That way, there's less chance of misidentifying my query. |
Beta Was this translation helpful? Give feedback.
-
Hey, Thanks for your great Work! |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot Dom. Couldn't we do something similar with mutations? |
Beta Was this translation helpful? Give feedback.
-
blog/the-query-options-api
v5 brought a new, powerful API, especially if you're using React Query with TypeScript...
https://tkdodo.eu/blog/the-query-options-api#inject-comments
Beta Was this translation helpful? Give feedback.
All reactions