A resolver function tells GraphQL how to get the data for a specific field. It connects the query to the actual data source.
Resolver function signature in GraphQL
function resolver(parent, args, context, info) { // return data }
parent: The result from the previous resolver or root object.
args: The arguments passed to the field in the query.
context: Shared data like authentication info or database connections.
info: Information about the execution state of the query.
function getUser(parent, args, context, info) { return context.db.findUserById(args.id); }
const resolver = (parent, args, context) => { return parent.name.toUpperCase(); };
This simple resolver returns a greeting string when the 'hello' query is called.
const resolvers = { Query: { hello: (parent, args, context, info) => { return 'Hello, world!'; } } };
Resolvers run in the order fields are requested in the query.
If a resolver is missing for a field, GraphQL returns the value of that field from the parent object.
Resolver functions connect queries to data sources.
They receive four arguments: parent, args, context, and info.
Resolvers can return data directly or perform logic before returning.
Practice
Solution
Step 1: Understand resolver role
Resolvers are functions that fetch or compute data for a GraphQL query.Step 2: Match purpose to options
Only It connects a GraphQL query to the actual data source and returns data. correctly states that resolvers connect queries to data sources and return data.Final Answer:
It connects a GraphQL query to the actual data source and returns data. -> Option BQuick Check:
Resolver function purpose = Connect query to data [OK]
- Confusing resolvers with schema definitions
- Thinking resolvers format queries
- Assuming resolvers handle authentication only
Solution
Step 1: Recall resolver argument order
The standard order is parent, args, context, info.Step 2: Compare options to standard order
Only function resolver(parent, args, context, info) { ... } matches the correct order exactly.Final Answer:
function resolver(parent, args, context, info) { ... } -> Option CQuick Check:
Resolver args order = parent, args, context, info [OK]
- Mixing up the order of arguments
- Omitting required arguments
- Using incorrect argument names
args = { id: 5 } and parent = null?
function resolver(parent, args, context, info) {
return args.id * 2;
}Solution
Step 1: Identify input values
args.id is 5, parent is null but unused.Step 2: Calculate return value
Return is args.id * 2 = 5 * 2 = 10.Final Answer:
10 -> Option AQuick Check:
args.id * 2 = 10 [OK]
- Confusing parent with args
- Assuming parent affects output here
- Expecting an error due to null parent
const resolver = (args, parent, context, info) => {
return parent.value + args.increment;
}Solution
Step 1: Check argument order
Resolvers must have arguments in order: parent, args, context, info.Step 2: Identify mismatch
This function has args first, then parent, which is incorrect.Final Answer:
The order of arguments is incorrect; parent should be first. -> Option AQuick Check:
Argument order must be parent first [OK]
- Swapping parent and args positions
- Thinking arrow functions are invalid
- Assuming return must be Promise always
Solution
Step 1: Verify argument order and usage
function resolver(parent, args, context, info) { console.log(info.fieldName); return context.db.getUserById(args.id); } uses correct argument order and logs info.fieldName before returning data.Step 2: Check context usage for data fetching
function resolver(parent, args, context, info) { console.log(info.fieldName); return context.db.getUserById(args.id); } correctly calls context.db.getUserById with args.id.Step 3: Review other options for errors
Options B and C have wrong argument order or missing info; D logs after return, so log never runs.Final Answer:
function resolver(parent, args, context, info) { console.log(info.fieldName); return context.db.getUserById(args.id); } -> Option DQuick Check:
Correct signature and usage = function resolver(parent, args, context, info) { console.log(info.fieldName); return context.db.getUserById(args.id); } [OK]
- Logging after return statement
- Incorrect argument order
- Not using context for data access
Ready to prove it?
Pass the test and this pattern moves unknown
