patterngraphqlModeratepending
GraphQL schema design best practices
Viewed 0 times
graphqlschema designconnectionspaginationmutationsrelay
Problem
Need to design a GraphQL schema that is consistent, performant, and maintainable.
Solution
GraphQL schema design guidelines:
Rules:
# 1. Use connections for pagination (Relay-style)
type Query {
users(first: Int, after: String): UserConnection!
}
type UserConnection {
edges: [UserEdge!]!
pageInfo: PageInfo!
totalCount: Int!
}
type UserEdge {
cursor: String!
node: User!
}
type PageInfo {
hasNextPage: Boolean!
endCursor: String
}
# 2. Input types for mutations
input CreateUserInput {
name: String!
email: String!
role: Role = USER
}
type Mutation {
createUser(input: CreateUserInput!): CreateUserPayload!
}
# 3. Mutation payloads with errors
type CreateUserPayload {
user: User
errors: [UserError!]!
}
type UserError {
field: String
message: String!
}
# 4. Use enums for fixed sets
enum Role {
USER
ADMIN
MODERATOR
}
# 5. Interface for shared fields
interface Node {
id: ID!
}
type User implements Node {
id: ID!
name: String!
email: String!
}Rules:
- Non-nullable by default, nullable for optional fields
- Use connections for lists (not bare arrays)
- Mutations return payloads with possible errors
- Use interfaces for polymorphism
- Avoid deeply nested resolvers (N+1 risk)
Why
A well-designed GraphQL schema prevents breaking changes, enables efficient querying, and provides clear contracts between frontend and backend.
Gotchas
- N+1 queries: use DataLoader for batch loading
- Deeply nested queries can be expensive - implement query depth limits
- GraphQL has no built-in rate limiting - implement per-query cost analysis
Context
Designing GraphQL APIs
Revisions (0)
No revisions yet.