0
0
NestJSframework~20 mins

Queries and mutations in NestJS - Practice Problems & Coding Challenges

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