Performance: Resolvers
Resolvers impact server response time and client perceived latency by controlling how data is fetched and processed before sending to the client.
async user() { const user = await this.userService.findUserWithPosts(); return user; }
async user() { const user = await this.userService.findUser(); user.posts = await Promise.all(user.postIds.map(id => this.postService.findPostById(id))); return user; }
| Pattern | Server Calls | Latency Impact | Network Payload | Verdict |
|---|---|---|---|---|
| Multiple nested calls per resolver | Many sequential calls | High latency | Larger payload due to delays | [X] Bad |
| Single optimized query per resolver | One call with joins | Low latency | Smaller, timely payload | [OK] Good |
@Resolver in a NestJS GraphQL application?@Resolver decorator marks a class that handles GraphQL queries and mutations, linking them to the app's data or business logic.@Query decorator is used to define query resolver methods.@Query(() => String), which is correct syntax.hello query is called?
@Resolver()
export class HelloResolver {
@Query(() => String)
hello() {
return 'Hi NestJS';
}
}hello method is decorated with @Query(() => String) and returns the string 'Hi NestJS'.hello query is called, it returns the string exactly as coded: 'Hi NestJS'.@Resolver()
export class SampleResolver {
@Query(() => String)
sample() {
return 123;
}
}@Query(() => String) decorator declares the return type as String.age argument. Which code snippet correctly implements this in NestJS?@Query(() => [User]) which correctly indicates it returns a list of users.@Args('age', { nullable: true }) age?: number allows the age argument to be optional, filtering users if provided, or returning all users otherwise.Pass the test and this pattern moves unknown