GraphQL vs REST API: A Comprehensive Comparison for Modern Development

Abhishek Lodha
4 min readSep 2, 2023

--

Choosing the right API development approach can have a profound impact on your application’s performance and user experience. In this comprehensive comparison, we’ll explore the GraphQL and REST paradigms, diving into their features, benefits, and drawbacks. By the end, you’ll have a clear understanding of when and why GraphQL or REST shines for your project.

GraphQL

GraphQL is a query language and runtime for APIs that was developed by Facebook. It provides a more efficient, powerful, and flexible alternative to traditional REST APIs for fetching and manipulating data. GraphQL allows clients to request exactly the data they need, reducing over-fetching and under-fetching of data, which can be common issues in REST APIs.

Let’s deep dive into practice

  1. Schema Definition (schema.graphql):
type Query {
bookById(id: ID): Book
books: [Book]
}

type Book {
id: ID
name: String
pageCount: Int
author: String
}

2. Controller (Java):

import graphql.ExecutionResult;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.graphql.data.method.annotation.SchemaMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Map;

@Controller
public class GraphQLController {
@QueryMapping
public Book bookById(@Argument String id) {
return Book.getById(id);
}

@QueryMapping
public List<Book> books() {
return Book.books();
}
}

3. Setting Up GraphQL (Main.java):

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Main {

public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}

Once the app is run, danymically we have choose what fields we need in the reponse without explicitly writing the service for that unlike REST.

books service returning all books with only id attribute
bookById service returning id, name attribute

GraphQL Pros

  1. Efficiency: Clients can request exactly the data they need, avoiding over-fetching and under-fetching.
  2. Flexibility: The schema’s evolving nature makes it easier to add new features without versioning.
  3. Single Endpoint: One endpoint for all operations simplifies API requests.

GraphQL Cons

  1. Complexity: Learning GraphQL and writing queries can be more complex for newcomers.
  2. Security: Poorly written queries can lead to performance issues, and a malicious query can cause denial-of-service.
  3. Caching: GraphQL doesn’t have built-in caching, which can be a drawback for read-heavy applications.

When to Use GraphQL

  1. Complex Data Requirements: GraphQL shines when clients have complex data requirements and need to fetch multiple related resources in a single request.
  2. Agile Development: In agile environments where the API may evolve frequently, GraphQL’s flexibility eliminates the need for versioning.
  3. Mobile Apps: GraphQL is advantageous for mobile apps where bandwidth efficiency is crucial, as clients can request only the data they need.
  4. Third-party Clients: When serving third-party clients, GraphQL allows clients to fetch precisely the data they require, reducing over-fetching.

REST

Representational State Transfer (REST) is an architectural style for designing networked applications. RESTful APIs use standard HTTP methods like GET, POST, PUT, DELETE, and follow principles such as statelessness and resource-based routing.

REST API Example

Let’s check out similar example as writen written above for GraphQL

Below will always return all the attributes of Book, rather than the one we want.

import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/books")
public class RestController {
private List<Book> books = new ArrayList<>();

@GetMapping
public List<Book> getAllBooks() {
return books;
}

@GetMapping("/{id}")
public Book getBookById(@PathVariable String id) {
// Search for the book by ID and return it
return Book.getById(id);
}
}

REST API Pros

  1. Simplicity: REST APIs are easy to understand and use. They follow a straightforward structure and rely on HTTP methods.
  2. Caching: REST leverages HTTP caching mechanisms, making it efficient for read-heavy applications.
  3. Maturity: REST has been around for a long time and is well-established.

REST API Cons

  1. Over-fetching and Under-fetching: Clients often receive more or less data than needed, leading to inefficiency.
  2. Versioning: Changes to the API often require versioning to avoid breaking existing clients.

One View Comparision

One view Comparision

Conclusion

In the GraphQL vs. REST debate, there’s no one-size-fits-all answer. Your choice depends on the specific requirements of your project. REST offers simplicity and maturity, while GraphQL provides flexibility and efficiency in handling complex data requests.

In practice, many applications use a hybrid approach, where REST serves as the foundation, and GraphQL is introduced for specific use cases where its advantages are most beneficial. The decision ultimately boils down to your project’s needs, your team’s familiarity with the technologies, and your long-term goals.

Checkout git repo

Please share your experience or feedback in the comments.

Keep Learning !

--

--

No responses yet