Ayush Goyal
Ayush Goyal2y ago

opts object passed to mutation , i can not understand where it came from

export const authorizedProcedure = publicProcedure
.input(z.object({ townName: z.string() }))
.use((opts) => {
if (opts.input.townName !== 'Pucklechurch') {
throw new TRPCError({
code: 'FORBIDDEN',
message: "We don't take kindly to out-of-town folk",
});
}
 
return opts.next();
});
 
export const appRouter = t.router({
hello: authorizedProcedure.query(() => {
return {
message: 'hello world',
};
}),
goodbye: authorizedProcedure.mutation(async (opts) => {
await opts.ctx.signGuestBook();
 
return {
message: 'goodbye!',
};
}),
});
export const authorizedProcedure = publicProcedure
.input(z.object({ townName: z.string() }))
.use((opts) => {
if (opts.input.townName !== 'Pucklechurch') {
throw new TRPCError({
code: 'FORBIDDEN',
message: "We don't take kindly to out-of-town folk",
});
}
 
return opts.next();
});
 
export const appRouter = t.router({
hello: authorizedProcedure.query(() => {
return {
message: 'hello world',
};
}),
goodbye: authorizedProcedure.mutation(async (opts) => {
await opts.ctx.signGuestBook();
 
return {
message: 'goodbye!',
};
}),
});
here in goodbye route , where does the opts object come from , in authorized proccesor middleware , i get it otps come from the input but in goodbye i am not able to understand where opts come from , maybe
return opts.next();
return opts.next();
passes it to the goodbye route
2 Replies
Ayush Goyal
Ayush GoyalOP2y ago
or maybe i got it , opt.next() redirects us to the next route or middleware and since it has no middleware it direacts us to the next route right , also every opts has three parameters , input , context and next , sure we have not defined input so we can not use it but we can use context and next
Nick
Nick2y ago
Opts is just a bundle of stuff that we give middlewares and procedures Everything is just a middleware though, your procedure is just the terminating middleware, so calling next calls whatever is next in the chain, be it a middleware or a procedure. But the opts object is composed automatically under the hood