0
0
NestJSframework~8 mins

Object types and input types in NestJS - Performance & Optimization

Choose your learning style9 modes available
Performance: Object types and input types
MEDIUM IMPACT
This concept affects server response time and client rendering speed by influencing how data is validated and transformed before processing.
Defining GraphQL object and input types for API requests
NestJS
import { ObjectType, Field, InputType } from '@nestjs/graphql';

@ObjectType()
export class User {
  @Field()
  name: string;

  @Field()
  age: number;
  // password field omitted from output type
}

@InputType()
export class CreateUserInput {
  @Field()
  name: string;

  @Field()
  age: number;

  @Field()
  password: string; // only in input type
}
Separating input and output types avoids sending sensitive data to clients and reduces payload size.
📈 Performance GainSmaller payloads improve network speed and reduce client rendering workload.
Defining GraphQL object and input types for API requests
NestJS
import { ObjectType, Field, InputType } from '@nestjs/graphql';

@ObjectType()
export class User {
  @Field()
  name: string;

  @Field()
  age: number;

  @Field()
  password: string; // included in output type
}

@InputType()
export class CreateUserInput {
  @Field()
  name: string;

  @Field()
  age: number;

  @Field()
  password: string;
}
Including sensitive fields like password in output object types causes unnecessary data exposure and larger payloads.
📉 Performance CostIncreases payload size, causing slower network transfer and longer client rendering times.
Performance Comparison
PatternPayload SizeNetwork TransferClient ParsingVerdict
Including sensitive fields in output typesLargeSlowerMore CPU[X] Bad
Separating input and output types properlySmallFasterLess CPU[OK] Good
Rendering Pipeline
Object and input types define the shape of data sent and received. Proper separation reduces data size, which speeds up network transfer and lowers client rendering time.
Network Transfer
Client Parsing
Client Rendering
⚠️ BottleneckNetwork Transfer due to payload size
Core Web Vital Affected
INP
This concept affects server response time and client rendering speed by influencing how data is validated and transformed before processing.
Optimization Tips
1Always separate input types from output object types in GraphQL schemas.
2Exclude sensitive or unnecessary fields from output types to reduce payload size.
3Use input types to validate and accept data without exposing it back to clients.
Performance Quiz - 3 Questions
Test your performance knowledge
Why should sensitive fields like passwords be excluded from GraphQL output object types?
ATo make the code shorter
BTo reduce payload size and avoid exposing sensitive data
CTo increase server CPU usage
DTo make input types larger
DevTools: Network
How to check: Open DevTools, go to Network tab, inspect GraphQL request and response payload sizes.
What to look for: Look for smaller response sizes and faster transfer times indicating efficient object type usage.