T
tRPC

Context is not fully globally accessed? [ probably newbie question ]

Context is not fully globally accessed? [ probably newbie question ]

Vvenego9/28/2023
I create a context in the based procedure, but It's undefined in the procedure based on it. Also the opts.ctx doesn't exist before I create it, although I've used .context() upon initializing trpc, but that's no big deal. NVM I should've defined it upon server creation. Context defined by a procedure cannot be seen by another procedure?? how do I get around that?
const trpc = initTRPC.context<{
userSession: { id: string, authenticated: boolean }
}>().create();

const baseProcedure = trpc.procedure.use((opts) => {
opts.ctx = {
userSession: {
id: 'dummy',
authenticated: false
}
};

return opts.next()
});
const protectedProcedure = baseProcedure.use((opts) => {
// ctx is undefined
if (!opts.ctx.userSession.authenticated) {
throw new TRPCError(
{ code: 'UNAUTHORIZED', cause: 'because!' }
);
}
return opts.next();
});
const trpc = initTRPC.context<{
userSession: { id: string, authenticated: boolean }
}>().create();

const baseProcedure = trpc.procedure.use((opts) => {
opts.ctx = {
userSession: {
id: 'dummy',
authenticated: false
}
};

return opts.next()
});
const protectedProcedure = baseProcedure.use((opts) => {
// ctx is undefined
if (!opts.ctx.userSession.authenticated) {
throw new TRPCError(
{ code: 'UNAUTHORIZED', cause: 'because!' }
);
}
return opts.next();
});
LLucas9/28/2023
you should define the ctx variable by passing it as an argument to the opts.next() function you are returning like this:
const baseProcedure = trpc.procedure.use((opts) => {
return opts.next({
ctx: {
userSession: {
id: 'dummy',
authenticated: false
}
}
})
});
const baseProcedure = trpc.procedure.use((opts) => {
return opts.next({
ctx: {
userSession: {
id: 'dummy',
authenticated: false
}
}
})
});
otherwise even though you are doing opts.ctx = ..., that does nothing You can see more about how context extension works here -> https://trpc.io/docs/server/middlewares#context-extension
Vvenego9/28/2023
Thanks @Lucas Thevenet, that saved my day, I guess reading the docs starting from the top is not that practical before I finish reading it.
LLucas9/28/2023
Glad I could be of help! šŸ‘

Looking for more? Join the community!

T
tRPC

Context is not fully globally accessed? [ probably newbie question ]

Join Server
Recommended Posts
Using tRPC for server to server requestsHey everyone! The start up Iā€™m at recently deployed a server separate from our main server. Our mainReferenceError: FormData is not definedI am using the experimental form data as shown in the examples and it works correctly on my machine,TRPCError that has TRPCError as causeHaving some troubles with error handling, specifically, my errors are double-wrapped in a TRPCErrorHow do you make use of custom input validation?I have an input validation middleware, to parse my input based on some values in ctx. ``` export fuWhy useQuery() tries to refetch when error occurs while useMutation() doesn't?I'm using the T3 stack. ```test: publicProcedure.input(z.string().min(5)).query(async () => { Lambda WITHOUT API Gateway (Lambda Function URL)Anyone know how to make this work?In a monorepo can I have 2 packages each one with different trpc server, and use both in 1 app?I want to create 2 different APIs, and in my monorepo I have several apps, in some I want to use botHave you used electron-trpc? how does it work for you? Do you recommend it?https://www.electron-trpc.dev/ Thankstrpc error fetch failedplease helpHow to type a helper function to standardise loading and error states with a useQuery call?I am trying to standardize handling of error and loading states. I'd like to have one function/compoTypeScript Issue: router.createCaller Implicitly has Return Type "any"Hello everyone, I'm currently working on a project using tRPC and Prisma. However, I've run into an2 react renders causing 4 trpc query executesI see that my nextjs page is rendering twice on initial load. This is causing 4 executes of my querySuspend subscriptions when app is in backgroundWhen using React Native, subscriptions stay open even when the app is in the background. While this Callbacks in Consecutive MutationsI seem to be running into this issue https://tanstack.com/query/v4/docs/react/guides/mutations#conse