GraphQL

Queries, mutations, schemas, and comparison with REST

What is GraphQL?

A query language that puts clients in control of their data

GraphQL is a query language for APIs that lets clients request exactly the data they need. Unlike REST, where each endpoint returns a fixed data structure, GraphQL exposes a single endpoint and lets the client describe the shape of the response.

All GraphQL requests go to a single endpoint (e.g., POST /graphql). The server has a schema defining available types and operations, and resolvers that know how to fetch data for each field.

Queries, Mutations & Subscriptions

The three operation types in GraphQL

GraphQL Operations

Click each operation to see the query and response

Query (read)
Mutation (write)
Subscription (real-time)

Schema & Type System

The strongly-typed contract between client and server

GraphQL Type Definitions

Click each type to see its SDL definition

Click on a type to see its definition

Built-in Scalar Types

Int

Signed 32-bit integer

Float

64-bit floating point

String

UTF-8 text

Boolean

true or false

ID

Unique identifier

💡

Type Modifiers

String! Non-nullable (required)
[String] List (nullable items)
[String!]! Non-null list of non-null items
String Nullable (optional)

How Resolvers Work

The execution engine behind every GraphQL query

Query Execution Flow

Watch how a query is parsed, validated, and resolved step by step

Client
GraphQL Server
Resolvers
Data Sources
Client Sends Query

Client sends a GraphQL query specifying exactly which fields it needs

query {
  user(id: "1") {
    name
    posts { title }
  }
}
💡

Resolver Function Signature

fieldName(parent, args, context, info)
  parent  → result from parent resolver
  args    → arguments passed to the field
  context → shared data (auth, DB connections)
  info    → query AST and execution info

REST vs GraphQL

Understanding when to use each approach

Fetching a user's profile with their posts and followers requires 3 separate requests:

GET /api/users/1 → returns all user fields
GET /api/users/1/posts → returns all post fields
GET /api/users/1/followers → returns all follower fields

3 round trips, likely returns more data than needed (over-fetching)

Data Fetching
REST

Multiple endpoints, each returns fixed data structure. May require multiple requests for related data.

GraphQL

Single endpoint, client specifies exact fields needed. Fetches related data in one request.

Over-fetching
REST

Returns all fields even if client only needs a few. Wastes bandwidth.

GraphQL

Client requests only the fields it needs. No wasted data.

Under-fetching
REST

May need to call multiple endpoints to get all related data (N+1 requests).

GraphQL

Nested queries fetch all related data in a single request.

Caching
REST

Simple HTTP caching with ETags, Cache-Control. Each URL is a cacheable resource.

GraphQL

Complex - POST requests are not cached by default. Requires client-side caching (Apollo, Relay).

Error Handling
REST

Standard HTTP status codes (404, 500, etc). Well-understood conventions.

GraphQL

Always returns 200 OK. Errors are in response body. Partial data with errors is possible.

Versioning
REST

Requires explicit versioning (v1, v2) for breaking changes.

GraphQL

Deprecate fields instead of versioning. Clients only use the fields they need.

File Uploads
REST

Natively supports multipart/form-data. Simple and well-supported.

GraphQL

Not built-in. Requires multipart spec extension or separate REST endpoint.

Learning Curve
REST

Uses standard HTTP methods and URLs. Easy to learn and debug with curl/browser.

GraphQL

New query language, schema definition, resolvers. Steeper learning curve.

💡

When to Choose What?

  • REST - Simple CRUD apps, public APIs, strong caching needs, file handling
  • GraphQL - Complex data relationships, mobile apps (bandwidth), multiple client types, rapid iteration
  • Both - Many companies use REST for simple services and GraphQL as a gateway layer

Performance & Security

Common pitfalls and production-ready patterns

Click on a topic to see the problem and solution

Key Terms to Remember

Master these terms for technical interviews