GraphQL is a powerful query language for APIs developed by Facebook, allowing clients to request exactly the data they need and nothing more.
Unlike REST APIs, GraphQL lets you fetch multiple resources in a single request, improving performance and flexibility for modern web and mobile apps.
This GraphQL Cheatsheet is your ultimate quick reference guide to all essential syntax, queries, mutations, subscriptions, and schema definitions — perfect for developers who want to master GraphQL fast.
Table of Contents:
What is GraphQL?
GraphQL is a data query and manipulation language for APIs, as well as a runtime for fulfilling queries with your existing data.
It provides a strongly typed schema that defines how clients can access your API.
✅ Key Advantages of GraphQL:
- Fetch multiple resources in one query
- No over-fetching or under-fetching
- Strong typing system
- Real-time data with subscriptions
- Perfect for microservices and frontend apps
Basic GraphQL Syntax
| Operation | Syntax Example |
|---|---|
| Query | query { user(id: 1) { name email } } |
| Mutation | mutation { addUser(name: "John", email: "john@site.com") { id } } |
| Subscription | subscription { newUser { id name } } |
| Fragment | fragment userInfo on User { name email } |
graphql cheatsheet, graphql tutorial for beginners, graphql queries examples, graphql mutations, graphql vs rest, graphql schema design, graphql resolver, graphql syntax, graphql subscription, graphql with nodejs
GraphQL Query Example
A query is used to read or fetch data.
query {
user(id: 1) {
id
name
email
posts {
title
content
}
}
}
✅ Output:
{
"data": {
"user": {
"id": 1,
"name": "Alice",
"email": "alice@example.com",
"posts": [
{ "title": "Intro to GraphQL", "content": "GraphQL basics..." }
]
}
}
}
GraphQL Mutation Example
A mutation is used to create, update, or delete data.
mutation {
createUser(name: "John", email: "john@example.com") {
id
name
email
}
}
✅ Output:
{
"data": {
"createUser": {
"id": "2",
"name": "John",
"email": "john@example.com"
}
}
}
GraphQL Schema Definition Language (SDL)
A schema defines the structure of data and operations available in GraphQL.
type User {
id: ID!
name: String!
email: String!
posts: [Post]
}
type Post {
id: ID!
title: String!
content: String!
author: User
}
type Query {
users: [User]
user(id: ID!): User
}
type Mutation {
createUser(name: String!, email: String!): User
}
GraphQL Fragments
Fragments help reuse field selections across multiple queries.
fragment userDetails on User {
id
name
email
}
query {
users {
...userDetails
}
}
GraphQL Directives
Directives modify query execution dynamically.
query GetUsers($showEmail: Boolean!) {
users {
name
email @include(if: $showEmail)
}
}
GraphQL Variables
query getUser($userId: ID!) {
user(id: $userId) {
name
email
}
}
Variables:
{
"userId": "1"
}
GraphQL Subscriptions (Real-Time Updates)
Subscriptions allow you to receive live data updates when events occur.
subscription {
postAdded {
id
title
content
}
}
GraphQL vs REST API
| Feature | GraphQL | REST API |
|---|---|---|
| Data Fetching | Single endpoint | Multiple endpoints |
| Over-fetching | ❌ No | ✅ Yes |
| Real-time support | ✅ Yes (Subscriptions) | ❌ No |
| Query flexibility | High | Fixed |
| Data typing | Strongly typed | No typing |
| Performance | Optimized | Can be slower |
GraphQL Common Use Cases
- Web and mobile APIs
- Microservice communication
- Real-time dashboards
- Cross-platform applications
- Data aggregation from multiple sources
Error Handling in GraphQL
{
"errors": [
{
"message": "User not found",
"locations": [{ "line": 2, "column": 3 }],
"path": ["user"]
}
]
}
GraphQL Tools and Libraries
| Tool | Purpose |
|---|---|
| Apollo Server | Build GraphQL APIs in Node.js |
| Apollo Client | Query GraphQL APIs from the frontend |
| GraphQL Yoga | Simple server framework |
| GraphiQL | Interactive in-browser query editor |
| Hasura | Auto-generates GraphQL APIs from databases |
| Prisma | ORM for GraphQL and databases |
PostgreSQL Integration with GraphQL
GraphQL can integrate directly with PostgreSQL using:
- Hasura GraphQL Engine
- PostGraphile
- Prisma
Example query:
query {
users {
id
name
posts {
title
}
}
}
Performance Optimization Tips
- Use query caching for frequent requests.
- Avoid deep nested queries to prevent recursion.
- Implement batch resolvers to reduce round trips.
- Use pagination for large datasets (
limit,offset).
FAQ — GraphQL Cheatsheet
Q1: What is GraphQL used for?
GraphQL is used to efficiently fetch and manipulate data from APIs using a single endpoint.
Q2: Is GraphQL faster than REST?
Yes, because it eliminates multiple endpoint calls and reduces payload size.
Q3: Can I use GraphQL with PHP or Python?
Yes, libraries like Lighthouse (PHP) and Graphene (Python) support GraphQL.
Q4: Does GraphQL replace REST?
Not necessarily. It complements REST by providing more flexible querying capabilities.
Q5: How do I test GraphQL queries?
Use GraphiQL, Apollo Studio, or Postman GraphQL mode.

