Jokcy
Jokcy
Explore posts from servers
TtRPC
Created by Jokcy on 10/28/2023 in #❓-help
Is there any easy way to create a Type for a trpc router
I want to publish a set of trpc api client to npm, which will be used by my customer directly. But if I use typeof MyRouter to infer the Router type, my bundler (which is tsup) will bundle db schema types into the d.ts file. If I want my customer to gain the typesafe ability I have to make my db schema info public. Is there any way to define a Router type without typeof MyRouter type infer? So that I can expose limited info to public.
3 replies
TtRPC
Created by Jokcy on 2/27/2023 in #❓-help
Cache not working for `useQuery`
I have a query like this:
const { data: article, isFetching } = api.public.getArticle.useQuery({
id: targetId,
});
const { data: article, isFetching } = api.public.getArticle.useQuery({
id: targetId,
});
which targetId is a dynamic value. When I change targetId from a to b then switch back from b to a, the useQuery hook still send a new request for a instead of using cache. Any idea how to make the cache work?
6 replies
TtRPC
Created by Jokcy on 1/22/2023 in #❓-help
Demo code `trpc.infinitePosts.add` on infinitedQuery not working
Doc here: https://trpc.io/docs/useInfiniteQuery#getinfinitedata, I created an my query like this:
infinitePosts: protectedProcedure
.input(
z.object({
limit: z.number().min(1).max(100).nullish(),
cursor: z.string().nullish(), // <-- "cursor" needs to exist, but can be any type
})
)
.query(async ({ ctx, input }) => {
const limit = input.limit ?? 20;
const { cursor } = input;
const items = await ctx.prisma.post.findMany({
take: limit + 1, // get an extra item at the end which we'll use as next cursor
cursor: cursor ? { id: cursor } : undefined,
orderBy: {
updatedAt: 'asc',
},
});
let nextCursor: typeof cursor | undefined = undefined;
if (items.length > limit) {
const nextItem = items.pop();
nextCursor = nextItem!.id;
}
return {
items,
nextCursor,
};
}),
infinitePosts: protectedProcedure
.input(
z.object({
limit: z.number().min(1).max(100).nullish(),
cursor: z.string().nullish(), // <-- "cursor" needs to exist, but can be any type
})
)
.query(async ({ ctx, input }) => {
const limit = input.limit ?? 20;
const { cursor } = input;
const items = await ctx.prisma.post.findMany({
take: limit + 1, // get an extra item at the end which we'll use as next cursor
cursor: cursor ? { id: cursor } : undefined,
orderBy: {
updatedAt: 'asc',
},
});
let nextCursor: typeof cursor | undefined = undefined;
if (items.length > limit) {
const nextItem = items.pop();
nextCursor = nextItem!.id;
}
return {
items,
nextCursor,
};
}),
Then on my client: api.post.infinitePosts, but there is no .add or .delete on it, am I miss something or is there any detail doc?
1 replies