Resolvers tell GraphQL how to get the data for each part of a query. Organizing them well keeps your code clean and easy to understand.
Resolver organization in GraphQL
Introduction
Syntax
GraphQL
const resolvers = { Query: { fieldName: (parent, args, context, info) => { // return data } }, Mutation: { mutationName: (parent, args, context, info) => { // perform action and return result } }, TypeName: { fieldName: (parent, args, context, info) => { // resolve nested field } } };
Resolvers are grouped by operation type: Query, Mutation, and custom types.
Each resolver is a function that returns the data for its field.
Examples
hello that returns a string.GraphQL
const resolvers = { Query: { hello: () => 'Hello world!' } };
GraphQL
const resolvers = { Mutation: { addUser: (parent, args) => { // add user logic return { id: 1, name: args.name }; } } };
GraphQL
const resolvers = { User: { fullName: (parent) => `${parent.firstName} ${parent.lastName}` } };
Sample Program
This example sets up a simple GraphQL server with one Query field greeting. The resolver returns a greeting string.
GraphQL
const { ApolloServer, gql } = require('apollo-server'); const typeDefs = gql` type Query { greeting: String } `; const resolvers = { Query: { greeting: () => 'Hello from resolver!' } }; const server = new ApolloServer({ typeDefs, resolvers }); server.listen().then(({ url }) => { console.log(`Server ready at ${url}`); });
Important Notes
Keep resolvers small and focused on one task.
Use separate files or folders to organize resolvers for large projects.
Resolvers receive four arguments: parent, args, context, and info.
Summary
Resolvers connect schema fields to data sources.
Organize resolvers by Query, Mutation, and type for clarity.
Good organization helps maintain and scale your GraphQL server.
Practice
1. What is the main purpose of resolvers in a GraphQL server?
easy
Solution
Step 1: Understand the role of resolvers
Resolvers are functions that fetch or compute the data for each field in the GraphQL schema.Step 2: Identify what resolvers connect
Resolvers connect the schema fields to the actual data sources like databases or APIs.Final Answer:
To connect schema fields to the actual data sources -> Option AQuick Check:
Resolvers = Connect schema to data [OK]
Hint: Resolvers link schema fields directly to data sources [OK]
Common Mistakes:
- Confusing resolvers with schema definitions
- Thinking resolvers handle UI or styling
- Assuming resolvers manage client state
2. Which of the following is the correct way to organize resolvers in a GraphQL server?
easy
Solution
Step 1: Recognize common resolver organization
Resolvers are typically grouped by operation types: Query, Mutation, and by object types for clarity.Step 2: Evaluate the options
Grouping by Query, Mutation, and types helps maintain and scale the server easily.Final Answer:
Group resolvers by Query, Mutation, and type names -> Option AQuick Check:
Organize resolvers by operation type [OK]
Hint: Group resolvers by Query, Mutation, and types for clarity [OK]
Common Mistakes:
- Putting all resolvers in one list without grouping
- Mixing resolver code inside schema files
- Using a single resolver for all fields
3. Given this resolver structure:
const resolvers = {
Query: {
user: () => ({ id: 1, name: "Alice" })
},
User: {
fullName: (parent) => parent.name + " Smith"
}
};
What will be the result of querying { user { fullName } }?medium
Solution
Step 1: Understand the Query resolver
The Query resolver returns a user object with id 1 and name "Alice".Step 2: Understand the User type resolver
The User resolver for fullName appends " Smith" to the parent's name, resulting in "Alice Smith".Final Answer:
{ "user": { "fullName": "Alice Smith" } } -> Option DQuick Check:
fullName = parent.name + " Smith" [OK]
Hint: User resolver uses parent data to build fullName [OK]
Common Mistakes:
- Forgetting to use parent argument in field resolver
- Expecting fullName to be directly in Query resolver
- Assuming missing resolver causes error
4. Consider this resolver code snippet:
const resolvers = {
Query: {
posts: () => fetchPosts()
},
Post: {
author: () => fetchAuthor()
}
};
What is the main problem with this resolver organization?medium
Solution
Step 1: Analyze Post.author resolver
The author resolver does not use the parent post argument, so it cannot fetch the correct author for each post.Step 2: Understand resolver context
Field resolvers should use the parent argument to access related data; ignoring it causes wrong or unrelated data.Final Answer:
The Post.author resolver ignores the parent post and fetches unrelated author data -> Option BQuick Check:
Use parent argument in nested resolvers [OK]
Hint: Nested resolvers must use parent to access related data [OK]
Common Mistakes:
- Ignoring parent argument in nested resolvers
- Misplacing Query resolvers inside Mutation
- Assuming all resolvers must be async
5. You want to organize resolvers for a GraphQL server with Queries, Mutations, and a custom type
Book. Which structure below best supports maintainability and scalability?hard
Solution
Step 1: Identify best practice for resolver organization
Grouping resolvers by operation type (Query, Mutation) and by custom types (Book) keeps code clear and modular.Step 2: Compare options
const resolvers = { Query: {...}, Mutation: {...}, Book: {...} }; clearly separates concerns and allows easy updates and scaling.Final Answer:
const resolvers = { Query: {...}, Mutation: {...}, Book: {...} }; -> Option CQuick Check:
Separate resolvers by operation and type [OK]
Hint: Group resolvers by Query, Mutation, and types for best structure [OK]
Common Mistakes:
- Putting all resolvers in one object without grouping
- Omitting resolvers for custom types
- Ignoring Mutation resolvers
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
