Introduction to GraphQL for Backend
Developers
Lecture Notes • v1.0
These notes cover GraphQL fundamentals and advanced patterns for real-world backends.
1. Core Concepts
• Schema Definition Language (SDL) — types, queries, mutations, subscriptions
• Resolvers — field-level data fetchers, batching, and caching
• N+1 Problem — solve with dataloaders and careful resolver design
2. Example Schema
type Query {
departure(id: ID!): Departure
departures(where: DepartureFilter, limit: Int = 50, offset: Int = 0):
[Departure!]!
}
type Departure {
id: ID!
start_date: String!
trip_version: TripVersion!
reservations(limit: Int = 20): [Reservation!]!
}
type TripVersion {
id: ID!
name: String!
dynamic_pricing: Boolean!
}
type Reservation {
id: ID!
status: String!
}
3. Query Patterns
• Pagination with limit/offset or cursor-based relay style
• Filtering via input types (e.g., *_bool_exp)
query GetDepartures($where: DepartureFilter, $limit: Int!, $offset:
Int!) {
departures(where: $where, limit: $limit, offset: $offset) {
id
start_date
trip_version { id name dynamic_pricing }
reservations(limit: 5) { id status }
}
}
4. Security & Best Practices
• Apply auth at resolver level • Rate-limit expensive fields • Persisted queries
• Add operation-level metrics and timeouts • Validate inputs with schema
Further Reading
• Official GraphQL Specification
• Apollo Server Docs
• Hasura Docs (for Postgres-backed GraphQL)