DEV Community

Nguyen Hoang
Nguyen Hoang

Posted on

π†π«πšπ©π‘ππ‹ 𝐯𝐬. 𝐑𝐄𝐒𝐓 π€ππˆ

Let me share a π†π«πšπ©π‘ππ‹ 𝐯𝐬. 𝐑𝐄𝐒𝐓 π€ππˆ guide with you today.
Imagine booking a restaurant online.

𝐖𝐒𝐭𝐑 π‘πžπ¬π­: You send a request for booking options, and the system β€œtalks back” with available rooms.

𝐖𝐒𝐭𝐑 π†π«πšπ©π‘ππ‹: You ask for exactly what you need, maybe just availability and prices and get a tailored response.

Both systems facilitate communication, but how they do it differs, and each has its strengths.

π–π‘πšπ­ π€π«πž 𝐑𝐄𝐒𝐓 π€ππˆπ¬ 𝐚𝐧𝐝 π†π«πšπ©π‘ππ‹?
Think of REST APIs as waiters in a restaurant. You (the client) send an order (request), the API takes it to the kitchen (server), gets your food (data), and brings it back. It’s efficient, but sometimes you get more food than you need or need to make multiple requests for a complete meal.

GraphQL is more like a customized buffet. You tell it exactly what dishes (data) you want, and it serves just those items, all in one trip.

𝐈𝐧 𝐑𝐄𝐒𝐓:
β€’ GET: Asking for the menu (fetching data)
β€’ POST: Ordering food (creating new data)
β€’ PUT: Changing your order (updating data)
β€’ DELETE: Sending back your plate (removing data)
β€’ PATCH: Asking for extra sauce (partially updating data)

𝐈𝐧 π†π«πšπ©π‘ππ‹:

  1. You can ask for any combination of data in a single request, whether it’s just the appetizer or the entire meal (nested data).
  2. You control what the response looks like.

𝐖𝐑𝐲 π€π«πž π“π‘πžπ² 𝐈𝐦𝐩𝐨𝐫𝐭𝐚𝐧𝐭?
REST is simple, using familiar HTTP methods. However, you often receive more data than needed or must make multiple requests.
GraphQL is more flexible, letting you request exactly what you want in one call, minimizing data over-fetching or under-fetching.

Top comments (1)

Collapse
 
timthoi profile image
Nguyen Hoang

Image description