Technical Community

GraphQL Vs REST. Which one is better?

August 19 ,2019

1. The introduction of API

As everyone knows, a website is created by both front-end and back-end elements. While users interact with front-end components such as using a keyboard to type something or accessing a site with just a click of a mouse, back-end components have to control the server and the database to satisfy the users’ requirements. In order to maintain the communication between users and a server, we need a “machine translation” named API (Application Programming Interface), which is able to translate “human action/language” and help the server understand what our needs.

As you might concern what API can actually do instead of only translating,

  • API can enhance the security of both server and users because they do not communicate with each other directly (but through API).
  • API can support a lot of figures of HTTP such as URI, request or response headers, caching, versioning, and content format, etc.
  • Besides, API has an open-source thus you can use API through any client supporting XML or JSON.
  • The most common operations are GET (create new data), POST (read data), PUT (Update data) and DELETE (delete data).
  • Finally, API is easy to understand and configure, that is why it has become a standard in modern web development.

2. REST and GraphQL

a. Introduction of REST

REST (REpresentational State Transfer) was one of the very first API introduced in over the world. It was invented and introduced by Roy Thomas Fielding in his Ph.D. thesis in 2000.

Since it was one of the first API ever, REST has quickly become a standard named RESTful API(s). Although REST does not have any strict rules (it is flexible and it depends on how developers want their site’s structure to look like), there are some definitions you should follow.

  • REST works as a client-server model, in which a server combines a range of services listening requests from clients. A request sent from a client needs one or more services to satisfy depends on how complicated the request is.
  • REST is stateless, which means the client or server does not save the state of each other. This could help REST is easy to develop since it has not to update the state memories of clients, however, without state memories, each request sent to the server sometimes needs to be combined with the state memories, thereby broadening the information and data sent to the server.
  • REST has the capability of catching.

When a request comes to REST, before doing anything, REST will look up the request’s information in cache to know whether the information is in cache or not. If there was the same information (that means there had already been the same request sent through REST), REST will response exactly as the response of the previous request. If the sent request is new, REST will communicate with the server to have the information the response to the client as well as add the information of the request to the cache.

  • REST has standardized a lot of services, making the communication between different devices or methods of a lot of kinds of clients with the server easier. However, because the system has been standardized, we barely can optimize it.
  • REST has a lot of layers, which are responsible for their own types of requests, thus REST is easy to broaden each element.

b. Why REST?

  • REST is easy to understand.
  • REST helps the application become simple.
  • Optimize the code.
  • Optimize the system’s resources.
  • REST response data in a lot of types (HTML, XML, RSS, JSON, etc.).

c. Introduction of GraphQL

graphql beginners
What is GraphQL? Image: Contentful.

Since REST is simply an API that transfers requests from clients to servers, GraphQL has been developed based on REST to make all the requests more optimized and straightforward.GraphQL was developed internally by Facebook team in 2012 before being released and becoming an open-source in 2015.

GraphQL is a query language, using any data types provided by developers, thusGraphQL is flexible and easy to back-up.

Unlike REST, which each request is sent alone, GraphQL defines a request with its types and fields describing the request and everything is connected.

For example, if we are looking for a book with providing information on its name, author, and release date, REST has to send three requests while GraphQL only has to send one request combining all the information.

assume that we are looking for a (Book).

Since GraphQL has been developed based on REST, it inherited all the strengths and features of REST.

  • GraphQL has a hierarchical structure.
  • GraphQL can send and receive data immediately because of its optimized structure. Therefore, GraphQL can satisfy strict requirements in real-time (streaming live, video calling, etc.).
  • Because GraphQL combines a lot of information and resources into one request, GraphQL has one end-point while REST has multiple end-points to fetch (over fetching and under fetching – one of the biggest disadvantages of REST).
  • GraphQL has a single schema. GraphQL provides its own type of system with its own syntax call SDL (Schema Definition Language), which defines a contract between the server and the client, telling users how a client can access the data, thus it is easy to look for the relationships between resources. Moreover, with a fully defined schema, front-end teams and back-end teams can work independently yet in a cohesive way.
  • GraphQL also supports JavaScript Frameworks.
  • GraphQL has ease of server and client development.
  • GraphQL works with queries, thus when a request is sent to the server, it will retrieve the GraphQL query.

As a result, we can say that using GraphQL is like using a multiple REST.

d. REST vs GraphQL?

As you can see, GraphQL has a lot of advantages compared to REST.

  • GraphQL’s performance is much better.
  • Data of GraphQL is more optimized while REST sometimes has excess or lack of data.
  • GraphQL is more flexible, faster, and easier to maintain consistency because it is not based on any specific database or storage engine.
  • GraphQL is very easy to understand (even much easier than REST) because it is straightforward and intuitive.

Because of those advantages, GraphQL has become more popular and common, people find it might be a better API. But can GraphQL totally replace REST?

GraphQL is more common nowadays but because it is too flexible, thus REST is more standard and “RESTful”. Working with REST, developers can easily access, modify, and broaden its libraries. Therefore, some companies or enterprises refuse to use GraphQL.

Each request of REST is specific and explicit (to multiple end-points), thus it is easy to find issues and fix them by calling multiple end-points to fetch related resources. On the other hand, requests in GraphQL are combined from a lot of types and information, it is difficult to track a false request.

Also, GraphQL is not cacheable (unlike REST), a request always has to be sent to the server. (However, GraphQL has been developed in terms of caching based on HTTP caching).

Since REST is the older generation of API it has been proven for decades, it has its own maturity and it has many more tools and integrations than GraphQL, thus it has more information on issues or bugs and developers might find it easier than GraphQL to have help from others.

3. Conclusion

REST or RESTful approach is always limited with a single resource. If users need more data and information, they always have to send multiple requests to the server. While GraphQL is much more flexible and GraphQL also combines connected entities within one GraphQL data query.

Both REST and GraphQL have been developed for developers to find it is much easier to modify and optimize the data. It is really difficult to tell which is better because it is based on specific circumstances and developers’ ideas (and maybe in the future, the would be a lot of types of API developed to satisfy our needs). Developers need to understand clearly their desires and the impacts of both REST and GraphQL before choosing either REST or GraphQL.

However, people at Designveloper recommend that choosing between REST and GraphQL is not a good idea. When you can fully understand them, they can co-exist (even with other APIs such as SOAP) to optimize the data perfectly.







Also published on

Share post on