# GRAPHQL Patterns

> Design resolvers, handle N+1, manage mutations, and structure your schema effectively.

- Skill: `rahulrachhoya/graphql-patterns` (Agent Skill)
- Install (CLI): `npx skillmds@latest add rahulrachhoya/graphql-patterns`
- Raw SKILL.md: https://api.skillmd.com/api/skills/rahulrachhoya/graphql-patterns/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Integrations & APIs
- Author: RahulRachhoya (https://skillmd.com/u/rahulrachhoya)
- Updated: 2026-09-22
- Page: https://skillmd.com/skills/rahulrachhoya/graphql-patterns

---


# GraphQL Patterns

## Schema Design
- **Describe data, not actions** — nouns in types, verbs in mutations
- **Use interfaces and unions** for polymorphic relationships
- **Nullable by default** — only mark required if it's truly always present
- **Paginate lists** — always use connection types for list fields

## N+1 Prevention
```graphql
# Bad: N+1 queries
type User {
  posts: [Post]  # Fetches posts for each user individually
}

# Good: use DataLoader
const postLoader = new DataLoader(ids =>
  db.posts.findAll({ where: { userId: { in: ids } } })
)
```

## Mutations
- Single responsibility per mutation
- Return the mutated object (so the client can update its cache)
- Input types end with `Input`: `CreateUserInput`
- Use optimistic updates for fast UX (revert on error)

## Security
- Depth limiting (prevent deeply nested queries)
- Complexity scoring (costly fields have higher weight)
- Rate limiting at the query level
- Auth checked in every resolver, not middleware
- Query whitelisting for production APIs (persisted queries)

