1. Introduction
In modern web and mobile applications, APIs play a crucial role in connecting front-end and back-end systems.
Traditionally, REST (Representational State Transfer) has been the go-to approach for building APIs. It’s simple, widely supported, and easy to understand.
However, with the growing complexity of modern applications and dynamic frontends like Angular and React, REST often causes problems like over-fetching, under-fetching, and multiple round trips to fetch related data.
To solve this, GraphQL, developed by Facebook, emerged as a flexible and efficient alternative that allows clients to request exactly what they need and nothing more.
In this article, we will explore the core differences between REST and GraphQL, understand their technical architecture in ASP.NET Core, and learn how to implement each step by step.
2. What is REST API?
REST is an architectural style that uses HTTP methods like GET, POST, PUT, DELETE to manage resources.
Each resource (like user, product, or order) is exposed through a unique URL.
Example REST endpoints
GET /api/products
GET /api/products/10
POST /api/products
PUT /api/products/10
DELETE /api/products/10
Advantages
Simple and widely supported
Easy to cache responses
Works with any client (browser, mobile, IoT)
Limitations
Over-fetching: Client gets more data than needed
Under-fetching: Client must call multiple endpoints for related data
API versioning required for changes
3. What is GraphQL?
GraphQL is a query language for APIs and a runtime for executing those queries.
It allows clients to define the exact shape and structure of the data they need from the server.
Example GraphQL query
{
product(id: 10) {
name
price
category {
name
}}}This single query can replace multiple REST API calls.
The client gets only the requested fields (name, price, category.name) — no extra data.
Advantages
Fetch only what you need
No versioning required
Strong typing via schema
Great for complex UIs (like dashboards)
Limitations
Harder caching and monitoring
More complex to implement initially
Requires a schema definition and resolver logic
4. Technical Workflow (Flowchart)
+-----------------------------+
| Client App (UI) |
+---------------+-------------+
|
REST | GraphQL
|
+----------------+ | +-------------------+
| HTTP Endpoint | | | /graphql Endpoint |
+-------+--------+ | +----------+--------+
| | |
+------v------+ | +------v------+
| Controller | | | Resolver |
+------+------| | +------+------+
| | |
+------v------+ | +------v------+
| Service | | | Resolver |
+------+-------+ | +------+-------+
| | |
+------v------+ | +------v------+
| Database | | | Database |
+--------------+ | +-------------+In REST, each endpoint corresponds to a resource (controller-action pair).
In GraphQL, there’s usually one endpoint (/graphql) that handles all types of queries and mutations via resolvers.
5. Implementing REST in ASP.NET Core
Let’s build a simple REST API using ASP.NET Core 8/9.
// Models/Product.cspublic class Product {
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
}
// Controllers/ProductController.cs
[ApiController]
[Route("api/[controller]")]
public class ProductController : ControllerBase
{
private static List<Product> Products = new List<Product> {
new Product { Id = 1, Name = "Laptop", Price = 80000, Category = "Electronics" },
new Product { Id = 2, Name = "Shoes", Price = 2500, Category = "Fashion" }
};
[HttpGet("{id}")]
public IActionResult Get(int id)
{
var product = Products.FirstOrDefault(p => p.Id == id);
if (product == null) return NotFound();
return Ok(product);
}
[HttpGet]
public IActionResult GetAll() => Ok(Products);
}
API Response (GET /api/products/1)
{"id": 1,"name": "Laptop","price": 80000,"category": "Electronics"}6. Implementing GraphQL in ASP.NET Core
We’ll use the HotChocolate GraphQL library for ASP.NET Core.
Step 1: Install Packages
dotnet add package HotChocolate.AspNetCore
dotnet add package HotChocolate.Data
dotnet add package HotChocolate.EntityFramework
Step 2: Define Schema (Type + Query)
// Models/Product.cspublic class Product {
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
}
// GraphQL/Query.cspublic class Query
{
public List<Product> GetProducts() => new List<Product> {
new Product { Id = 1, Name = "Laptop", Price = 80000, Category = "Electronics" },
new Product { Id = 2, Name = "Shoes", Price = 2500, Category = "Fashion" }
};
public Product GetProductById(int id) => GetProducts().FirstOrDefault(p => p.Id == id);
}

Join the conversation! Your thoughts help the community grow.