A Resolver is a special class in NestJS that handles GraphQL queries and mutations. It connects GraphQL requests to the code that fetches or modifies data.
Click to reveal answer
beginner
How do you define a Query in a NestJS Resolver?
You use the @Query() decorator on a method inside a Resolver class. This method runs when the GraphQL query with the same name is called.
Click to reveal answer
beginner
What decorator is used to create a Mutation in a NestJS Resolver?
The @Mutation() decorator is used on a method inside a Resolver class to handle GraphQL mutations, which change data.
Click to reveal answer
intermediate
Why do Resolvers use dependency injection in NestJS?
Resolvers use dependency injection to get services that handle the actual data logic. This keeps the Resolver focused on GraphQL and clean code separation.
Click to reveal answer
beginner
What is the role of the @Resolver() decorator in NestJS?
The @Resolver() decorator marks a class as a GraphQL Resolver. It can optionally take an object type to link the Resolver to a GraphQL type.
Click to reveal answer
Which decorator defines a GraphQL query method in a NestJS Resolver?
A@Injectable()
B@Mutation()
C@Resolver()
D@Query()
✗ Incorrect
The @Query() decorator marks a method as a GraphQL query handler.
What does the @Resolver() decorator do?
ADefines a GraphQL mutation
BMarks a class as a GraphQL Resolver
CInjects a service into a class
DCreates a GraphQL schema
✗ Incorrect
The @Resolver() decorator marks a class as a GraphQL Resolver in NestJS.
How do Resolvers get access to services in NestJS?
AUsing dependency injection in the constructor
BBy importing them directly
CUsing global variables
DBy calling static methods
✗ Incorrect
Resolvers receive services via dependency injection through their constructor.
Which decorator is used to handle data changes in GraphQL with NestJS?
A@Mutation()
B@Resolver()
C@Query()
D@Controller()
✗ Incorrect
The @Mutation() decorator is used for GraphQL mutations that change data.
What is the main purpose of a Resolver in NestJS GraphQL?
ATo define REST endpoints
BTo manage database connections
CTo connect GraphQL requests to data logic
DTo style the frontend
✗ Incorrect
Resolvers connect GraphQL queries and mutations to the code that fetches or changes data.
Explain how to create a simple Query Resolver method in NestJS and what it does.
Think about how a method is marked to respond to a GraphQL query.
You got /4 concepts.
Describe the role of dependency injection in NestJS Resolvers and why it is useful.
Consider how services are provided to Resolvers without manual creation.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a @Resolver in a NestJS GraphQL application?
easy
A. To handle GraphQL queries and mutations by connecting them to data or actions
B. To define database schemas for the application
C. To manage HTTP routes and middleware
D. To style the frontend components
Solution
Step 1: Understand the role of @Resolver
The @Resolver decorator marks a class that handles GraphQL queries and mutations, linking them to the app's data or business logic.
Step 2: Differentiate from other roles
It does not define database schemas, manage HTTP routes, or style frontend components, which are handled by other parts of the app.
Final Answer:
To handle GraphQL queries and mutations by connecting them to data or actions -> Option A
A. The method returns a number but the return type is declared as String
B. Missing @Mutation decorator
C. The class is missing @Injectable decorator
D. The method name should be capitalized
Solution
Step 1: Check the return type annotation
The @Query(() => String) decorator declares the return type as String.
Step 2: Check the actual return value
The method returns the number 123, which conflicts with the declared String return type.
Final Answer:
The method returns a number but the return type is declared as String -> Option A
Quick Check:
Return type mismatch causes error [OK]
Hint: Return type must match decorator type [OK]
Common Mistakes:
Ignoring return type mismatch
Thinking @Mutation is needed for queries
Assuming @Injectable is required on resolvers
5. You want to create a resolver that returns a list of users filtered by an optional age argument. Which code snippet correctly implements this in NestJS?
D. @Resolver()
export class UserResolver {
@Query(() => [User])
users() {
return users;
}
}
Solution
Step 1: Check the query decorator and return type
The method uses @Query(() => [User]) which correctly indicates it returns a list of users.
Step 2: Verify optional argument handling
The @Args('age', { nullable: true }) age?: number allows the age argument to be optional, filtering users if provided, or returning all users otherwise.
Step 3: Confirm filtering logic
The method filters the users array by age if age is given, otherwise returns all users, matching the requirement.