0
0
NestJSframework~8 mins

Queries and mutations in NestJS - Performance & Optimization

Choose your learning style9 modes available
Performance: Queries and mutations
MEDIUM IMPACT
This concept affects the speed of data fetching and updating on the client side, impacting how fast the UI responds and renders new data.
Fetching data with GraphQL queries and updating data with mutations in NestJS
NestJS
import { Resolver, Query, Mutation, Args } from '@nestjs/graphql';

@Resolver()
export class UserResolver {
  @Query(() => [User])
  async users(@Args('limit', { nullable: true }) limit?: number, @Args('offset', { nullable: true }) offset?: number) {
    // Fetch users with pagination
    return this.userService.findPaginated(limit ?? 10, offset ?? 0);
  }

  @Mutation(() => User)
  async updateUser(@Args('id') id: string, @Args('input') input: UpdateUserInput) {
    // Validate input and batch updates if possible
    return this.userService.update(id, input);
  }
}
Pagination reduces data size per request improving load speed. Validations and batching reduce server calls and improve mutation responsiveness.
📈 Performance GainReduces data transfer size; lowers server processing time; improves INP by faster query and mutation responses.
Fetching data with GraphQL queries and updating data with mutations in NestJS
NestJS
import { Resolver, Query, Mutation, Args } from '@nestjs/graphql';

@Resolver()
export class UserResolver {
  @Query(() => [User])
  async users() {
    // Fetch all users without pagination or filtering
    return this.userService.findAll();
  }

  @Mutation(() => User)
  async updateUser(@Args('id') id: string, @Args('input') input: UpdateUserInput) {
    // Update user and return updated user
    return this.userService.update(id, input);
  }
}
Fetching all users without pagination or filtering causes large data loads, slowing response and rendering. Mutation updates without validation or batching can cause multiple slow server calls.
📉 Performance CostBlocks UI rendering until full data loads; triggers multiple server round-trips; increases INP due to slow responses.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Fetch all data without paginationHigh (large data causes many DOM nodes)Multiple (each data update triggers reflow)High (large repaint area)[X] Bad
Fetch data with pagination and filteringLow (smaller data set)Single or few (batched updates)Low (smaller repaint area)[OK] Good
Rendering Pipeline
Queries and mutations trigger network requests that affect the browser's rendering pipeline by delaying data availability for rendering and interaction updates.
Network
JavaScript Execution
Layout
Paint
⚠️ BottleneckNetwork latency and server processing time delay data arrival, blocking JavaScript updates and subsequent layout and paint.
Core Web Vital Affected
INP
This concept affects the speed of data fetching and updating on the client side, impacting how fast the UI responds and renders new data.
Optimization Tips
1Always paginate or filter queries to limit data size.
2Batch mutations when possible to reduce network calls.
3Validate and optimize input to avoid unnecessary server processing.
Performance Quiz - 3 Questions
Test your performance knowledge
What is a main performance benefit of using pagination in GraphQL queries?
AIncreases server load by making more requests
BDelays UI updates by batching data
CReduces data size per request, speeding up response and rendering
DTriggers more reflows in the browser
DevTools: Network and Performance panels
How to check: Open DevTools, go to Network panel, observe query and mutation request sizes and timings. Then use Performance panel to record interaction and see delays in scripting and rendering.
What to look for: Look for large payload sizes, long server response times, and scripting delays that block UI updates indicating slow queries or mutations.