This is a sample graphql server built with Typescript + Apollo Server
yarn install + yarn dev
Official Website: https://graphql.org/
One-liner summary: Strongly typed, JSON-only, API handler.
- Strongly typed via schema
- Data-serving API
- Not suitable for web frontend serving
- JSON-only
- No XML or binary
- Binary alternatives:
- Base64
- Link to CDN/REST endpoint for binary data
A GraphQL request is called an operation. There are three main types:
- Read-only (advisory, not enforced)
- Similar to
GET/ SQLSELECT
- Modifies data (like
POST,PUT,DELETE) - Handled by backend resolvers
- Long connections, typically over WebSockets
A GraphQL server:
- Runs on one endpoint, typically via
POST - Can coexist with regular REST endpoints
- Benefits from REST best practices:
- HTTP2, compression, HTTPS, etc.
From linear REST logic:
- Parse β Access DB β Clean β Respond
To graph-oriented design:
- Define resources
- Connect via schema
- Implement resolvers per resource
- GraphQL engine handles resolver orchestration
| Style | Description |
|---|---|
| Schema-First | Define schema manually, then attach resolvers |
| Code-First | Schema generated from code (Recommended) |
π More details
Since GraphQL operates over HTTP, any REST client works.
query {
student(id: "123") {
id
name
contact
}
}curl -X POST -d '{"query":"query{student(id:\"123\"){id name contact}}"}' <your-server-address>const query = `
query {
student(id: "123") {
id
name
contact
}
}
`;
fetch("<your-server-address>", {
method: 'POST',
body: JSON.stringify({ query })
}).then(...);- Postman
- GraphiQL / GraphQL Playground
- Usually comes with Graphql server framework, like Postgres' native IDE
- Apollo DevTools (Browser extension)
| Ecosystem | Notes |
|---|---|
| Relay | From Facebook relay.dev |
| Apollo | Community-driven, easier to use apollographql.com (Recommended) |
| REST | GraphQL | SQL | |
|---|---|---|---|
| Input | Endpoint + Method + Query/body | Fixed endpoint + query string | SQL queries |
| Output | Anything (JSON/XML/etc.) | JSON, typed via schema | Table data, typed |
| Logic | Endpoint handler (function) | Resolver (function) | Limited, mostly querying |
| Strengths | Flexible, universal | Typed, shape-controlled, flexible | Precise querying, minimal response |
- Consistent schema across teams
- Tools like GraphQL Codegen enable type-safe client code
- Server validates:
- Incoming query
- Outgoing response
- Clients request only needed fields
- Reduces over-fetching
-
Self-serve data fetching for frontend:
- Frontend developers can define exactly what data they need via GraphQL queries without waiting for backend developers to create or modify specific REST endpoints.
-
Flexible response shaping:
- Unlike REST, where the response shape is fixed per endpoint, GraphQL allows frontend to shape the response directly in the query. No more back-and-forth requests to βadd a fieldβ or βremove a nested relation.β
-
Avoid multiple backend iterations for UI changes:
- Example:
- Traditional REST approach:
- Backend exposes
/product,/vendor,/orderendpoints. - Frontend builds a dashboard page requiring data from multiple endpoints.
- Backend needs to create a
/dashboardendpoint to combine data for performance optimization. - Any dashboard changes = more backend work.
- Backend exposes
- GraphQL approach:
- Frontend simply writes a single query pulling
product,vendor,orderin one request. - Backend remains unchanged as long as the schema supports the required fields.
- Frontend simply writes a single query pulling
- Traditional REST approach:
- Example:
-
Fewer alignment meetings between teams:
- Reduces typical cycles of:
- Frontend asks for an endpoint.
- Backend implements it.
- Frontend realizes extra fields are needed.
- Backend modifies again.
- With GraphQL, schema evolves independently, and frontend can iterate faster without constant backend adjustments.
- Reduces typical cycles of:
-
Cleaner separation of concerns:
- Backend focuses on business logic and data modeling.
- Frontend focuses on UI and data composition.
- Caching
- Loading/Error states
- Pagination & Infinite Scroll (FetchMore)
- Reactive UI binding (Redux-like experience)
- Automatic cache normalization:
- Shared resources (e.g.,
student(id: "123")) across multiple queries and views are de-duplicated in the cache. - When a resource is updated (e.g., via a
mutation), all queries and UI components referencing the same resource ID will automatically reflect the updated data without requiring manual refresh or additional requests.
- Shared resources (e.g.,
- Reduced redundant network requests:
- GraphQL clients like Apollo avoid unnecessary requests by serving data directly from the cache if itβs already available, reducing server load and improving performance.
- Learning Curve: Like React vs jQuery
- Backend-First Setup: Best with GraphQL backend first
- Measuring Gains: Performance gains arenβt always easy to quantify
GraphQL views resources as a graph:
- Root Operations:
query,mutation,subscription - Intermediate Nodes: Connected resources
- Leaf Nodes: Scalars (strings, numbers, etc.)
This leads to flexible, efficient, and maintainable API design.
GraphQL shines in large applications with complex data needs and multiple frontends. With tools like Apollo, strong typing, and streamlined queries, it reduces redundancy and improves developer efficiency β albeit with an upfront investment in learning and backend setup.