4 ms·
Imagine a To-Do app, and you want to implement a new feature "mark all tasks as completed". With a RESTful API you do these requests: GET https://api.tod
by brillout 4y ago
Imagine a To-Do app, and you want to implement a new feature "mark all tasks as completed".
With a RESTful API you do these requests:
GET https://api.todo.com/task?completed=false
POST https://api.todo.com/task/42 { "completed": true }
POST https://api.todo.com/task/1337 { "completed": true }
POST https://api.todo.com/task/7 { "completed": true }
With RPC/Telefunc, this is what you do instead:
export async function onMarkAllAsCompleted() {
await sql('UPDATE tasks SET completed = true WHERE completed = false')
}
The crux the matter is that a RESTFull/GraphQL API is a set of generic endpoints that are set-in-stone and that are designed to be able to serve all kinds of frontends, whereas with RPC you implement endpoints as-you-go that are tailored to your frontend.
For example, if you want to provide third-parties access to your data, then RPC wouldn't make sense. You need an API for that.
On the other hand, if all you need is your frontend to access your database, then an API really is just an unnecessary indirection. (Although for large teams an API can be useful to decouple the frontend from the backend.)
- madsbuch 4y agoThere is not reason why your couldn't expose an endpoint that lets you marked completed a filtered query. You can implement arbitrary DSLs over REST / GraphQL. Philosofically, there is no difference though each have their own connotations. You expect RPCs to be much more integrated in a language and an API to run over standard transport protocols using standard data formats. However, this is isomorph.
- wruza 4y agoWith RPC/Telefunc, this is what you do instead: get_tasks({completed:false}) add_task({id, …}) update_task({id, completed}) upsert_task({id, …}) delete_task(id) Or a meaningful subset of it, also set in stone when appropriate. There is no reason to start being unreasonable with your APIs once you switch underlying protocols. And no protocol will stop you from doing POST /sqls <some sql> Also, pedantically, your POSTs are wrong. You have to PUT (or likely PATCH in this case) into /task/:id, or POST into /tasks. https://restfulapi.net/rest-put-vs-post/ https://restfulapi.net/rest-put-vs-post/