Skip to content

Rest_graphql


REST (Representational State Transfer) is an architectural style that uses HTTP methods and standard status codes for communication.

REST API Structure
==================
GET /users/123
GET /users/123/orders
POST /users
PUT /users/123
Response:
{
"id": 123,
"name": "John",
"email": "john@example.com"
}
CharacteristicDescription
ResourcesNouns (users, orders)
HTTP MethodsGET, POST, PUT, DELETE
StatelessEach request independent
Standard Status200, 201, 404, 500

GraphQL is a query language for APIs that allows clients to request exactly the data they need.

GraphQL Query
=============
Query:
------
query {
user(id: "123") {
name
email
orders {
id
total
}
}
}
Response:
---------
{
"data": {
"user": {
"name": "John",
"email": "john@example.com",
"orders": [
{ "id": "1", "total": 100 },
{ "id": "2", "total": 50 }
]
}
}
}
CharacteristicDescription
Query LanguageClient specifies fields
Single Endpoint/graphql
Typed SchemaStrongly typed
Client-drivenRequest what you need

AspectRESTGraphQL
Data fetchingMultiple endpointsSingle request
Over-fetchingGets extra dataExact data needed
Under-fetchingNeeds multiple callsOne request
CachingHTTP cachingCustom caching
Learning curveLowerHigher
ToolingMatureGrowing

REST: Fetching User with Orders
=================================
Request 1: GET /users/123
Response: { id, name, email }
Request 2: GET /users/123/orders
Response: [ { id, total }, ... ]
Total: 2 requests
Problem: What if you need nested data?
Solution: Create custom endpoint /users/123/with-orders
But then: Over-fetching if you just need user
GraphQL: Single Request
======================
query {
user(id: "123") {
name
orders {
total
}
}
}
Single request!
GraphQL decides what data to fetch based on query
Single backend query covers everything

ScenarioWhy
Simple resourcesCRUD operations
Public APIsStandard, well understood
Caching importantHTTP caching works well
MicroservicesClear boundaries
Team experienceMore familiar

ScenarioWhy
Mobile appsBandwidth efficient
Complex dataMultiple nested resources
Rapid iterationChange query without backend
Multiple clientsWeb, mobile, TV
AggregationsCombine data from sources

RESTGraphQL
AmazonFacebook
eBayGitHub
StripeShopify
TwitterAirbnb

Hybrid Architecture
===================
+--------------------------------------------------+
| API Gateway |
+--------------------------------------------------+
| |
v v
+-----------+ +-----------+
| REST | | GraphQL |
| API | | API |
+-----------+ +-----------+
Use REST for:
- Simple CRUD
- Public APIs
- High cacheability
Use GraphQL for:
- Complex queries
- Aggregations
- Client-specific needs

Key comparison points:

  1. REST - Multiple endpoints, predictable, HTTP caching
  2. GraphQL - Flexible queries, single endpoint, exact data
  3. REST better for - Simple, public, cached APIs
  4. GraphQL better for - Complex data, mobile, rapid dev
  5. Can use both - Different use cases, same backend

Next: Chapter 18: Authentication & Authorization