PUT request with tRPC

Hi everyone! First of all, I tried to find an answer to my question but I couldn’t find one. I’m trying to update an entity in my database using tRPC stack. For example, I want to edit some of my posts on my website and update their title. Everything works fine but instead of a PUT request, I see a POST request in my network tab. How can I tell to my tRPC API to send a PUT request instead of a POST request? Much appreciated!! Thank you in advance
24 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
rre
rre3y ago
what if you do a query. does it do a POST as well? out of curiosity i am quite clueless when it comes to tRPC but it seems that it's the same as in graphQL, queries and mutations. at least in graphQL you only have POST
pogadev
pogadevOP3y ago
Let’s assume that you have a blog and you want to create a post. For that, you’ll have a form on the client side and some inputs for the post title and post body. On submit, you send a POST request, using the useMutation hook. This POST request creates a new resource in your database. Later that day, you want to edit the post title and again, from the client side, you have a form from which you can edit the title of the post. But for that, you’ll want to send a PUT request in order to update an existing resource. (That blog post that you already have in DB) The correct way should be to send a PUT request when you create mutations for already existing entities
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
rre
rre3y ago
oh okay thanks, but yeah. it seems as in creating and updating is both POST - because they are mutations.
nexxel
nexxel3y ago
you're using trpc so you don't need to think about http methods. just do a mutation to update stuff
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
pogadev
pogadevOP3y ago
@flicknote the thing is that the browser is using a POST request for that 🙂
rre
rre3y ago
for mutations it does a post request
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
rre
rre3y ago
even though you're doing a mutation to update an existing entity, it will do a post in the browser this functionality seems identical to GraphQL
pogadev
pogadevOP3y ago
here is the code
pogadev
pogadevOP3y ago
yeah, but I don't like that 😆
rre
rre3y ago
why does it matter? if you ever hop on to graphql, it's the same there
pogadev
pogadevOP3y ago
it's not technically correct :))
rre
rre3y ago
i wouldnt spend time thinking about it, seems trivial
pogadev
pogadevOP3y ago
yeah, I get it! It's just that it's the first time I'm using tRPC and I've noticed this situation. Everything works fine, I was just curios if I'm missing something 🙂 Thanks for the answers, I am happy living with it just like you said @rre
Piotrek
Piotrek3y ago
Just a quick note: If I'm not mistaken you can use https://github.com/jlalmes/trpc-openapi to add meta object to your route and pass PUT method in there
GitHub
GitHub - jlalmes/trpc-openapi: OpenAPI support for tRPC 🧩
OpenAPI support for tRPC 🧩. Contribute to jlalmes/trpc-openapi development by creating an account on GitHub.
Piotrek
Piotrek3y ago
It's more useful if you want to have public api tho
pogadev
pogadevOP3y ago
interesting! I'll take a look at it! Thanks @Piotrek
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
_anno
_anno3y ago
Yep, @PogaDev RPC and REST are two different architectural patterns. In Rest you use the HTTP Methods to describe the business and domains. GET means fetching Data, POST most of the time creating, PUT updating a dataset. RPC on the other hand is describing your domain with methods ("procedures"). Loading data means sending a request to call "loadData", updating data is sending a request to call "updateData" To enable this tRPC uses the HTTP Protocol/ Methods aswell to let you execute Code on the server You would say that a tRPC interface looks like a REST API, but in reality it is just a HTTP API (because we call HTTP APIs most of the time an REST API even when it does not follow the architectural patterns what a REST API really means Tldr: REST and tRPC are describing two different approaches to provide an API using HTTP Endpoints so a PUT would be correct for a REST API but is not necessary for tRPC
pogadev
pogadevOP3y ago
thanks a lot 😄 oh, got it! thank you for your response
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server