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
[RFC] Built in react client #13
Comments
|
Let me know if you have other ideas for the client |
|
Another great addition would be Given that i completely control the queries i can make assumptions about queries types and manipulate them accordingly This way the hook function can contain the pagination logic that changes the query arguments (and is also type safe) It would look something like const Page = () => {
const { items, nextPage } = usePaginatedQuery(
{ itemsPerPage: 10 },
({ first, after }) => {
return {
users: [
{
first,
after,
},
{
edges: {
...everything,
},
pageInfo: {
...everything,
},
},
],
}
},
)
return (
<div>
{items.map((x) => x.name)}
<button onClick={nextPage}>more</button>
</div>
)
} |
IMO this is not a big issue since it can just be avoided by declaring a function not inline, and it's what I naturally do when I follow React Query docs, e.g. import { useQuery } from "react-query";
import QueryCacheKey from "@/constants/queryCacheKeys";
import useSdk from "@/api/sdk";
export default function useItem(id?: string) {
const sdk = useSdk();
const getItem = (key: string, id?: string) => {
return sdk.query({
item: [
{
id,
},
{
id: true,
name: true,
description: true,
code: true,
created_at: true,
updated_at: true,
property_values: [
{},
{
item_property_id: true,
text: true,
number: true,
},
],
},
],
});
};
return useQuery([QueryCacheKey.Item, id], getItem, { enabled: !!id });
}
export type ItemResult = ReturnType<typeof useItem>["data"];
What do you mean here by "defer the mutation execution"?
I think that subscriptions need more thinking than normal queries. For example, if you have a paginated query, how do you make it realtime with subscription? When using Apollo I used to just swap For my apps, I tend to use subscriptions as a notifier (e.g. I listen to |
There is an example here
There is an example of
Above i don't pass any reducer function, but you could get so it works just like a If you instead pass a reducer that accumulates the subscription results you could get a stream of notification for example const { data, loading, error } = useSubscription(
{
onNewMessage: {
message: true,
}
},
(acc: string[], item) => [...acc, item.message],
)I think this is pretty extensible, do you have any other ideas for I am still trying these concepts on my application to test all the different use cases, i am open to changes |
I think I'm not getting it. What problem is this trying to solve that something like React Query has? The syntax looks almost identical to me
How do you update the cache when you receive new data? Are you planning to expose swr |
|
Why do you consider general purpose libraries instead of a more specialized one like
The main advantage I see picking |
|
Sorry for the double post but we have now a common type standardized around client consumption : https://github.com/dotansimha/graphql-typed-document-node I think the best course of action would be to end with a TypedDocumentNode, this is what tql is doing and it seems to work great! So we can build the query using genql syntax but defer the execution to any client compatible with TypedDocumentNode! |
|
^ I'm also interested in this - we're looking for a typesafe query client that can generate TypedDocumentNode so it can be used with any client. Specifically looking to use urql |
To see an example possible client usage check out this code
Using genql with generic react fetching libraries like
react-queryandswris not optimal:apollo, where you can defer the mutation execution (when a button is clicked for example)urqldoes)This is why I want to build a native react client based on
swr, the exported functions will be similar to theapolloones, but caching, polling, etc will be handles byswrThe exported functions will be
useQuery, tiny wrapper arounduseSwruseMutation, does not cache results, can be deferred unit an execute functionuseSubscription, similar to useSubscription fromurql, gets a reducer argument that aggregates subsequent subscription results into an accumulatorThis client will be generated when you add a
--reactflag, it will require answrpeer dependencyThe text was updated successfully, but these errors were encountered: