DEV Community

Cover image for REST API vs GraphQL : The Battle for API Supremacy 🥷
Ky-Ar
Ky-Ar

Posted on

REST API vs GraphQL : The Battle for API Supremacy 🥷

REST (Representational State Transfer) APIs and GraphQL are two different approaches to building APIs (Application Programming Interfaces) that allow clients to request data from a server.

REST APIs

REST (Representational State Transfer) APIs are a standardized way to request and retrieve data from a server using HTTP methods, such as GET, POST,PUT, and DELETE. REST APIs are designed to be stateless, meaning that the server does not store any information about the client's session or state.

To use a REST API, a client sends an HTTP request to a specific endpoint (a URL) on the server, along with any necessary parameters or data. The server then processes the request and sends back an HTTP response, which may include the requested data or an error message.

Lets consider a simple REST API for a blog website that allows clients to retrieve a list of blog posts, as well as individual blog posts. The API might have the following endpoints:

GET /posts: Retrieve a list of all blog posts.
GET /posts/:id: Retrieve a specific blog post by its ID.

To retrieve a list of all blog posts, the client would send a GET request to the /postsendpoint. The server would then respond with a list of all blog posts in the form of a JSON object. To retrieve a specific blog post, the client would send a GET request to the /posts/:id endpoint, where :id is the ID of the specific blog post.

Here is an example of a simple REST API that allows a client to retrieve a list of users from a server:

Endpoint: GET /users

Request:

GET /users HTTP/1.1
Host: example.com
Accept: application/json
Enter fullscreen mode Exit fullscreen mode

Response:

HTTP/1.1 200 OK
Content-Type: application/json

[
  { "id": 1, "name": "Alice" },
  { "id": 2, "name": "Bob" },
  { "id": 3, "name": "Charlie" }
]
Enter fullscreen mode Exit fullscreen mode

In this example, the client sends a GET request to the /users endpoint on the server, and the server responds with a list of user objects in JSON format. The server also includes an HTTP status code of 200, indicating that the request was successful.

  1. REST APIs are often used to create web services, which are APIs that are accessible over the internet and can be used by a wide range of clients, such as web browsers, mobile apps, and other servers. REST APIs are also commonly used to integrate different systems and applications, allowing them to exchange data and perform actions on each other's behalf.

  2. REST APIs typically use HTTP status codes to indicate the success or failure of a request, and may also use HTTP headers and response codes to provide additional information about the request and the response.

GraphQL

GraphQL is a query language for APIs that was developed by Facebook. It allows clients to request specific data from the server, rather than receiving a fixed set of data from a specific endpoint. This allows clients to request exactly the data they need, and nothing more.

To use a GraphQL API, a client sends a GraphQL query to the server, which specifies the data that the client wants to retrieve. The server then processes the query and returns the requested data to the client in the form of a JSON object.

One of the key benefits of GraphQL is its flexibility. Unlike REST APIs, which have a fixed set of endpoints for each type of request, GraphQL allows the client to request any data that is available on the server, as long as the server's GraphQL schema defines it. This means that the client can request exactly the data it needs, and can easily change the data it retrieves by modifying the GraphQL query.

For example, consider the same blog API as above, but this time implemented using GraphQL. The client could send a GraphQL query to the server that looks like this:
Request:

{
  users {
    id
    name
  }
}
Enter fullscreen mode Exit fullscreen mode

Response:

{
  "data": {
    "users": [
      { "id": 1, "name": "Alice" },
      { "id": 2, "name": "Bob" },
      { "id": 3, "name": "Charlie" }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

This query would retrieve a list of users, including their IDs, name. The client can also request specific data for a single user by including an id field in the query:

{
  users(id : 007) {
    id
    name
  }
}
Enter fullscreen mode Exit fullscreen mode

This query would retrieve a single user with the ID "123", including its ID, Name.

GraphQL also includes support for mutations, which allow clients to send data to the server and modify server-side resources. For example, a client might use a GraphQL mutation to create a new user or update an existing user's data.

Overall, GraphQL is a powerful tool for building APIs that allow clients to request exactly the data they need, and is particularly well-suited for modern, data-driven applications.

In summary, REST APIs are a standardized way to request and retrieve data from a server using specific endpoints, while GraphQL allows clients to request specific data from the server using a flexible query language query language.

Top comments (0)