GraphQL vs REST API | Similarities & Differences

David Mosyan
5 min readMay 26, 2024

--

Overview

Both GraphQL and REST are powerful technologies behind most of our modern applications. Both are meant to exchange data over the internet. REST enables client applications to exchange data with a server using HTTP verbs (GET, POST, etc.), which is the standard communication protocol of the internet. On the other hand, GraphQL is an API query language that defines specifications of how a client application should request data from a remote server.

GraphQL emerged in 2012 as a response to the need for speed in emerging social media platforms. Developers found that existing API architectures, like REST, were too lengthy and structured to produce news feeds efficiently.

Similarities

Both GraphQL and REST are popular API architecture styles that enable the exchange of data between different services or applications in a client-server model.

APIs facilitate the access of data and data operations like this:

  1. A client sends an API request to an endpoint or multiple endpoints on a server.
  2. The server gives a response that contains data, data status, or error codes.

REST and GraphQL allow you to create, modify, update, and delete data on a separate application, service, or module via API. APIs developed with REST are known as RESTful APIs or REST APIs. Those developed with GraphQL are simply GraphQL APIs.

Both REST and GraphQL implement several common API architectural principles. For example, here are principles they share:

  • Both are stateless, so the server does not save response history between requests.
  • Both use a client-server model, so requests from a single client result in replies from a single server.
  • Both are HTTP-based, as HTTP is the underlying communication protocol.
  • REST and GraphQL both design their data interchange around resources. A resource refers to any data or object that the client can access and manipulate through the API. Each resource has its own unique identifier (URI) and a set of operations (HTTP methods) that the client can perform on it.
  • Both REST and GraphQL support similar data formats. JSON is the most popular data exchange format that all languages, platforms, and systems understand. The server returns JSON data to the client. Other data formats are available but less commonly used, including XML and HTML.
  • REST and GraphQL both support caching. So, clients and servers can cache frequently accessed data to increase speed of communication.
  • Both work with any database structure and any programming language, both client-side and server-side. This makes them highly interoperable with any application.

GraphQL solves some REST limitations

REST APIs always return a whole dataset. For example, from a person object in the REST API, you would receive the person’s name, date of birth, address, and phone number. You would get all of this data even if you only needed a phone number.

Similarly, if you wanted to know a person’s phone number and last purchase, you would need multiple REST API requests. The URL /person would return the phone number and the URL /purchase would return purchase history.

Social media developers had to write a lot of code just to process API requests, which affected performance and user experience.

GraphQL emerged as a query-based solution. Queries can return the exact data in only one API request and response exchange.

Differences

  • A REST API is an architectural concept for application communication. On the other hand, GraphQL is a specification, an API query language, and a set of tools. GraphQL operates over a single endpoint using HTTP.
  • REST request uses HTTP verbs that determine the action, a URL that identifies the resource on which to action the HTTP verb and parameters & values to parse, if you want to create or modify an object within an existing server-side resource to work.
  • In contrast, GraphQL uses query for getting read-only data, mutation for modifying data and subscription to receive event-based or streaming data updates.
  • GraphQL uses a server-side schema to define data and data services, which differs from a REST API. On the other hand, REST APIs do not require a server-side schema. But you can define it optionally for efficient API design, documentation, and client development.
  • REST APIs often include versioning in the URL to solve this issue, like https://example.com/api/v1/person/12341. However, versioning is not mandatory, and it can lead to errors. GraphQL requires API backward compatibility. So deleted fields return an error message, or those with a deprecated tag return a warning.
  • GraphQL is a strongly typed API architecture, which means that it requires a detailed description of the data, its structure, and data operations in the schema. Due to the level of detail in the schema, the system can automatically identify request errors and provide useful error messages. REST APIs are weakly typed, and you must build error handling into the surrounding code. For example, if a PUT request parses a number value as text instead of as an integer, the system does not identify the error automatically.

When to use GraphQL & REST

GraphQL is likely a better choice if you have these considerations:

  • You have limited bandwidth, and you want to minimize the number of requests and responses
  • You have multiple data sources, and you want to combine them at one endpoint
  • You have client requests that vary significantly, and you expect very different responses

On the other hand, REST is likely a better choice if you have these considerations:

  • You have smaller applications with less complex data
  • You have data and operations that all clients use similarly
  • You have no requirements for complex data querying

It’s also possible to build a single application with both GraphQL APIs and REST APIs for different areas of functionality.

Summary

--

--