is it bad to write to database in .query()

I do get confused sometimes i write my ctx.prisma.user.create({...}) In a .query() Instead of .mutation()
73 Replies
amanuel
amanuel•3y ago
Yeah, use .mutation() instead useQuery defaults to true to a lot of refetch options, so you wouldn't want that unless you're actually getting data that you want to display and persist state between client and server
Lopen
LopenOP•3y ago
I am talking of the server side not client side
Keef
Keef•3y ago
Generally queries are idempotent while mutations don't have to be and thats the pattern react query encourages
cje
cje•3y ago
at the end of the day everything is just functions but you'll really have to bend react query to not shoot yourself in the foot by doing this and it will make anyone who reads your code hate you
amanuel
amanuel•3y ago
Why are you doing this server side? I don't understand what you mean On the server you define .query() or .mutate() yes, but you actually have to .useQuery() or .useMutate() on the client
Lopen
LopenOP•3y ago
Lopen
LopenOP•3y ago
or this
amanuel
amanuel•3y ago
Yeah but you're calling this from the client side, aren't you? You have to use .useQuery() on the client side Or .useMutation() And then do .mutate()
cje
cje•3y ago
can i ask why you want to mutate in a query
Lopen
LopenOP•3y ago
my question was based on server not client
cje
cje•3y ago
why not just do it in a mutation
amanuel
amanuel•3y ago
Anything you change in the server will affect the client though?
Lopen
LopenOP•3y ago
nothing really just that i get mixed up sometimes just wanted to know it has any issues if its done that way
amanuel
amanuel•3y ago
This is what I'm trying to explain though? Hahahaha
cje
cje•3y ago
the issue is that queries by default run on every page load or even more frequently than that so you're putting shit in your database all the time when you don't want to mutations only run when you mutate them
amanuel
amanuel•3y ago
If you do .query() and then .useQuery() on the client side, you'll have a bunch of refetch options enabled by default
Lopen
LopenOP•3y ago
oh i am getting your point but then both usequery() and useMutaion() accepts inputs
amanuel
amanuel•3y ago
Yeah of course That's up to you
cje
cje•3y ago
on the backend side, queries and mutations are just functions that take input and then do stuff but like the point is that you call them with useQuery and useMutation, and those are very different things
Lopen
LopenOP•3y ago
no i didnt ask on this it was just based on the server
amanuel
amanuel•3y ago
Bruh
cje
cje•3y ago
well if you just make a server and then never call the stuff on the server you can just ship an empty file and it's the same
Lopen
LopenOP•3y ago
your still not getting the point😅 but then you can still use .query() .mutation() on same instance
cje
cje•3y ago
yea sure have a nice day
amanuel
amanuel•3y ago
@Lopen32 You asked whether to use .mutation() or .query() in the backend, right?
Lopen
LopenOP•3y ago
yes when i want to write to the database
amanuel
amanuel•3y ago
Ok So where are you calling this tRPC procedure from?
Lopen
LopenOP•3y ago
?? i just wanted to know if using .query() is bad
amanuel
amanuel•3y ago
That's not the point though Where are you calling the function?
Lopen
LopenOP•3y ago
i use .mutation() normally server/trpc/router
amanuel
amanuel•3y ago
Like from your client side? How are you calling it When you use .query() for example?
Lopen
LopenOP•3y ago
trpc.register.signup.useQuery({data: data})
trpc.register.signup.useQuery({data: data})
amanuel
amanuel•3y ago
Okay great! So this is where the difference is my friend! If you use .query you need the .useQuery() hook on the client side And this has a ton of refetch options on by default Right? This thing will automatically try to update the data that comes out of it
Lopen
LopenOP•3y ago
not update it creates
amanuel
amanuel•3y ago
useQuery() will try to refetch the return So in this case you're just returning status and message This is why . query() is bad here because it will trigger by itself and cause side effects Like you said, creating an account or whatever it is On the other hand, if you use the .mutate and . useMutation() hook, you get a mutate function on the client side that you can explicitly call only when you want to create an account
cje
cje•3y ago
do you know when this runs, and how often?
Lopen
LopenOP•3y ago
once oh nice thanks for this explanation
cje
cje•3y ago
are you sure it runs once?
amanuel
amanuel•3y ago
Something like this pseudo code:
const {mutate:createSomething()} = trpc.something.useMutation()
const {mutate:createSomething()} = trpc.something.useMutation()
And then you can call createSomething() explicitly only when you want to do this
cje
cje•3y ago
click outside of the browser and then inside of it again check your network tab
Lopen
LopenOP•3y ago
yes
amanuel
amanuel•3y ago
Lopen, if you can Read what I wrote If it doesn't make sense, search up react query and go to docs Or here
amanuel
amanuel•3y ago
Ctrl+f "refetch" And then look at this
Lopen
LopenOP•3y ago
i usually do it like this
const { mutateAsync } = trpc.register.signup.useMutation();
const { mutateAsync } = trpc.register.signup.useMutation();
amanuel
amanuel•3y ago
I hope that helps! Be careful, mutateAsync has some pitfalls Make sure to understand it's a promise I'd do mutate And if you need to do something when it succeeds For example with the status Use the onSuccess/onError callbacks
Lopen
LopenOP•3y ago
try catch block not enough?
cje
cje•3y ago
please read through the react query docs this is a pointless conversation otherwise
Lopen
LopenOP•3y ago
okay
amanuel
amanuel•3y ago
@cje It's difficult to understand in the beginning, it's okay It took me a while to wrap my head around this @Lopen32 just experiment a little and you'll see the difference, just be careful when you're doing auth Why not use next-auth?
cje
cje•3y ago
Quick Start | TanStack Query Docs
This code snippet very briefly illustrates the 3 core concepts of React Query: Queries
cje
cje•3y ago
then go on to read more about whatever parts you care about
Lopen
LopenOP•3y ago
i use next auth next auth is for sign in not sign up
amanuel
amanuel•3y ago
Oh okay, I didn't know that for Credentials! Why use Credentials?
Lopen
LopenOP•3y ago
formaility
amanuel
amanuel•3y ago
All right! Best of luck! Also, if you're ever iffy on how to use tRPC, you can always just use the api folder of NextJs And then use fetch('POST') on the frontend If anything
Lopen
LopenOP•3y ago
??
amanuel
amanuel•3y ago
What?
Lopen
LopenOP•3y ago
you can always just use the api folder of NextJs
you can always just use the api folder of NextJs
that means i will start writing api ?
amanuel
amanuel•3y ago
API Routes: Introduction | Next.js
Next.js supports API Routes, which allow you to build your API without leaving your Next.js app. Learn how it works here.
amanuel
amanuel•3y ago
You can write endpoints there and just use fetch() regularly
Lopen
LopenOP•3y ago
i have used next api not an option
amanuel
amanuel•3y ago
Oh, why not?
Lopen
LopenOP•3y ago
thats why i chose trpc in the frist place else will use express
amanuel
amanuel•3y ago
tRPC just provides type-safety And it uses react query under the hood I'm saying if react query ever gets complicated you can just use the api folder and have handlers there Why would you use express?
Lopen
LopenOP•3y ago
industry standard and i just dont like graphql
amanuel
amanuel•3y ago
No I'm asking, why do you think you have to use express if you use API routes?
Lopen
LopenOP•3y ago
nothing really just familiar with it
mrnicericee
mrnicericee•3y ago
are you using the create-t3-app?
Lopen
LopenOP•3y ago
Yes
mrnicericee
mrnicericee•3y ago
where are you declaring your queries & mutations?
Lopen
LopenOP•3y ago
server
Want results from more Discord servers?
Add your server