📖 Scenario: You are creating a small NestJS GraphQL API for a bookstore. You want to provide a way for clients to get book details by their ID.
🎯 Goal: Build a GraphQL resolver in NestJS that returns a book object with id, title, and author fields when queried by id.
📋 What You'll Learn
Create a simple array of book objects with exact fields and values
Add a variable to hold the book ID to search for
Write a resolver method that returns the book matching the given ID
Complete the resolver class with the proper decorator and export
💡 Why This Matters
🌍 Real World
GraphQL resolvers in NestJS are used to handle client queries and return data from databases or other sources.
💼 Career
Understanding resolvers is essential for backend developers working with NestJS and GraphQL APIs.
Progress0 / 4 steps
1
Create the books data array
Create a constant array called books with these exact objects: { id: '1', title: '1984', author: 'George Orwell' }, { id: '2', title: 'Brave New World', author: 'Aldous Huxley' }, and { id: '3', title: 'Fahrenheit 451', author: 'Ray Bradbury' }.
NestJS
Hint
Use const books = [ ... ] with exact objects inside.
2
Add the book ID variable
Create a constant called bookId and set it to the string '2' to represent the book to find.
NestJS
Hint
Use const bookId = '2'; exactly.
3
Write the resolver method to find the book
Create a method called getBookById that returns the book object from books where id matches bookId. Use find on the books array.
NestJS
Hint
Use books.find(book => book.id === bookId) inside the function.
4
Complete the resolver class with decorator
Create a class called BookResolver decorated with @Resolver(). Inside it, add a method book decorated with @Query(() => Object) that calls and returns getBookById(). Export the class.
NestJS
Hint
Use @Resolver() on the class and @Query(() => Object) on the method.
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.