T
tRPC

trpc query running twice

trpc query running twice

Rrustclan1/31/2023
Hey guys. I have a trpc endpoint which makes a request to the discord api to fetch a list of the users guilds.
getGuilds: publicProcedure
.input(z.object({ accessToken: z.string().nullish() }))
.query(async (ctx) => {
console.log(1);
if (!ctx.input.accessToken) return null;
const botGuilds = await prisma.guilds.findMany();
const userGuilds = await getUserGuilds(ctx.input.accessToken);
if (!userGuilds || userGuilds.length <= 0) return [];

const guilds = userGuilds.map((g) => {
const findGuild = botGuilds.find((bg) => bg.guildId === g.id);
g.isPremium = findGuild?.premium || false;
g.inServer = !!findGuild;
return g;
});

return guilds.sort((a, b) => {
if (a.inServer && !b.inServer) return -1;
return 1;
});
}),
getGuilds: publicProcedure
.input(z.object({ accessToken: z.string().nullish() }))
.query(async (ctx) => {
console.log(1);
if (!ctx.input.accessToken) return null;
const botGuilds = await prisma.guilds.findMany();
const userGuilds = await getUserGuilds(ctx.input.accessToken);
if (!userGuilds || userGuilds.length <= 0) return [];

const guilds = userGuilds.map((g) => {
const findGuild = botGuilds.find((bg) => bg.guildId === g.id);
g.isPremium = findGuild?.premium || false;
g.inServer = !!findGuild;
return g;
});

return guilds.sort((a, b) => {
if (a.inServer && !b.inServer) return -1;
return 1;
});
}),
It works fine. However, because this is in my dev environment the TRPC query runs two times, which causes the request to fail due to discord ratelimits. I am using nextjs and I am unsure how I can resolve this. Up until now, I have always used my own api on my server using python, so I could cache things. But with vercel you cannot cache results. Does anybody have any suggestions? Technically, i could make it so the queries only run once in dev. But, this feature is enabled by default for a reason. Also, doing that is just a band aid fix. What if the user refreshes the page twice? Then my app breaks for a few second and the user gets greeted with a error page. Even though they have already recieved the data from the first request.
RRhys1/31/2023
For dev here is how I do it: https://github.com/AnswerOverflow/AnswerOverflow/blob/main/packages/auth/src/discord-oauth.ts I just create a variable to cache the user servers and that says loaded in memory For production, I’d you want to deploy serverlessly then you need to use something like Redis to cache the data or deploy on a normal server If you bring that dev caching approach to prod, you’re also gonna need to use a proper cache that pops off data once it gets too large
Rrustclan1/31/2023
Damn... okay. Thank you very much for the information. Much appreciated 🙂 I like your z_user_schema.parse(data.data); function. Very useful.
RRhys1/31/2023
thank copilot not me I'm probably going to end up doing the Redis approach that I described so you can watch this repository for when I implement that to use it as a reference https://github.com/AnswerOverflow/AnswerOverflow/issues/114 this is the github issue you can watch to be closed
Rrustclan1/31/2023
Awesome! I'm having a look into redis as we speak. Never used it before though, we'll see what happens!

Looking for more? Join the community!

T
tRPC

trpc query running twice

Join Server
Recommended Posts
tRPC Call To ServerOn the mobile version of my app, in NextJS, the build has to be static so I have to use my hosted weGet count of stages assigned to user with stage name?Need help in writing prisma query to get stages assigned to user with stage name? this is the schemeTRPC global loading pageHey. With trpc/nextjs I'm wondering if its possible to have a global loading context/state which is Multiple Clients/Services In tRPC ContextI know it's typical to pass around the prisma client in the trpc context, is the same true for otherUse middleware to send responseHello, how can I send a response from the middleware without executing the input, such as making a cdynamic router creationhas anyone been able to create a dynamic router? e.g. i pass in the schema and some metadata and it Type issue react query with enabled.We migrate to trpc and using @tanstack/react-query directly to the trpc syntax of trpc.procedureNameTRPCClientError: Unexpected end of JSON inputHey I am running into this issue when calling a `mutateAsync` in production in Vercel (**the error dIs it ok to use a createCaller() router within ServerSideProps?The official trpc docs shown an example of how to use the SSGHelper router within SSPs. The procedurtRPC Cors Authentication ErrorHey, I was mainly looking to enable CORS locally for calling my tRPC endpoints in localhost:3000 (NeDemo code `trpc.infinitePosts.add` on infinitedQuery not workingDoc here: https://trpc.io/docs/useInfiniteQuery#getinfinitedata, I created an my query like this: ``NextJS + TRPC + NXi have a nextjs application inside a NX monorepo. i have everything for @trpc/server inside a node lGlobal context or shared context.We are using react query and trpc and we ran into a undesired effect I hope someone could help me wiNot sure how to troubleshoot `Failed to fetch at Function.from`Am getting this on a trpc mutation in v.9 that is firing successfully then pushing me to the URL witthe tRPC loop or cyclewhat is the full sequesnce of execution of trpc? where does it start when a user requests something tRPC context and NextJSGuys quick question about using trpc and nextjs. I'm using context with trpc to create some queries Hardware IoT APIMaybe it's a wrong place to ask but out of curious: Does anyone have experience building API for harHelp to understand how and when to use tRPC in larger projectsHi! I'm pretty sure the answer to my question is that tRPC is not suitable for this project, but anBest practices on trpc and SentryWe are currently migrating to trpc from Apollo server/graphql. We don't use next. What are the best How to do dependecy injection?My routes are grouped by using the router object. i'd like to be able to inject a service to each ro