function
Throw NOT_FOUND on nullish
I have many procedures like this:
getFoo: protectedProcedure.query(({ input }) => db.foo.findFirst({ id: input.id })
This example uses Drizzle, which doesn't have a findFirstOrThrow
convenience function.
Since i want to throw TRPCError
when the DB record couldn't be found, i'd have to write this code everywhere:Is there a nicer way to achieve this when dealing with nullish return values?
My research so far:
- Output validators are not a solution because when they fail it results in a INTERNAL_SERVER_ERROR
.
- Middleware sounds like it should be the natural solution but the documentation doesn't show any examples accessing procedure data, i.e. post-procedure middleware. There is a discussion on accessing req/res but the solution instructing to use context sounds very hacky and will definitely not get me type-safe results.
- There's also a Drizzle issue to add findFirstOrThrow
from 2023 but it doesn't look like they'll implement this anytime soon.
- This actually works and is type-safe (removes nullish from resulting type) but kind of ugly in usage:
2 replies
Why not create queryClient and trpcClient outside of React?
From the docs: https://trpc.io/docs/client/react/setup#4-add-trpc-providers
Relevant excerpt:
What are the downsides to moving these two clients outside the component? Does it break hot reloading or something?
E.g.
4 replies
Why are `new QueryClient` and `trpc.createClient` run inside a component in the React setup?
From https://trpc.io/docs/client/react/setup:
if these two are "made stable" through useState() and since they should exist only once per SPA (like a singleton), why not just put them outside the component?
is this related to HMR?
25 replies
Globally handle specific Error type on backend
Hi, i have a lot of code that's used for both backend and frontend.
That's why i'm not throwing TRPCErrors but custom errors instead. They indicate that the error message should be sent to the client if it arises on the backend (on the frontend this obviously doesn't matter).
Can i set up the tRPC server side in a way where it will catch all errors so i can do something like this?
7 replies
404 TRPCError: no query procedure on path
Hi, this is my entire standalone tRPC server:
and here is my tRPC client:
but when i use it with
trpc.foo.useQuery()
, this the response:
What am i doing wrong? I already tried removing /trpc
from the url that the client uses which also resulted in 404.18 replies
Why does this starter with Prisma have it's own postinstall script?
Prisma has it's own postinstall hook/script: https://www.prisma.io/docs/concepts/components/prisma-client/working-with-prismaclient/generating-prisma-client#generating-prisma-client-in-the-postinstall-hook-of-prismaclient
The starter's
package.json
that also has that hook/script:
https://github.com/trpc/examples-next-prisma-websockets-starter/blob/420be5a7916f4d91ae2307f484ce38c1f5c6c67c/package.json#L334 replies
next-prisma-websockets-starter seeds twice on 'pnpm dx'
Hi, i'm using this starter template for my app. The
dx
script from package.json runs both prisma migrate
as well as prisma seed
and the former seems to run the seeder as well, causing data to exist twice in the database after running dx
.
Link to line in package.json: https://github.com/trpc/examples-next-prisma-websockets-starter/blob/db3a7794caa8d024f7115ce1f767d84c0172dd93/package.json#L212 replies