0
0
NestJSframework~20 mins

Resolvers in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 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.