tRPC query syntax
Can someone please help a Noob here. I'm struggling to work out how to modify the create-T3-app code below for a findUnique or findFirst query where I will need both {ctx} and {input}. Apologies, I'm new to TypeScript !
import { z } from "zod";
import { router, publicProcedure } from "../trpc";
export const exampleRouter = router({
hello: publicProcedure
.input(z.object({ text: z.string().nullish() }).nullish())
.query(({ input }) => {
return {
greeting:
Hello ${input?.text ?? "world"}
,
};
}),
getAll: publicProcedure.query(({ ctx }) => {
return ctx.prisma.example.findMany();
}),
});7 Replies
When writing e.g.
ctx.prisma.example.findUnique(
your IDE should already tell you what your options are thanks to TypeScript (spoiler alert: it's an object with props predefined by prisma).
A regular findUnique
call looks something like .findUnique({where: id: input.id})
most of the timesThanks @Froxx. I kinda got that bit. It's the synax for the publicProcedure.query(({ctx} blar blar {input} bit that has me scratchin'
Oh I see. It's just
.query(({ ctx, input })
@Froxx cheers!
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
@Koen thanks for that. Helpful.