tRPC and querying data from external api
I am trying to set up the following this.
User writes a text in the form. He presses the button, the button puts input into tRPC and does a query(or maybe a mutation?) to send a completion request to OpenAI API and returns a result from there. I take this result and display it in the new text format bellow the button.
Right now I know I have realised it incorrectly as it is not working, but I have no idea how to solve this or what is the best practice. Maybe I shouldn't be using tRPC at all.
27 Replies
Is there any specific reason why you pre-fetch the mutation before the user even clicks the button?
I'd expect a request to be send when the button is presed
That is exactly what I want to do, but instead I get this error when I try to put my fetching on the button press.
It seems extremely inefficient to send a request on every character typed by the user. But whatever, that is up to you. What is setUpdated? That line of code doesnt seem to be included in the snippets you sent
I agree, I am just not sure how to do it otherwise, I want the request to be sent only on the button press if I can make it work
Dude, you just sent the same code twice
In what component is this?
Inside GptButtonText
Allright.so im not sure why you're getting that error, since all hooks in fact are in the component. What are you trying to achieve here?
This just seems like a redundant mirroring of states?
Also, it is super confusing that you are making changes in the code, while sending it as if nothing has happened
Here's how I would re-write the component
With some reservation for errors as I'm not 100% sure of your intentions, nor did I set up a repo for this I just kinda free balled it in my text editor
@Vincent Udén I am trying to make this work, why did you choose to do mutation here?
This is how my tpc router file looks like, maybe you can give me insight?
Unlike queries, mutations are typically used to create/update/delete data or perform server side-effects. For this purpose, React Query exports a useMutation hook.
From React query docs: https://react-query-v3.tanstack.com/guides/mutations
Calling an api for chat gpt is a server side effect
Aha... Thanks, let me try that
No worries
Okay, so I still have an issue with this, havenât solved it yet, still says that input is in the wrong format
I have no idea where to get help anymore, trpc doesnât have a documentation to the TRPCMutationsOptions types
Ill take a look in a few hours
Thanks appreciate it!
I have a very-very similar problem. I am also trying to communicate with openAI API. I have the following code that gives me the type error that I have no idea how to solve.
This is the gpt.ts
I also have a component that is as following:
And I get the following error
And actually if I don't do the setGeneratedText I still get an error about the type not matching
@Simvolick you are passing your arguments to useMutation, but you want to pass your arguments to useMutation().mutate(). useMutation() constructs your mutator, but to actually run the mutation you need to call .mutate(). so in your handleClick, change to api.gpt.gptMutate.useMutation().mutate({text: message})
it's a little counterintuitive that you pass args directly to useQuery but not useMutation, but that's just the way it is
đŠ I am sad to know that it was that easy to fix, but is there a way to fix this then
You don't put the useMutation hook in your event handler. The useMutation hook is used on the top level of the component. Then you can call
mutate({...}=
on it whenever you need the mutationRules of Hooks â React
A JavaScript library for building user interfaces
is it possible to put openAI sdk to trpc ctx similiar to prisma?
Like Vincent said, use âuseMutation()â at the beginning of the component like you do with useState. Youâll deconstruct the mutate function and then use it in your event handler like this
Is how i would do it
Because mutate doesnât return your value, it uses callbacks like onSettled
@Simvolick any luck with this?
I got it working finally, the main reason was that I was trying to render an advanced object and not the jsx component.
Super thankfull actually to you and everyone who helped, really took my mind of worrying about this problem
Awesome! Closing the thread đ
Can you elaborate what you really want inside of ctx there?
Hey! You are really the boss to answer this question, I have been coming back to this 5 times now to solve issues in my code! Thanks!
đ glad to be of help!