Resolver
ELI5 — The Vibe Check
A resolver is a function that fetches the data for a single field in a GraphQL query. When you ask for user.posts, there's a resolver that knows how to get those posts. Each field in your schema maps to a resolver. They're the workers that actually get the data.
Real Talk
In GraphQL, a resolver is a function that returns the data for a specific field in a schema. Resolvers receive four arguments: parent (result of the parent resolver), args (field arguments), context (shared across resolvers, holds auth/DB), and info (query metadata). They can return values, promises, or throw errors.
Show Me The Code
const resolvers = {
Query: {
user: (_, { id }, { db }) => db.users.findById(id),
},
User: {
posts: (parent, _, { db }) => db.posts.findByUserId(parent.id),
}
};
When You'll Hear This
"Each GraphQL field has a resolver that knows how to fetch its data." / "The user.posts resolver is causing an N+1 query — use a DataLoader."
Related Terms
Data Loader
DataLoader batches and caches database calls in GraphQL. Without it, fetching 100 users with their posts makes 101 database queries (the N+1 problem).
GraphQL
GraphQL is like ordering food where YOU specify exactly what you want on your plate.
N+1 Problem
The N+1 problem is when your code makes 1 query to get a list of things, then N more queries to get related data for each thing.
Schema
A database schema is the blueprint of your database — which tables exist, what columns they have, what types they are, and how they relate to each other.