1 What Is GraphQL? Core Concepts and Benefits
What Is GraphQL? Core Concepts and Benefits
In recent years, GraphQL has become one of the most exciting API technologies, especially among web and mobile developers. If you have been working in backend development for a while, you are surely familiar with REST APIs as the standard way for clients and servers to communicate. GraphQL, however, offers a new approach that is considered more flexible and efficient. So what exactly is GraphQL? What are its advantages and the core concepts behind it? Let’s explore it in depth, complete with code examples and flow diagrams to make the concepts even clearer.
What Is GraphQL?
GraphQL is a query language for APIs, as well as a runtime for executing those queries against your existing data. GraphQL was created by Facebook in 2012 and officially open-sourced in 2015. Unlike REST, which requires the server to expose various endpoints for each resource, GraphQL uses only a single endpoint. Through that one endpoint, the client can request exactly the data it needs, no more and no less.
A Simple Definition
Core Concepts of GraphQL
To build a solid understanding, here are the key concepts in GraphQL:
Strongly Typed Schema
Every GraphQL API defines an explicit schema: what data types exist, how they relate, and which operations can be performed.Query
How the client requests data from the server. The client can specify in great detail exactly which data it needs.Mutation
How the client modifies data on the server, such as create, update, or delete (similar to POST/PUT/DELETE in REST).Subscription
An option to receive real-time updates from the server (a sort of push notification or WebSocket).
Comparison: REST vs GraphQL
| Characteristic | REST | GraphQL |
|---|---|---|
| Endpoint | Many (per resource) | One |
| Returned Data | All fields in the resource | Only the requested fields |
| Over/Underfetching | Happens often | Almost never (more precise and lightweight) |
| Documentation | Must be written manually | Generated automatically from the schema |
| Real-time | Requires a custom implementation | Native support (subscription) |
GraphQL Flow Architecture Diagram
Sometimes a process is easier to grasp through a diagram. Here is the flow of a GraphQL request:
sequenceDiagram
participant Client
participant Server
participant DataSource as Data Source (DB/API)
Client->>Server: Send query/mutation
Server->>Server: Validate query against the schema
Server->>DataSource: Fetch/modify data as needed
DataSource-->>Server: Return data
Server-->>Client: Send the resulting data as requested
With a single endpoint, GraphQL gives the client a high degree of flexibility to define the data and the relationships between resources it needs.
GraphQL Query Examples
To give you a concrete picture, here is a simple example API scenario involving users and posts.
1. GraphQL Schema
Suppose we have the most basic GraphQL schema:
1type User {
2 id: ID!
3 name: String!
4 posts: [Post]
5}
6
7type Post {
8 id: ID!
9 title: String!
10 content: String!
11 author: User
12}
13
14type Query {
15 users: [User]
16 posts: [Post]
17 user(id: ID!): User
18 post(id: ID!): Post
19}2. Querying Data
If your application only needs the user’s name and the titles of the posts they wrote, simply run this query:
1query {
2 users {
3 name
4 posts {
5 title
6 }
7 }
8}The response:
1{
2 "data": {
3 "users": [
4 {
5 "name": "Andi",
6 "posts": [{ "title": "Belajar GraphQL dengan Mudah" }]
7 },
8 {
9 "name": "Budi",
10 "posts": [{ "title": "REST vs GraphQL: Mana Pilihanmu?" }]
11 }
12 ]
13 }
14}3. Adding Data with a Mutation
Suppose you want to add a new user:
1mutation {
2 createUser(name: "Citra") {
3 id
4 name
5 }
6}Benefits of GraphQL
1. Avoiding Over-fetching & Under-fetching
REST APIs sometimes force the client to fetch too much (over-fetching) or too little (under-fetching) data. GraphQL eliminates this problem by giving the client full control.
2. Efficient for Mobile & Web
Because it can request specific data, a GraphQL query is bandwidth-friendly, making it a great fit for mobile applications that need to conserve data.
3. Self-Documenting
The GraphQL schema is automatically published and can be explored with tools such as GraphiQL or Apollo Studio, so there is no need for manual documentation that quickly becomes outdated.
4. Faster Frontend Development
Frontend engineers can design queries to suit new feature requirements without having to depend on backend developers to add or modify endpoints.
5. Real-time with Subscriptions
Need live data updates (chat, notifications, and so on)? GraphQL subscriptions support this pattern natively.
6. Typed & Validated
Wrong query or a field that doesn’t exist? The server returns a clear error because everything is strongly typed and validated before it ever reaches the database.
A Simple Case Study: REST vs GraphQL
Suppose you want to display a user profile page along with their latest posts and comments.
REST (the usual way)
- GET /users/42
-> User detail data - GET /users/42/posts
-> List of posts - GET /posts/{postId}/comments for each post
-> Comments for each post
At least 4–5 requests to get the complete view!
GraphQL
A single query:
1query {
2 user(id: 42) {
3 name
4 posts {
5 title
6 comments {
7 content
8 author { name }
9 }
10 }
11 }
12}Just one call, and the result already matches the structure the user interface needs.
When Should You Use GraphQL?
GraphQL is an excellent fit for:
- Applications with complex nested/relational data
- Dynamic and frequently changing mobile/web frontends
- Many data sources (microservices, third parties, and so on)
That said, REST remains simpler when your data needs are not complicated, or for small projects.
Closing
GraphQL is not an absolute replacement for REST, but rather an evolution of how clients and servers communicate data in a more flexible and efficient way. The concepts of a schema, specific queries, and a self-documentation system make GraphQL appealing to many modern development teams. As applications and their data become increasingly dynamic, GraphQL can become the foundation of your future API.
If you want to explore further, try libraries such as Apollo Server (Node.js), HotChocolate (C#), or Graphene (Python).
Happy coding! 🚀
Additional resources: