A query is a way to ask the server for data without changing anything. It fetches information and returns it to you.
Click to reveal answer
beginner
What is a mutation in NestJS GraphQL?
A mutation is a request to change data on the server, like adding, updating, or deleting something.
Click to reveal answer
intermediate
How do you define a query method in a NestJS resolver?
Use the @Query() decorator above a method inside a resolver class. This method returns the data requested.
Click to reveal answer
intermediate
How do you define a mutation method in a NestJS resolver?
Use the @Mutation() decorator above a method inside a resolver class. This method performs data changes and returns the result.
Click to reveal answer
beginner
Why should queries not change data and mutations should?
Queries are for reading data only to keep things safe and predictable. Mutations change data because they represent actions that modify the server state.
Click to reveal answer
Which decorator is used to define a query in a NestJS GraphQL resolver?
A@Resolver()
B@Query()
C@Mutation()
D@Controller()
✗ Incorrect
The @Query() decorator marks a method as a GraphQL query resolver.
What does a mutation do in NestJS GraphQL?
AChanges data on the server
BFetches data without changes
CDefines a new GraphQL type
DHandles HTTP requests
✗ Incorrect
Mutations are used to change data on the server, like creating or updating records.
Where do you place the @Mutation() decorator in NestJS?
AAbove a service method
BAbove a controller method
CAbove a resolver method
DAbove a module class
✗ Incorrect
The @Mutation() decorator is used above methods inside a resolver class.
Which of these is true about queries?
AThey fetch data without side effects
BThey modify data
CThey are only used in REST APIs
DThey require authentication always
✗ Incorrect
Queries fetch data and should not cause side effects or data changes.
In NestJS GraphQL, what is the purpose of a resolver?
ATo style the UI components
BTo handle HTTP requests
CTo store data in the database
DTo define how queries and mutations fetch or change data
✗ Incorrect
Resolvers define how GraphQL queries and mutations get or modify data.
Explain how queries and mutations differ in NestJS GraphQL and how you define them in a resolver.
Think about reading vs changing data and the decorators used.
You got /5 concepts.
Describe why it is important to separate queries and mutations in GraphQL and how this helps in building APIs with NestJS.
Consider safety, clarity, and API design benefits.
You got /5 concepts.
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?