Michał Tyszkiewicz
7/20/2020
If you’ve read an article or even some brief description of GraphQL, it probably included a line about what it does better than REST and other APIs. Glancing over the question like that isn't really fair though is it? You won't get the whole picture without really looking at each API’s uses and advantages so let’s try to dig a bit into that here.
GraphQL is a flexible system for requesting data that focuses on pinpoint requests and delivering exactly what is required. What sets GraphQL apart from other APIs is its unique approach, it’s client-driven. Instead of handling it as usual i.e. server to a client, here the client decides how everything is handled. The main benefits are:
The most well-known example of GraphQL application is probably GitHub. They switched to it in 2016 citing scalability and flexibility as the two major reasons. REST simply wasn't cutting it for them, because it often required multiple requests to get the data you wanted and also over-fetched data on each of those requests. With GitHub’s rapid growth and user base in the tens of millions, you can imagine how big a concern that had to be. GraphQL provided exactly the alternative they needed because it’s focused on the client being able to request particular data in a specific format for a specific use.
After voicing some complaints about it it’s only fair to take a look at REST. Even though some question if it’s not outdated it’s still the most commonly used API. REST is a stateless architecture for data transfer. RESTful systems put an emphasis on statelessness and hypermedia. Unlike GraphQL here the concerns of client and server are separated. The usually mentioned advantages are:
As mentioned above, REST is still the most popular API. Twitter, Facebook, Google, and Paypal are all using RESTful APIs. REST can tie together a variety of resources that can be requested in a number of ways and in different formats for various purposes. If you need a more general-purpose API than the pinpoint GraphQL this is the one for you.
gRPC is a lightweight and quick system for requesting data, it’s a fresh take on an old method called Remote Procedure Call or RPC. The main difference here is how it defines it’s contract negotiations. REST defines interactions through standardized terms in its requests, GraphQL runs requests against a created schema to fetch exactly what is required. RPC functions on contracts, the negotiation is defined by the relationship between the server and the client not by the architecture. Most of the power relies on the client-side while handling and computations are offloaded to a remote server hosting the resource. To put it short the key advantages are:
In short, gRPC is suited for systems that need a set amount of data or processing routinely and in which the requester is low power or wants to preserve resources. The best examples are immensely popular IoT devices like voice controllers, smart light switches, smoke alarms locks, and cameras.
As you can see each of these options has its own applications and advantages. There is no clear winner here and what you should use, or rather what you want to use, depends mostly on your aims and approach. So now that you hopefully know a bit about each of them, look into what would best fit your project and pick the right one for yourself.