Bird
Raised Fist0
NestJSframework~20 mins

Queries and mutations in NestJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
NestJS GraphQL Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this NestJS GraphQL query resolver?
Consider this NestJS GraphQL query resolver code. What will be the output when the query getGreeting is called?
NestJS
import { Query, Resolver } from '@nestjs/graphql';

@Resolver()
export class GreetingResolver {
  @Query(() => String)
  getGreeting(): string {
    return 'Hello, NestJS!';
  }
}
A"Hello, NestJS!"
Bundefined
Cnull
DError: Missing return type
Attempts:
2 left
💡 Hint
Look at the return value inside the getGreeting method.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a mutation to add a user in NestJS GraphQL?
You want to create a mutation named addUser that accepts a name string and returns a boolean. Which code snippet is correct?
A
@Mutation(() => Boolean)
addUser(@Args('name') name: string): boolean {
  return true;
}
B
@Mutation(() => Boolean)
addUser(name: string): boolean {
  return true;
}
C
@Mutation()
addUser(name: string): boolean {
  return true;
}
D
@Query(() => Boolean)
addUser(@Args('name') name: string): boolean {
  return true;
}
Attempts:
2 left
💡 Hint
Mutations use the @Mutation decorator and arguments must be decorated with @Args.
❓ state_output
advanced
2:00remaining
What is the value of userCount after these mutations?
Given this NestJS resolver code, what is the value of userCount after calling addUser twice?
NestJS
import { Mutation, Resolver } from '@nestjs/graphql';

@Resolver()
export class UserResolver {
  private userCount = 0;

  @Mutation(() => Number)
  addUser(): number {
    this.userCount += 1;
    return this.userCount;
  }
}
AError: userCount is private
B1
C2
D0
Attempts:
2 left
💡 Hint
Each call to addUser increases userCount by 1 and returns it.
🔧 Debug
advanced
2:00remaining
What error does this NestJS GraphQL mutation code produce?
This mutation is intended to update a user's email but causes an error. What error is raised?
NestJS
import { Mutation, Args, Resolver } from '@nestjs/graphql';

@Resolver()
export class UserResolver {
  @Mutation(() => Boolean)
  updateEmail(@Args('email') email: string): boolean {
    this.email = email;
    return true;
  }
}
ANo error, returns true
BSyntaxError: Unexpected token
CReferenceError: email is not defined
DTypeScript error: Property 'email' does not exist on type 'UserResolver'
Attempts:
2 left
💡 Hint
Check if 'this.email' is defined in the class.
🧠 Conceptual
expert
2:00remaining
Which statement best describes the difference between queries and mutations in NestJS GraphQL?
Choose the most accurate description of queries and mutations in NestJS GraphQL.
AQueries modify data; mutations only fetch data without side effects.
BQueries fetch data without side effects; mutations modify data and may cause side effects.
CBoth queries and mutations only fetch data but differ in syntax.
DMutations are faster than queries because they run on the server.
Attempts:
2 left
💡 Hint
Think about what each operation does to data.

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

  1. Step 1: Understand the purpose of @Query

    The @Query decorator marks a method as a GraphQL query, which fetches data without changing it.
  2. Step 2: Differentiate from @Mutation

    @Mutation is used for methods that change data, so it is not correct here.
  3. Final Answer:

    @Query -> Option A
  4. 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

  1. Step 1: Check decorator usage with return type

    The correct syntax uses @Mutation with a function returning the GraphQL type, e.g., @Mutation(() => User).
  2. Step 2: Verify method name and decorator match

    The method name is createUser, and it must be decorated with @Mutation, not @Query.
  3. Final Answer:

    @Mutation(() => User)\ncreateUser() { /*...*/ } -> Option C
  4. 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?
@Query(() => String)
getUser() {
  return 'Alice';
}
medium
A. Error: Return type mismatch
B. "Alice"
C. null
D. undefined

Solution

  1. Step 1: Analyze the @Query return type and method

    The method getUser returns the string 'Alice' and is decorated with @Query(() => String), matching the return type.
  2. Step 2: Determine the GraphQL response

    GraphQL will return the string value "Alice" as the query result.
  3. Final Answer:

    "Alice" -> Option B
  4. Quick Check:

    Matching return type returns value correctly [OK]
Hint: Return value matches @Query type, so output is that value [OK]
Common Mistakes:
  • Expecting an error due to missing async
  • Confusing return value with null or undefined
  • Thinking GraphQL wraps string differently
4. Identify the error in this NestJS GraphQL mutation resolver code:
@Mutation(() => Boolean)
async updateUser() {
  this.userService.update();
  return true;
}
medium
A. Incorrect return type Boolean instead of boolean
B. updateUser method must return void
C. Mutation should not be async
D. Missing await before userService.update()

Solution

  1. Step 1: Check async method and service call

    The method is async but calls userService.update() without await, so the update may not complete before returning.
  2. Step 2: Understand the impact of missing await

    Without await, the mutation returns true immediately, possibly before update finishes, causing unexpected behavior.
  3. Final Answer:

    Missing await before userService.update() -> Option D
  4. Quick Check:

    Async method must await async calls [OK]
Hint: Always await async calls inside async mutations [OK]
Common Mistakes:
  • Ignoring missing await in async methods
  • Confusing Boolean with boolean types
  • Thinking async is not allowed in mutations
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?
hard
A. @Mutation(() => User) updateEmail(@Args('email') email: string) { return this.userService.updateEmail(email); }
B. @Query(() => User) updateEmail(@Args('email') email: string) { return this.userService.updateEmail(email); }
C. @Mutation(() => User) updateEmail(email: string) { return this.userService.updateEmail(email); }
D. @Mutation(() => User) updateEmail(@Args() email: string) { return this.userService.updateEmail(email); }

Solution

  1. Step 1: Confirm correct decorator for data change

    Since this method updates data, it must use @Mutation with the return type User.
  2. Step 2: Check argument decorator usage

    The argument email is correctly decorated with @Args('email') to extract the argument from GraphQL input.
  3. Step 3: Verify method signature correctness

    @Mutation(() => User) updateEmail(@Args('email') email: string) { return this.userService.updateEmail(email); } correctly combines @Mutation, return type, and @Args usage.
  4. Final Answer:

    @Mutation(() => User) updateEmail(@Args('email') email: string) { return this.userService.updateEmail(email); } -> Option A
  5. Quick Check:

    Mutation with @Args('email') and return type = @Mutation(() => User) updateEmail(@Args('email') email: string) { return this.userService.updateEmail(email); } [OK]
Hint: Use @Mutation with @Args('argName') for input arguments [OK]
Common Mistakes:
  • Using @Query for mutations
  • Omitting @Args decorator
  • Using @Args() without argument name
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard