GraphQL Server Architect
Design production-grade GraphQL APIs with efficient data loading, real-time subscriptions, and federated schema architecture.
Activation Triggers
Activate on: "GraphQL", "DataLoader", "subscription", "federation", "schema stitching", "resolver", "SDL", "Apollo Server", "GraphQL Yoga", "Pothos", "query complexity"
NOT for: REST API design → api-architect | Frontend GraphQL client → relevant frontend skill | Database queries → data-warehouse-optimizer
Quick Start
- Choose framework — GraphQL Yoga 5.x (lightweight), Apollo Server 4.x (ecosystem), Mercurius (Fastify)
- Schema-first or code-first — SDL for team collaboration, Pothos/Nexus for type-safe code-first
- Implement DataLoader — batch and cache per-request to solve N+1 queries
- Set query complexity limits — prevent clients from requesting arbitrarily deep/wide queries
- Add persisted queries — lock down production to known queries, improve CDN caching
Core Capabilities
| Domain |
Technologies |
| Servers |
GraphQL Yoga 5.x, Apollo Server 4.x, Mercurius 14+ |
| Schema |
Pothos (code-first), SDL (schema-first), GraphQL Codegen |
| Federation |
Apollo Federation 2.8+, GraphQL Mesh, Schema Stitching |
| Performance |
DataLoader, @defer/@stream, persisted queries, query complexity |
| Real-Time |
GraphQL Subscriptions (WebSocket), graphql-ws, SSE transport |
Architecture Patterns
DataLoader Pattern (N+1 Prevention)
import DataLoader from 'dataloader';
// Create per-request DataLoader
function createLoaders() {
return {
userById: new DataLoader<string, User>(async (ids) => {
// Single batch query instead of N queries
const users = await db.query('SELECT * FROM users WHERE id = ANY($1)', [ids]);
const map = new Map(users.map(u => [u.id, u]));
return ids.map(id => map.get(id) ?? new Error(`User ${id} not found`));
}),
};
}
// Resolver uses loader — automatically batched
const resolvers = {
Post: {
author: (post, _, { loaders }) => loaders.userById.load(post.authorId),
},
};
Apollo Federation 2 (Supergraph)
Clients
↓
Apollo Router (supergraph)
├─→ Users Subgraph (owns User type)
├─→ Orders Subgraph (extends User with orders)
└─→ Products Subgraph (owns Product type)
Each subgraph is an independent GraphQL service.
Router composes query plans across subgraphs.
# Users subgraph
type User @key(fields: "id") {
id: ID!
name: String!
email: String!
}
# Orders subgraph — extends User from Users subgraph
type User @key(fields: "id") {
id: ID!
orders: [Order!]!
}
type Order {
id: ID!
total: Float!
status: OrderStatus!
}
Query Complexity Limiting
import { createComplexityLimitRule } from 'graphql-validation-complexity';
const complexityLimit = createComplexityLimitRule(1000, {
scalarCost: 1,
objectCost: 10,
listFactor: 20,
onCost: (cost) => {
if (cost > 800) logger.warn(`High complexity query: ${cost}`);
},
});
const server = createYoga({
schema,
validationRules: [complexityLimit],
});
Anti-Patterns
- No DataLoader — resolvers that query the DB individually create N+1 problems; always batch with DataLoader
- Unbounded queries — without depth/complexity limits, a single query can join your entire graph and crash the server
- Exposing internal IDs — use opaque Relay-style global IDs, not raw database primary keys
- Fat resolvers — resolvers should delegate to service/repository layers, not contain business logic
- Subscription over-fetching — do not broadcast full objects; send minimal change payloads and let clients re-query
Quality Checklist
1---
2license: Apache-2.0
3name: graphql-server-architect
4description: "DataLoader, subscriptions, federation, and schema stitching for GraphQL APIs. Activate on: GraphQL, DataLoader, subscription, federation, schema stitching, resolver, SDL, Apollo, Yoga. NOT for: REST API design (use api-architect), frontend GraphQL clients (use relevant frontend skill)."
5allowed-tools: Read,Write,Edit,Bash(npm:*,npx:*)
6category: Backend & Infrastructure
7tags:
8 - graphql
9 - federation
10 - dataloader
11 - subscriptions
12 - schema
13pairs-with:
14 - skill: cache-strategy-invalidation-expert
15 reason: Query-level and resolver-level caching for GraphQL
16 - skill: observability-apm-expert
17 reason: Per-resolver tracing and query complexity monitoring
18 - skill: api-rate-limiting-throttling-expert
19 reason: Query cost analysis prevents expensive queries
20metadata:
21 source: https://skillmd.com/skills/curiositech/graphql-server-architect
22---
23
24# GraphQL Server Architect
25
26Design production-grade GraphQL APIs with efficient data loading, real-time subscriptions, and federated schema architecture.
27
28## Activation Triggers
29
30**Activate on:** "GraphQL", "DataLoader", "subscription", "federation", "schema stitching", "resolver", "SDL", "Apollo Server", "GraphQL Yoga", "Pothos", "query complexity"
31
32**NOT for:** REST API design → `api-architect` | Frontend GraphQL client → relevant frontend skill | Database queries → `data-warehouse-optimizer`
33
34## Quick Start
35
361. **Choose framework** — GraphQL Yoga 5.x (lightweight), Apollo Server 4.x (ecosystem), Mercurius (Fastify)
372. **Schema-first or code-first** — SDL for team collaboration, Pothos/Nexus for type-safe code-first
383. **Implement DataLoader** — batch and cache per-request to solve N+1 queries
394. **Set query complexity limits** — prevent clients from requesting arbitrarily deep/wide queries
405. **Add persisted queries** — lock down production to known queries, improve CDN caching
41
42## Core Capabilities
43
44| Domain | Technologies |
45|--------|-------------|
46| **Servers** | GraphQL Yoga 5.x, Apollo Server 4.x, Mercurius 14+ |
47| **Schema** | Pothos (code-first), SDL (schema-first), GraphQL Codegen |
48| **Federation** | Apollo Federation 2.8+, GraphQL Mesh, Schema Stitching |
49| **Performance** | DataLoader, @defer/@stream, persisted queries, query complexity |
50| **Real-Time** | GraphQL Subscriptions (WebSocket), graphql-ws, SSE transport |
51
52## Architecture Patterns
53
54### DataLoader Pattern (N+1 Prevention)
55
56```typescript
57import DataLoader from 'dataloader';
58
59// Create per-request DataLoader
60function createLoaders() {
61 return {
62 userById: new DataLoader<string, User>(async (ids) => {
63 // Single batch query instead of N queries
64 const users = await db.query('SELECT * FROM users WHERE id = ANY($1)', [ids]);
65 const map = new Map(users.map(u => [u.id, u]));
66 return ids.map(id => map.get(id) ?? new Error(`User ${id} not found`));
67 }),
68 };
69}
70
71// Resolver uses loader — automatically batched
72const resolvers = {
73 Post: {
74 author: (post, _, { loaders }) => loaders.userById.load(post.authorId),
75 },
76};
77```
78
79### Apollo Federation 2 (Supergraph)
80
81```
82Clients
83 ↓
84Apollo Router (supergraph)
85 ├─→ Users Subgraph (owns User type)
86 ├─→ Orders Subgraph (extends User with orders)
87 └─→ Products Subgraph (owns Product type)
88
89Each subgraph is an independent GraphQL service.
90Router composes query plans across subgraphs.
91```
92
93```graphql
94# Users subgraph
95type User @key(fields: "id") {
96 id: ID!
97 name: String!
98 email: String!
99}
100
101# Orders subgraph — extends User from Users subgraph
102type User @key(fields: "id") {
103 id: ID!
104 orders: [Order!]!
105}
106
107type Order {
108 id: ID!
109 total: Float!
110 status: OrderStatus!
111}
112```
113
114### Query Complexity Limiting
115
116```typescript
117import { createComplexityLimitRule } from 'graphql-validation-complexity';
118
119const complexityLimit = createComplexityLimitRule(1000, {
120 scalarCost: 1,
121 objectCost: 10,
122 listFactor: 20,
123 onCost: (cost) => {
124 if (cost > 800) logger.warn(`High complexity query: ${cost}`);
125 },
126});
127
128const server = createYoga({
129 schema,
130 validationRules: [complexityLimit],
131});
132```
133
134## Anti-Patterns
135
1361. **No DataLoader** — resolvers that query the DB individually create N+1 problems; always batch with DataLoader
1372. **Unbounded queries** — without depth/complexity limits, a single query can join your entire graph and crash the server
1383. **Exposing internal IDs** — use opaque Relay-style global IDs, not raw database primary keys
1394. **Fat resolvers** — resolvers should delegate to service/repository layers, not contain business logic
1405. **Subscription over-fetching** — do not broadcast full objects; send minimal change payloads and let clients re-query
141
142## Quality Checklist
143
144- [ ] DataLoader used for all relationship resolvers (no N+1)
145- [ ] Query depth limit set (max 10-15 levels)
146- [ ] Query complexity limit enforced (reject expensive queries)
147- [ ] Persisted queries enabled in production (no arbitrary queries from clients)
148- [ ] Schema published to registry with breaking change detection
149- [ ] Resolver-level tracing enabled (Apollo Studio, OpenTelemetry)
150- [ ] Error masking: internal errors never leak to clients in production
151- [ ] Pagination uses Relay cursor-based spec (not offset)
152- [ ] Input validation on all mutation arguments
153- [ ] Subscription authentication verified on connection init