REST as an API standard has evolved over the past few years, and GraphQL has slowly emerged as an architecture of choice among the developers due to its advantages. Before we dive into the debate of GraphQL vs. REST, let’s brush up a bit on our basics.

GraphQL Vs REST — Comparison

An API (Application Programming Interface) is the intermediary that lets two software applications to talk to each other. An API dictates how a client can load data from a server.

GraphQL helps specify precisely what data it needs from an API, which is called declarative data fetching. Now that we have the basics in place let us see how REST and GraphQL came into being.

REST — is History?

REST stands for “Representational State Transfer.” It is a set of rules that determines how an API looks like. One of these rules dictates that you should get a resource (a piece of data) when you link to a specific URL.

The URL is called a request, and the data sent back is called a response.

When you call a RESTful API, the server will transfer the representation of the state of the resource to the client.

For example, if you call the Facebook Profile API to fetch a specific user, REST will transfer the state of that user, including name, profile picture, latest posts, and more.

GraphQL Vs REST — Representational State Transfer — REST

Image credit

To perform an operation in REST, you need to call it in the form of an HTTP request, such as GET, POST, PUT or DELETE. For example, to get the Facebook profile of a particular user, you will need the URL that identifies that user and the HTTP method GET.

Now that you know the basics of REST let’s see how GraphQL came into existence and how it evolved.

GraphQL — Better than the REST?

While RESTful APIs provided a great new way of computers to interact with one another, there were also a few hiccups that made developers look elsewhere.

For starters, RESTful APIs returned more data than what was needed, meaning more API calls. Since every endpoint could return only the specified data, developers had to design endpoints keeping the front-end views in mind.

All this resulted in less flexibility, and Facebook engineers decide to build an alternative to REST. GraphQL was built by Facebook in 2012 and was made open source in 2015.

GraphQL describes how you can communicate with a server and transfer data from a client to a server. It differs from REST fundamentally due to the fact that the client can specifically ask what data it needs.

GraphQL makes it easy to source and aggregate data from multiple sources. Instead of multiple endpoints, you had a “smart” endpoint that can collect complicated queries and present the output in whatever format the client requires.

We will tell it to you with the help of a metaphor. Imagine you have to order food from your cafe, buy groceries, and borrow a book from a library. With REST, you will be making three different phone calls, which are three requests.

GraphQL — Better than the REST?

With GraphQL, these three phone calls can be replaced by a single one, like talking to Siri. All you have to do is tell the address to these places, and then place your order (“fetch me a Cappucino, a loaf of bread, and a copy of today’s Herald.”)

GraphQL — How does it work?

This is a very rudimentary way to look at how GraphQL works, but it serves the purpose. We will now see how GraphQL and REST are similar.

#graphql #web-development #javascript #rest #programming

GraphQL VS REST — Which one to choose when?
1.50 GEEK