NestJS Research & Evidence-Based Practice 5 — Questions and Answers
Question 1: When researching NestJS WebSocket support, which gateway decorator registers a class as a WebSocket server listening on a specific namespace?
- @WebSocketServer()
- @SubscribeMessage()
- @WebSocketGateway() (Correct answer)
- @MessageBody()
Correct answer: @WebSocketGateway()
@WebSocketGateway() marks a class as a WebSocket gateway and optionally accepts a port or namespace configuration object.
Question 2: Based on NestJS logging documentation, which built-in logger method should be used to record errors with optional stack trace context?
- Logger.warn()
- Logger.error() (Correct answer)
- Logger.fatal()
- Logger.debug()
Correct answer: Logger.error()
Logger.error() accepts a message and an optional stack trace string as a second argument, making it appropriate for exception logging.
Question 3: Evidence from NestJS GraphQL documentation shows that which decorator defines a resolver method that handles a query returning a list of objects?
- @Mutation()
- @Query() (Correct answer)
- @ResolveField()
- @Subscription()
Correct answer: @Query()
@Query() marks a resolver method as handling GraphQL queries, including those that return arrays of typed objects.
Question 4: Based on NestJS research, which lifecycle hook is the correct place to perform cleanup tasks like closing database connections when the application shuts down?
- onModuleInit()
- onApplicationBootstrap()
- beforeApplicationShutdown()
- onModuleDestroy() (Correct answer)
Correct answer: onModuleDestroy()
onModuleDestroy() is called when a module is being destroyed during application shutdown, making it the right place for resource cleanup.
Question 5: Research into NestJS queue processing shows that which package integrates BullMQ for background job processing?
- @nestjs/schedule
- @nestjs/bull
- @nestjs/bullmq (Correct answer)
- @nestjs/jobs
Correct answer: @nestjs/bullmq
@nestjs/bullmq is the official NestJS package that wraps BullMQ for type-safe, Redis-backed background job queue processing.
Question 6: When studying NestJS validation evidence, which class-validator decorator ensures a DTO string property is not empty and has a minimum length?
- @IsString() combined with @MinLength() (Correct answer)
- @IsNotEmpty() combined with @Length()
- @IsAlpha() combined with @MinLength()
- @IsDefined() combined with @IsString()
Correct answer: @IsString() combined with @MinLength()
Combining @IsString() and @MinLength(n) on a DTO property validates that the value is a string and meets the minimum character count requirement.
Question 7: Based on NestJS documentation research, which approach allows a custom decorator to access route handler metadata set with @SetMetadata()?
- Using ConfigService inside the decorator
- Using Reflector.get() inside a guard or interceptor (Correct answer)
- Using ExecutionContext.getArgs() directly
- Using the @Inject() decorator with a metadata token
Correct answer: Using Reflector.get() inside a guard or interceptor
Reflector.get(metadataKey, handler) retrieves custom metadata set on a route with @SetMetadata(), typically used inside guards to implement role-based access.
When researching NestJS WebSocket support, which gateway decorator registers a class as a WebSocket server listening on a specific namespace?