Bird
Raised Fist0
NestJSframework~20 mins

Resolvers in NestJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Resolver Mastery
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 GraphQL resolver method?

Consider this NestJS GraphQL resolver method:

@Query(() => String)
getGreeting() {
  return `Hello, NestJS!`;
}

What will be the response when this query is called?

NestJS
@Query(() => String)
getGreeting() {
  return `Hello, NestJS!`;
}
A"getGreeting"
Bnull
CAn error because of missing async
D"Hello, NestJS!"
Attempts:
2 left
💡 Hint

Look at the return value of the method and its type.

❓ state_output
intermediate
2:00remaining
What value does this resolver return after state change?

Given this resolver snippet:

@Resolver()
export class CounterResolver {
  private count = 0;

  @Query(() => Number)
  getCount() {
    return this.count;
  }

  @Mutation(() => Number)
  increment() {
    this.count++;
    return this.count;
  }
}

If the increment mutation is called twice, what will getCount return?

NestJS
@Resolver()
export class CounterResolver {
  private count = 0;

  @Query(() => Number)
  getCount() {
    return this.count;
  }

  @Mutation(() => Number)
  increment() {
    this.count++;
    return this.count;
  }
}
A1
B0
C2
Dundefined
Attempts:
2 left
💡 Hint

Think about how many times the count is increased and what the initial value is.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a resolver method with arguments?

Which of the following resolver methods correctly accepts an argument id of type string and returns it?

A
@Query(() => String)
getById(@Args('id') id: string) {
  return id;
}
B
@Query(() => String)
getById(id: string) {
  return id;
}
C
@Query(() => String)
getById(@Arg('id') id: string) {
  return id;
}
D
@Query(() => String)
getById(@Args id: string) {
  return id;
}
Attempts:
2 left
💡 Hint

Remember the NestJS decorator to extract GraphQL arguments.

🔧 Debug
advanced
2:00remaining
What error does this resolver code produce?

Consider this resolver code snippet:

@Query(() => String)
async fetchData() {
  const data = await this.service.getData();
  return data.value;
}

If this.service.getData() returns null, what error will occur?

NestJS
@Query(() => String)
async fetchData() {
  const data = await this.service.getData();
  return data.value;
}
ASyntaxError: Unexpected token 'await'
BTypeError: Cannot read property 'value' of null
CReferenceError: service is not defined
DNo error, returns undefined
Attempts:
2 left
💡 Hint

What happens if you try to access a property on null?

🧠 Conceptual
expert
2:00remaining
Which statement best describes resolver execution order in NestJS GraphQL?

In NestJS GraphQL, when a query requests multiple fields resolved by different resolver methods, how does NestJS execute these resolvers?

AResolvers execute in parallel for fields at the same level, but nested fields wait for parent resolvers.
BAll resolvers execute sequentially regardless of nesting or field level.
CResolvers execute strictly one after another in the order they are defined in the code.
DResolvers execute randomly without any predictable order.
Attempts:
2 left
💡 Hint

Think about how GraphQL resolves fields and nested fields.

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

  1. 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.
  2. 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.
  3. Final Answer:

    To handle GraphQL queries and mutations by connecting them to data or actions -> Option A
  4. Quick Check:

    Resolver purpose = handle GraphQL queries/mutations [OK]
Hint: Resolvers link GraphQL requests to your app's data/actions [OK]
Common Mistakes:
  • Confusing resolvers with database schema definitions
  • Thinking resolvers manage HTTP routes
  • Assuming resolvers handle frontend styling
2. Which of the following is the correct way to define a GraphQL query resolver method in NestJS?
easy
A. @Controller() getHello() { return 'Hello World'; }
B. @Mutation(() => String) getHello() { return 'Hello World'; }
C. @Query(() => String) getHello() { return 'Hello World'; }
D. @Resolver(() => String) getHello() { return 'Hello World'; }

Solution

  1. Step 1: Identify the decorator for queries

    In NestJS GraphQL, @Query decorator is used to define query resolver methods.
  2. Step 2: Check method syntax

    The method returns a string and is decorated with @Query(() => String), which is correct syntax.
  3. Final Answer:

    @Query(() => String) getHello() { return 'Hello World'; } -> Option C
  4. Quick Check:

    Query method uses @Query decorator [OK]
Hint: Use @Query for queries, @Mutation for mutations [OK]
Common Mistakes:
  • Using @Mutation instead of @Query for queries
  • Using @Resolver on methods instead of classes
  • Using @Controller which is for REST, not GraphQL
3. Given this resolver code, what will be the output when the hello query is called?
@Resolver()
export class HelloResolver {
  @Query(() => String)
  hello() {
    return 'Hi NestJS';
  }
}
medium
A. Error: Missing return type
B. "Hello World"
C. null
D. "Hi NestJS"

Solution

  1. Step 1: Analyze the resolver method

    The hello method is decorated with @Query(() => String) and returns the string 'Hi NestJS'.
  2. Step 2: Determine the query output

    When the hello query is called, it returns the string exactly as coded: 'Hi NestJS'.
  3. Final Answer:

    "Hi NestJS" -> Option D
  4. Quick Check:

    Query returns 'Hi NestJS' string [OK]
Hint: Return value inside @Query method is the output [OK]
Common Mistakes:
  • Expecting 'Hello World' instead of 'Hi NestJS'
  • Thinking a return type error occurs
  • Assuming null is returned by default
4. Identify the error in this NestJS resolver code:
@Resolver()
export class SampleResolver {
  @Query(() => String)
  sample() {
    return 123;
  }
}
medium
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

  1. Step 1: Check the return type annotation

    The @Query(() => String) decorator declares the return type as String.
  2. Step 2: Check the actual return value

    The method returns the number 123, which conflicts with the declared String return type.
  3. Final Answer:

    The method returns a number but the return type is declared as String -> Option A
  4. 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?
hard
A. @Resolver() export class UserResolver { @Mutation(() => [User]) users(@Args('age') age: number) { return users.filter(u => u.age === age); } }
B. @Resolver() export class UserResolver { @Query(() => [User]) users(@Args('age', { nullable: true }) age?: number) { return age ? users.filter(u => u.age === age) : users; } }
C. @Resolver() export class UserResolver { @Query(() => User) users(@Args('age') age: number) { return users.find(u => u.age === age); } }
D. @Resolver() export class UserResolver { @Query(() => [User]) users() { return users; } }

Solution

  1. Step 1: Check the query decorator and return type

    The method uses @Query(() => [User]) which correctly indicates it returns a list of users.
  2. 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.
  3. Step 3: Confirm filtering logic

    The method filters the users array by age if age is given, otherwise returns all users, matching the requirement.
  4. Final Answer:

    @Resolver() export class UserResolver { @Query(() => [User]) users(@Args('age', { nullable: true }) age?: number) { return age ? users.filter(u => u.age === age) : users; } } -> Option B
  5. Quick Check:

    @Resolver() export class UserResolver { @Query(() => [User]) users(@Args('age', { nullable: true }) age?: number) { return age ? users.filter(u => u.age === age) : users; } } [OK]
Hint: Use nullable args for optional filters in @Query [OK]
Common Mistakes:
  • Using @Mutation instead of @Query for fetching data
  • Returning single User instead of list
  • Not making argument optional when needed
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