tRPC

T

tRPC

Move Fast & Break Nothing. End-to-end typesafe APIs made easy.

Join Server

Community questions

NMNick Montoya9/30/2023

Basic Inference not Working

I have been struggling to get basic type inferencing to work with trpc, react, and Drizzle ORM.

This is what my server-side router is returning from the list method:
export const organizationRouter = router({
  list: adminProcedure.query(async () => {
    const response = await listOrganizations.all();
    return response;
  }),

Where, if I hover over the response in the return, it says the type is:
```
const response: {
id: number;
name: string;
description: string | nu...
Hhaardik9/30/2023

tRPC Websockets with a standalone Bun Server?

Hey folks!

Experiementing with setting up a standalone tRPC Server using Bun. The HTTP part is great - i'm wondering if anyone has succeeded getting it to work with Bun's websocket server? if not, i'll continue figuring it out and hopefully add an adapter to tRPC.
RSRohit Saini9/30/2023

Infinite session query when tab is in background

So, when I am in development mode, doing some changes in my code and my application's tab is not in focus, then there is an infinite loop of session query. I can see the query running in my logs. I don't know what the reason it. Can anyone please help me.
IAAI am a Dev9/30/2023

How to return a stream? like open ai api

I want that my api can have stream responses like open ai
https://github.com/openai/openai-node#streaming-responses
how can i do it?

Thanks
KKatzius9/29/2023

was wondering if anyone got a chance to use Ajv as input/output validator?

I have been trying to use Ajv https://ajv.js.org/ with tRPC, but I wasn't able to get the types working, no matter what I do the input type is always boolean, regardless of the schema I am using.
MMuezz9/29/2023

state change does not refetch the query and the app freezes

https://github.com/trpc/trpc/discussions/4856

I am not sure if linking is allowed but I opened a discussion in the repo and have gotten no responses. I am hoping someone here can help me.

It is very likely a mistake on my part and would really appreciate it if someone could point me in the right direction.
Kkamama9/29/2023

trpc subscription with react-query

TRPCClientError: Subscriptions should use wsLink

having error when use subscription with react-query


import { createTRPCReact } from "@trpc/react-query";
export const trpc = createTRPCReact<AppRouter>();

const wsClient = createWSClient({
url: ws://localhost:8080/ws,
});

const trpcClient = trpc.createClient({
transformer: SuperJSON,
links: [
httpLink({
url: /trpc,
}),
wsLink({
client: wsClient,
}),
],
});

<trpc.Provider client={trpcClient} queryClient...
Hhaardik9/28/2023

Unable to get mutation to trigger subscription because EventEmitter not being shared

Hey folks,

Been struggling with this for a few hours now hopelessly and trying random things - read all related posts in this forum, on github issues, and stackoverflow - and still don't understand what is going on.

I have a next app with a custom HTTP server and using tRPC. WSLink etc is all fine - i'm doing everything the proper way.

I have a router with these two functions:
```ts
sendMessage: protectedProcedure
.input(z.string())
.mutation(async ({ ctx, input }) => {
try {...
IAAI am a Dev9/28/2023

Is there a guide of how to test?

I'm usually use vitest but in frontend of shared packages.
I'm frontend trying to create my first api 🙂

Thanks
RRyan9/28/2023

Modify the payload before mutating

I have a specific mutation hook that is used all throughout my app, and inside of the hook I want to modify the payload before it is sent to the server (to read something from local storage and add it to payload). Is this possible? I can't seem to find a way to do this. There also isn't a way AFAIK to modify the mutationFn with the nextjs adapter.

My only solution would be redefining mutate and mutateAsync that are modified to do this preprocessing.

Thanks!
IAAI am a Dev9/28/2023

is there a simple boilerplate that has everything ready to deploy?

For example nodemon, reading the port for .env, etc. (I don't know if that's how it is haha)

I use turborepo, so can be an internal package.

But I want to see how the project configuration looks for the deploy.

It's the first time I am creating a server, for now it will be deployed on a PC on an intranet.

It's just a server to everyone in the intranet can access and modify a json file.

Thanks
Vvenego9/28/2023

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

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?

```ts
const trpc = initTRPC.context<{
userSession: { id: string, authenticated: boolean }
}>().create();

const baseP...
V21Vic9/27/2023

Using tRPC for server to server requests

Hey everyone! The start up I’m at recently deployed a server separate from our main server. Our main server uses a combination of tRPC and Express endpoints on Node 16.14.0. When we introduced this separate server we set up some functions to make requests from our main server to the separate server.

The core of these functions is encrypting + compressing requests to the separate server as well as decrytping + decompressing on the separate server and then again on the way back (the response).

T...
Bbanze9/27/2023

ReferenceError: FormData is not defined

I am using the experimental form data as shown in the examples and it works correctly on my machine, but when deploying to DigitalOcean App Platform, I get the error you see below.

ReferenceError: FormData is not defined in /node_modules/@trpc/server/dist/adapters/node-http/content-type/form-data/index.mjs for:
    const formData = new FormData();
    const parts = streamMultipart(Readable.toWeb(request), boundary);


Can anyone save me?
TTkDodo9/27/2023

TRPCError that has TRPCError as cause

Having some troubles with error handling, specifically, my errors are double-wrapped in a TRPCError.

What I'm doing is basically having a mutation that does:

something: t.procedure
  .input(mySchema)
  .mutation() => Promise.reject(new Error('oh no'))),


and then, the onError handler has:

onError({ error }) {
  console.log('onError', error.name, error.cause.name)
},


which logs: onError TRPCError TRPCError. If I then go further with error.cause.cause.name, I can see my er...
DDenam9/27/2023

How 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 function contextInputValidation(
    callback: (options: { ctx: Context }) => zod.Schema,
) {
    return middleware(async ({ next, ctx, rawInput }) => {
        const schema = callback({ ctx });
        const input = schema.parse(rawInput);
        const stack = await next({
            rawInput: input,
        });

        return stack;
    });
}


However, how can I pass the value of this input...
AAerys9/27/2023

Why 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 () => {
        return "hi!";
    }),

const { error } = api.user.test.useQuery("test");


(it should return a Zod validation error)

the client tries to refetch 3 times and then returns the error (4-5 seconds of delay), throughout the process the error is logged by the loggerLink, but the error returned takes a while, but when I switch to useMutation() the error is returned rather instantly, what...
Solution:
This is a feature of react query, you'll find retry details in their docs
HMHan Moos9/26/2023

Lambda WITHOUT API Gateway (Lambda Function URL)

Anyone know how to make this work?
IAAI am a Dev9/26/2023

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 both, in others only 1

Thanks