APIs & Integrations

GraphQL

GraphQL is a query language for APIs that lets clients request exactly the data they need. Learn queries, mutations, subscriptions, fragments, variables, error handling, and the N+1 problem — all in one place.

What is GraphQL?

GraphQL is a query language for APIs where the client asks for exactly the fields it needs, in one request, from a single endpoint. The server defines a typed schema, and clients send queries (reads) and mutations (writes). It avoids over-fetching and multiple round-trips, at the cost of more server complexity and harder HTTP caching.

query {
  user(id: "42") {
    name
    posts(last: 3) { title }
  }
}

const res = await fetch("/graphql", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ query, variables: { id: "42" } }),
});

GraphQL vs REST

FeatureRESTGraphQL
EndpointMany (/users, /posts)One (/graphql)
MethodGET/POST/PUT/DELETEPOST (mostly)
Over-fetchingCommonNone — ask for fields
Under-fetchingMultiple tripsOne request, nested
VersioningURL /v2/ or headersSchema evolution
ToolingOpenAPI/SwaggerGraphiQL / Playground
Learning curveLowMedium-high

Core SDL Types

type
type User { id: ID! name: String! }
Object type — the main building block
Query
type Query { user(id: ID!): User }
Read-only entry points (like GET)
Mutation
type Mutation { createUser(input: ...): User }
Write operations (create/update/delete)
Subscription
type Subscription { onMessage: Message }
Real-time events via WebSocket
input
input CreateUserInput { name: String! email: String! }
Input types for mutations
enum
enum Role { ADMIN USER GUEST }
Enumerated values
union
union SearchResult = User | Post
Return one of several types
interface
interface Node { id: ID! }
Shared fields across types

Common Pitfalls

Checking res.ok for GraphQL errors — GraphQL always returns HTTP 200 even on errors
Always check json.errors after parsing. The actual errors live in json.errors[], not the HTTP status.
Interpolating user input into query strings — enables GraphQL injection
Always use variables: graphqlRequest(url, query, { id: userId }). Variables are typed and safely handled server-side.
The N+1 problem: one query per item (100 posts → 100 user fetches)
Use DataLoader to batch and cache. All user.load(id) calls in one tick are batched into a single DB query.
Selecting all fields with __typename everywhere instead of using fragments
Define reusable fragments (fragment UserFields on User {...}) and spread them to avoid duplication and sync issues.

Interactive Demo

Code Editor

<button id="go">Run GraphQL query</button>
<pre id="out"></pre>

Preview

GraphQL: summary

Key points

  • Errors usually come back with status 200 in an errors array, so check it.
  • Clients like Apollo and urql add caching and React hooks.
  • Pass user input as variables, never by string-building queries.

Frequently asked questions

›GraphQL or REST?

REST is simpler and caches easily; it's a great default. GraphQL shines when many clients need different shapes of deeply related data.