Reviews GraphQL schemas, resolvers, and operations for N+1 problems, query complexity limits, input validation, security best practices, and proper error handling.
// BAD: Fetches all fields regardless of query
const resolvers = {
Query: {
user: (_, { id }) => db.user.findUnique({
where: { id },
include: {
posts: true, // Maybe not requested
comments: true, // Maybe not requested
followers: true // Maybe not requested
}
})
}
};
// GOOD: Use info to select only requested fields
import { GraphQLResolveInfo } from 'graphql';
import graphqlFields from 'graphql-fields';
const resolvers = {
Query: {
user: (_, { id }, __, info: GraphQLResolveInfo) => {
const requestedFields = graphqlFields(info);
return db.user.findUnique({
where: { id },
include: {
posts: 'posts' in requestedFields,
comments: 'comments' in requestedFields
}
});
}
}
};
// BETTER: Use Prisma's select based on GraphQL query
import { PrismaSelect } from '@paljs/plugins';
const resolvers = {
Query: {
user: (_, { id }, __, info) => {
const select = new PrismaSelect(info).value;
return db.user.findUnique({ where: { id }, ...select });
}
}
};
Critical: Mutation in Query
Pattern
Issue
Severity
Side effects in Query
Violates spec
CRITICAL
Write operation in Query
Unexpected behavior
CRITICAL
# BAD: Mutation disguised as Query
type Query {
incrementViewCount(postId: ID!): Int! # WRONG! This mutates data
markAsRead(notificationId: ID!): Boolean! # WRONG!
}
# GOOD: Mutations for side effects
type Mutation {
incrementViewCount(postId: ID!): Post!
markAsRead(notificationId: ID!): Notification!
}
# Query should be idempotent (read-only)
type Query {
post(id: ID!): Post
viewCount(postId: ID!): Int!
}
# BAD: Unnecessarily nullable
type User {
id: ID # Should always exist
email: String # Required for user
name: String # Should be required
bio: String # OK to be nullable
}
# GOOD: Clear nullability
type User {
id: ID! # Always present
email: String! # Required
name: String! # Required
bio: String # Optional (nullable)
deletedAt: DateTime # Optional
}
# For fields that may fail to resolve
type Post {
id: ID!
author: User # Nullable if author deleted
authorId: ID! # Always has the reference
}
Response Template
## GraphQL Code Review Results
**Project**: [name]
**Server**: Apollo Server 4.x
**Schema**: SDL-first / Code-first
### N+1 / DataLoader
#### CRITICAL
| File | Line | Issue |
|------|------|-------|
| resolvers/post.ts | 23 | N+1 in author resolver - use DataLoader |
| resolvers/user.ts | 45 | posts fetched per user without batching |
### Query Complexity / Security
| File | Line | Issue |
|------|------|-------|
| server.ts | 12 | No depth limit configured |
| schema.graphql | 34 | posts query unbounded - add pagination |
### Input Validation
| File | Line | Issue |
|------|------|-------|
| mutations/user.ts | 56 | No email validation |
| mutations/post.ts | 23 | Missing input sanitization |
### Error Handling
| File | Line | Issue |
|------|------|-------|
| resolvers/query.ts | 78 | Raw error thrown - use GraphQLError |
### Schema Design
| File | Line | Issue |
|------|------|-------|
| schema.graphql | 12 | Query with side effect - move to Mutation |
| types/user.graphql | 8 | Exposes sequential database ID |
### Recommendations
1. [ ] Implement DataLoader for all relationship resolvers
2. [ ] Add depth limit (max 5-7 levels)
3. [ ] Add query complexity plugin (max 1000)
4. [ ] Add pagination to all list fields
5. [ ] Validate all mutation inputs with Zod/Yup
### Positive Patterns
- Good use of Relay connections for pagination
- Proper error codes in GraphQL errors