GraphQL Introduction — A Query Language for Modern APIs
In this tutorial, you will learn about Graphql Introduction. We cover key concepts, practical examples, and best practices to help you master this topic.
GraphQL is an API query language developed by Meta that allows clients to specify exactly what data they need in a single request, receiving predictable results without the over-fetching common in REST.
What You'll Learn
- How GraphQL differs from REST in data fetching
- The core concepts of queries, mutations, and subscriptions
- When to choose GraphQL over REST
Why It Matters
GraphQL reduces the number of API calls needed for complex UIs and eliminates over-fetching. DodaTech's Durga Antivirus Pro dashboard uses GraphQL to let the UI request only the threat data it needs per view.
flowchart LR
A["Client Query"] --> B["GraphQL API\n(Single Endpoint)"]
B --> C["Type System\n(Schema)"]
B --> D["Resolvers"]
D --> E["Database"]
D --> F["External API"]
D --> G["Cache"]
A --> H["Response:\nExactly What Was Requested"]
style B fill:#dbeafe,stroke:#2563eb
Code Examples
# GraphQL query
query {
user(id: "123") {
name
email
posts {
title
}
}
}
// Calling GraphQL from Node.js
const query = `
query {
user(id: "123") {
name
email
posts {
title
}
}
}
`;
fetch('https://api.example.com/graphql', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({query})
})
.then(res => res.json())
.then(data => console.log(data));
import requests
query = """
query {
user(id: "123") {
name
email
}
}
"""
response = requests.post(
"https://api.example.com/graphql",
json={"query": query}
)
print(response.json())
Common Mistakes
1. Not Using a Schema
GraphQL's power comes from its strongly typed schema. Without it, you lose validation and introspection.
2. Creating N+1 Queries
Without DataLoader or batching, each nested field can cause a separate database query.
3. Ignoring Authentication
GraphQL resolves all fields. Ensure authorization checks run per field.
4. Using GraphQL for Simple CRUD
REST is simpler for basic CRUD operations with no complex data requirements.
5. Not Limiting Query Depth
Malicious clients can create deeply nested queries that overwhelm the server.
Practice Questions
- What problem does GraphQL solve that REST doesn't?
- What are the three operation types in GraphQL?
- How does GraphQL know what data is available?
- What is a resolver in GraphQL?
- Why might you choose REST over GraphQL?
Answers:
- Over-fetching (getting too much data) and under-fetching (needing multiple requests).
- Query (read), Mutation (write), Subscription (real-time).
- Through the schema, which defines types, fields, and relationships.
- A resolver is a function that fetches data for a specific field in the schema.
- For simple CRUD APIs, when HTTP Caching is critical, or when the team isn't familiar with GraphQL.
Challenge: Design a GraphQL schema for a blog with users, posts, and comments. Include queries, mutations, and at least one subscription.
FAQ
What's Next
Compare GraphQL with REST APIs, or explore the GraphQL Schema and Types system in depth.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro