📖 Scenario: You are creating a simple NestJS GraphQL API for a bookstore. You want to let users get book details and add new books.
🎯 Goal: Build a NestJS GraphQL resolver with a query to get a book by its id and a mutation to add a new book with title and author.
📋 What You'll Learn
Create a Book class with id, title, and author fields
Create a query called getBook that takes an id argument and returns the matching Book
Create a mutation called addBook that takes title and author arguments and returns the new Book
Use an in-memory array to store books
💡 Why This Matters
🌍 Real World
Building APIs with NestJS and GraphQL is common for modern web apps that need flexible data fetching and updating.
💼 Career
Understanding queries and mutations in NestJS GraphQL is essential for backend developers working with GraphQL APIs.
Progress0 / 4 steps
1
Create the Book class and initial books array
Create a class called Book with id (number), title (string), and author (string) fields. Then create a variable called books as an array with one book: { id: 1, title: '1984', author: 'George Orwell' }.
NestJS
Hint
Define a TypeScript class with the three fields. Then create an array with one object matching the class.
2
Add a query method to get a book by id
Add a method called getBook inside a resolver class BooksResolver. It should take an id argument of type number and return the book from books with that id. Use @Query(() => Book) decorator on the method.
NestJS
Hint
Use @Resolver and @Query decorators. Use @Args to get the id argument.
3
Add a mutation method to add a new book
Inside the BooksResolver class, add a method called addBook with title and author string arguments. It should create a new Book with a unique id, add it to books, and return the new book. Use @Mutation(() => Book) decorator on the method.
NestJS
Hint
Use @Mutation decorator. Create a new book object with a new id and add it to the books array.
4
Complete the resolver export and imports
Add the necessary imports from @nestjs/graphql at the top: Resolver, Query, Args, Int, and Mutation. Export the BooksResolver class so it can be used in the NestJS module.
NestJS
Hint
Make sure all decorators are imported and the resolver class is exported.
Practice
(1/5)
1. In NestJS GraphQL, which decorator is used to define a method that fetches data without modifying it?
easy
A. @Query
B. @Mutation
C. @ResolveField
D. @Subscription
Solution
Step 1: Understand the purpose of @Query
The @Query decorator marks a method as a GraphQL query, which fetches data without changing it.
Step 2: Differentiate from @Mutation
@Mutation is used for methods that change data, so it is not correct here.
Final Answer:
@Query -> Option A
Quick Check:
Fetch data = @Query [OK]
Hint: Queries fetch data, use @Query decorator [OK]
Common Mistakes:
Confusing @Mutation with @Query
Using @ResolveField for queries
Thinking @Subscription fetches data
2. Which of the following is the correct syntax to define a mutation method named createUser in a NestJS GraphQL resolver?
easy
A. @Mutation\ncreateUser() { /*...*/ }
B. @Query(() => User)\ncreateUser() { /*...*/ }
C. @Mutation(() => User)\ncreateUser() { /*...*/ }
D. @Mutation()\ncreateUser() { /*...*/ }
Solution
Step 1: Check decorator usage with return type
The correct syntax uses @Mutation with a function returning the GraphQL type, e.g., @Mutation(() => User).
Step 2: Verify method name and decorator match
The method name is createUser, and it must be decorated with @Mutation, not @Query.
Final Answer:
@Mutation(() => User)\ncreateUser() { /*...*/ } -> Option C
Quick Check:
Mutation with return type syntax = @Mutation(() => User)\ncreateUser() { /*...*/ } [OK]
Hint: Use @Mutation(() => ReturnType) for mutations [OK]
Common Mistakes:
Omitting the return type function in decorator
Using @Query instead of @Mutation
Adding empty parentheses without return type
3. Given the following NestJS GraphQL resolver code, what will be the output when the getUser query is called?
5. You want to create a NestJS GraphQL mutation that updates a user's email and returns the updated user object. Which of the following resolver method signatures correctly combines input argument and mutation decorator?