Performance: Object types and input types
This concept affects server response time and client rendering speed by influencing how data is validated and transformed before processing.
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 }
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; }
| Pattern | Payload Size | Network Transfer | Client Parsing | Verdict |
|---|---|---|---|---|
| Including sensitive fields in output types | Large | Slower | More CPU | [X] Bad |
| Separating input and output types properly | Small | Faster | Less CPU | [OK] Good |
@ObjectType() in NestJS GraphQL?@ObjectType()@InputType() is used for data clients send, while @ObjectType() is for data sent back to clients.@InputType() decorator and fields must be decorated with @Field() to expose them to GraphQL.user query?
@ObjectType()
class User {
@Field()
id: string;
@Field()
name: string;
}
@Resolver()
class UserResolver {
@Query(() => User)
user() {
return { id: '1', name: 'Alice' };
}
}@InputType()
class UpdateUserInput {
name: string;
@Field()
age: number;
}@InputType()
class UpdateUserInput {
@Field({ nullable: true })
name?: string;
@Field({ nullable: true })
age?: number;
}{ nullable: true } in @Field() and mark properties optional with '?'.Pass the test and this pattern moves unknown