Differences between GraphQL basics and REST APIDifferences between GraphQL basics and REST API

Differences between GraphQL basics and REST API

“REST API” has long been the industry standard as an API design method. However, in recent years, as front-ends have become more sophisticated, an increasing number of companies are adopting GraphQL. Before we get into the technical details, let’s first think about the difference between the two using the analogy of ordering at a restaurant.

Limitations of REST API in Restaurants

REST API is like a “food court” with different counters for each menu. For example, let’s say you want to order a hamburger set. You must first go to the burger counter (/api/burgers/1), then to the potato counter (/api/sides/potato), and finally to the drinks counter (/api/drinks/coke).

This requires the hassle of going back and forth to the counter many times (multiple requests). Furthermore, even if you wish to remove the pickles from your hamburger, the counter will always give you the finished product with pickles in it. This is a problem called “overfetching” where unnecessary data is retrieved.

The ultimate customization called GraphQL

GraphQL, on the other hand, is a “very capable personal waiter.” You hand the waiter an order (query) that says ``hamburger (without pickles), small size fries, and cola’’. The waiter then goes to the kitchen and brings out the items you specified on one tray.

This is the biggest feature of GraphQL. The front end (customer) can specify exactly what data they want and in what form, and the back end will return the exact amount of data accordingly.

GraphQL benefits and implementation barriers

This mechanism provides the following benefits:

  • Reducing communication volume: In mobile environments such as smartphones, not acquiring unnecessary data can dramatically improve performance.
  • Flexible UI changes: Even if you want to add new display items to the screen, there is no need to have the backend create a new endpoint like in REST, and you can do it just by rewriting the front query.

On the other hand, it’s not a magic wand. There are also issues such as difficult to control cache,'' N+1 problem is likely to occur,’’ and ``backend implementation becomes more difficult.’’ For simple blog sites, REST API is often easier and more suitable.

Which one should you choose?

GraphQL can be the best choice for developing complex web applications that display multiple types of resources (user information, posts, comments, likes, etc.) on a single screen. Determine what kind of orders your “restaurant” receives the most and choose the appropriate architecture.