-
Notifications
You must be signed in to change notification settings - Fork 17
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
Return query key from useQuery #467
Comments
Hi @anuraaga, this is an interesting proposal. We could of course do it ourselves, although this feels more like something that would benefit all of @tanstack/query. In fact, this item was already brought up here. Part of me wants to hew close to whatever tanstack query does but I can see the value here. I think the only argument I really see against returning the queryKey is that that key is now disjoint from the associated data stored in the cache. Which means that it will allow you to do something like: queryClient.setQueryData(getStuffKey, { somethingTotallyWrong: true }); Which will cause everything to break. We have some potential work coming after the v2 release that eases this slightly with a custom queryClient that would instead allow you to do something like: queryClient.setConnectQueryData(getStuff, { stuffId }, (prev) => {
return {
...prev,
stuff: [...prev.stuff, resp.newStuff],
};
}); This method enforces type safety and makes sure that the new data incoming matches the cached data types. It still requires you building the same input, but I personally think that's pretty minor compared to ensuring static type safety. |
Thanks for the consideration @paul-sachs. TBH I have only every used react query with connect, so I'm not sure on idiomatic vanilla usage but IIUC there it didn't make much sense since the user creates the key, and it's trivial to just reference that without returning it. Here, since connect-query is creating it, it could make sense to return what it created. The final blog post linked in the discussion was interesting https://tkdodo.eu/blog/the-query-options-api#datatag It seems query already has the concept of associating the return type with the key! If the returned key has the datatag with the response type, it seems like the other methods like setQueryData should be type-safe even without a custom QueryClient. Perhaps this provides even more motivation to return the key? |
BTW, I prototyped the idea here The key point is calling This was actually my first experience with connect-query's So I guess I don't see a big reason to return query key anymore, while it could be convenient for
|
Interesting note on the It doesn't entirely eliminate the usefulness of a custom query client, but it does make me think about how we can reduce our surface area. Having it for fetch/prefetch are likely still enough to have it exist but we'll see how investigations go. You can also use |
useQuery
is the primary entrypoint for fetching data and manages creating a query key and setting the query function as per the README. I was wondering if it would make sense to have it return the query key it created? It feels a bit tedious to have to create it again in mutations.The text was updated successfully, but these errors were encountered: