When choosing an API design architecture for a web app for transferring data, you can make a choice between REST and GraphQL. REST has become the industry standard for designing Web APIs.
Thus, according to Postman’s 2020 State of the API Report 93.4% of respondents were most familiar with REST. By contrast, GraphQL is a modern revolutionary alternative for developing an API. In this article, we will look at REST and GraphQL, consider their principles and weigh the pros and cons of both technologies.
Let’s start with the example. We consider an API for the library. The API provides information about authors and their books. Our application fetches the name of the author, and titles of all their books by author’s id.
With REST we send two HTTP requests with empty bodies and receive fixed data structures:
Request: GET /author/7
Response: 200 OK
{
"id": 7,
"firstName": "Joshua",
"lastName": "Bloch",
"birthday": "08/28/1961"
}
Request: GET /author/7/books
Response: 200 OK
[
{
"id": 1008,
"title": "Effective Java, 3rd Edition",
"pages": 416,
"lang": "en",
"isbn": "9780134686097"
},
{
"id": 1010,
"title": "Java Puzzlers: Traps, Pitfalls, and Corner Cases",
"pages": 312,
"lang": "en",
"isbn": "032133678X"
},
…
}
If an author with a specified id does not exist:
Response: 404 Not Found
With GraphQL we send a single request and define in its body what data needs to be transmitted. The request is written in a special query language:
Request: POST /graphql
query {
author(id: 7) {
firstName
lastName
books {
title
}
}
}
Response: 200 OK
{
"data": {
"Author": {
"firstName": "Joshua",
"lastName": "Bloch"
},
"books": [
{
"title": "Effective Java, 3rd Edition"
},
{
"title": "Java Puzzlers: Traps, Pitfalls, and Corner Cases"
}
]
}
}
If an author with a specified id does not exist:
Response: 200 OK
{
"errors": [
{
"message": "Author with id=7 does not exist"
}
]
}
REST (Representational State Transfer) is an architectural concept for web services that interact over HTTP protocol. The REST principles were introduced in 2000 by Roy Fielding in his Ph.D. dissertation. These principles are listed below.
An architecture of API should correspond to the client-server model. The client and server distribute their concerns. The server proceeds and stores data while the client requests resources and services. Also, the client and server don’t know the implementation details of each other. A decoupled architecture allows to use of the same server across multiple clients and helps to reduce software complexity.
The server doesn’t store a client state. So, requests from the client should contain all needed information to perform operations and requests are independent.
Servers, clients, and intermediaries can cache response data, as cacheability is one of the standard HTTP protocol features. Caching decreases interactions between system components and improves performance.
There are four constraints for the uniform interface:
The client is not aware of the intermediate components like a proxy, load balancer, or server that implement security policies. All intermediate layers should be invisible to the client.
The server can send an executable code from the client when requested, extending client functionality.
GraphQL is a query language for APIs (QL means query language). GraphQL was started developing in 2012 as an internal project of Facebook, became open-source in 2015 and the first stable version was released in 2018. The key feature of the language is that the client decides what data is needed to obtain from the server.
Besides, Netflix or Coursera were working on REST alternatives for improving API interactions. Netflix has presented Falcor. However, Coursera stopped developing its own solution after Facebook open-sourced GraphQL.
Today GraphQL is the winner of this competition.
One of the most major GraphQL features is that it allows clients to query data from multiple resources in the same request. All data are fetched from a single URI.
GraphQL uses its own type system that is described by GraphQL Schema Definition Language (SDL). GraphQL SDL became part of the specification in 2018. The type system is expressive and supports polymorphism using interfaces. A schema defines a graph structure of data, which means that objects are connected hierarchically.
GraphQL is great for applications where related and nested data are fetched because you can use the full power of its query language.GraphQL is also well suited for mobile development as bandwidth usage is optimized by avoiding over-fetching and under-fetching. But after weighing the pros and cons, we see that GraphQL is not always the best option. If you need a clear and convenient API without overhead, or you want to use HTTP capabilities like caching or authentication, you should choose REST.