server side calls trpc
Hi, is there someone who can explain to me how do I make server side call with trpc in t3 ??
25 Replies
did you check the docs
Yes
Since I use the t3 boilerplate
I cant use the examples there
So i just want to execute an mutation
User.update
How would I do that with the T3 config?
For example, I have the appRouter with all my routes
And I just want to do something like
‘appRouter.createCaller()’
However I need some context there and when i try to add it the app crashes.. tried a few stuff but nothing helped
I also tried something like appRouter.mutation… but also a dead end
How would you inmplement it using the T3 boilerplate? @cje
You use the createContext from server/tRPC/context.ts
Fetching lots of data ss is rough. Page loads so slowly if prisma is involved
for example, if you want to expose an endpoint publically, create a normal next api handler:
or maybe you want to do some ssg instead:
let me try this, I want to execute a simple mutation in backend
I want to input data
Can I do this
without endpoing?
endpoint*
yea anywhere on the server
await createContext({ req, res }),
what would be here instead of req / res ?
oh no thats where you use the createContextInner sorry
if you dont have access to the req/res
Got an error now that it wants a session..
ok it says to use ssgHelpers
yea so you would need to get the session, you need the request object for that, or if you dont need the user authed you can pass
session: null
to the createContextInneryes.. thanks let me try to hack something now
I think I got all the info I needed
thank you very much!
you have the req/res in gSSP but note that you cant use that if you use ssr: true for trpc
Essentially I have a method on the server which calculates 2 users from the DB some stats..
after the calculation is over, I want to do a mutation instead of going back to the frontend and doing a mutation there
alright where do you do the calculation
server/common/calculation.ts
yea but where do you call it
Frontend I think
Ye
huh? in a query or smth or just straight up calling server code from the frontend?
I do have trpc call sorry
where i have input the 2 users
and now that you ask me this, I could use just the IDs of the users, do a prisma.find then calculate and result of calculation could call a prisma update?
in the TRPC call?
yea thats what i think initially
yeah, so I just have 1 call and that handles all the logic in one go
yea i don't see any reason to do the query and then a mutation straight after if you can just call it in the query directly
so its the same pattern as with REST in that regard?