GraphQL logoGraphQL vLatestINTERMEDIATE

GraphQL

Complete guide to GraphQL - query language and runtime for APIs with types, queries, mutations, and subscriptions

15 min read
graphqlapiqueriesmutationssubscriptionsapolloschemaresolvers

Setup & Basics

GraphQL Server Setup

📄 Codejavascript
💡 GraphQL is a query language and runtime for APIs
⚡ Single endpoint typically at /graphql
📌 Strongly typed with schema definition
🟢 Works with any backend language or database

Schema Definition Language (SDL)

📄 Codegraphql
💡 Schema defines API structure and types
⚡ ! means non-nullable, [] means list
📌 Scalar types: Int, Float, String, Boolean, ID
🔥 Custom scalars for dates, JSON, etc.

Queries

Basic queries

graphql
💡 Request exactly what you need
⚡ Single request for nested data
📌 No over-fetching or under-fetching
🟢 Predictable results matching query shape

Query variables and directives

graphql
💡 Variables make queries reusable
⚡ Directives modify query execution
📌 @include and @skip for conditional fields
🔥 Custom directives for advanced features

Mutations

Basic mutations

graphql
💡 Mutations modify server-side data
⚡ Return updated data after mutation
📌 Use input types for complex arguments
🟢 Can include queries in response

Optimistic updates and error handling

📄 Codejavascript
💡 Optimistic UI updates for better UX
⚡ Rollback on error automatically
📌 Error handling with try-catch
🔥 Cache updates after mutations

Subscriptions

Real-time subscriptions

javascript
💡 Real-time updates via WebSocket
⚡ Server pushes updates to clients
📌 Use for live features like chat
🔥 Efficient for selective updates

Resolvers

Resolver structure and context

javascript
💡 Resolvers fetch data for schema fields
⚡ Four arguments: parent, args, context, info
📌 Context shares data across resolvers
🟢 Can be sync or async functions

DataLoader and N+1 problem

📄 Codejavascript
💡 DataLoader batches and caches requests
⚡ Solves N+1 query problem
📌 Automatic request deduplication
🔥 Per-request caching

Authentication & Authorization

Authentication patterns

javascript
💡 Use context for auth state
⚡ JWT tokens common for GraphQL
📌 Check auth in resolvers or middleware
🔐 Never expose sensitive fields

Error Handling

Error types and handling

javascript
💡 GraphQL returns 200 OK even with errors
⚡ Errors in errors array, data can be partial
📌 Use custom error classes for clarity
🔥 Format errors for production

Performance Optimization

Query optimization techniques

javascript
💡 Use DataLoader for batching
⚡ Implement query depth limiting
📌 Add query complexity analysis
🔥 Cache with Redis or in-memory

Testing

Testing GraphQL APIs

javascript
💡 Test resolvers in isolation
⚡ Use test server for integration tests
📌 Mock data sources and context
🟢 Test both success and error cases

Best Practices

Schema design principles

graphql
💡 Design schema for clients, not database
⚡ Use clear, consistent naming
📌 Avoid deep nesting (max 3-4 levels)
🎯 Version via field deprecation, not versions

Security best practices

📄 Codejavascript
🔐 Always validate and sanitize inputs
⚡ Implement rate limiting
📌 Disable introspection in production
🔥 Use query depth and complexity limits