What if your app could catch data mistakes before they cause bugs or crashes?
Why Object types and input types in NestJS? - Purpose & Use Cases
Imagine building a web app where you manually check every piece of data sent by users to make sure it fits the expected shape before saving it.
You write lots of code to verify each field, convert types, and handle errors everywhere.
This manual checking is slow, repetitive, and easy to forget or mess up.
It leads to bugs, security holes, and confusing errors for users.
Object types and input types in NestJS let you define clear blueprints for data.
They automatically check and transform incoming data, so your code stays clean and safe.
if(typeof data.name !== 'string') throw new Error('Invalid name'); if(typeof data.age !== 'number') throw new Error('Invalid age');
@InputType()
class UserInput {
@Field()
name: string;
@Field()
age: number;
}You can trust your data is correct and focus on building features, not fixing bugs.
When users submit a form to create an account, input types ensure the name is text and age is a number before saving.
Manual data checks are slow and error-prone.
Object and input types define clear data shapes.
They automate validation and keep your app safe and clean.
Practice
@ObjectType() in NestJS GraphQL?Solution
Step 1: Understand the role of
This decorator is used to define the structure of data that GraphQL queries and mutations return to clients.@ObjectType()Step 2: Differentiate from input types
@InputType()is used for data clients send, while@ObjectType()is for data sent back to clients.Final Answer:
To define the shape of data returned by queries and mutations -> Option AQuick Check:
ObjectType = output data shape [OK]
- Confusing @ObjectType() with @InputType()
- Thinking @ObjectType() creates database tables
- Assuming @ObjectType() validates HTTP requests
Solution
Step 1: Identify correct decorator and syntax
Input types require the@InputType()decorator and fields must be decorated with@Field()to expose them to GraphQL.Step 2: Check class vs interface usage
Input types must be classes, not interfaces, to work with decorators in NestJS.Final Answer:
@InputType() class CreateUserInput { @Field() name: string; } -> Option CQuick Check:
InputType + @Field + class = correct input type [OK]
- Using @ObjectType() instead of @InputType()
- Using interfaces instead of classes for input types
- Omitting @Field() decorator on fields
user query?
@ObjectType()
class User {
@Field()
id: string;
@Field()
name: string;
}
@Resolver()
class UserResolver {
@Query(() => User)
user() {
return { id: '1', name: 'Alice' };
}
}Solution
Step 1: Analyze the @ObjectType and @Field decorators
The User class defines two fields, id and name, both exposed to GraphQL via @Field().Step 2: Check the resolver return and query type
The user query returns an object matching User shape with id and name strings, matching the GraphQL type.Final Answer:
A User object with fields id and name -> Option BQuick Check:
Query returns User object with id and name [OK]
- Expecting type errors for string id when GraphQL expects string
- Assuming query returns a list without @Query(() => [User])
- Thinking null is returned without data source
@InputType()
class UpdateUserInput {
name: string;
@Field()
age: number;
}Solution
Step 1: Check decorators on class properties
In GraphQL input types, each field must have the @Field() decorator to be exposed.Step 2: Identify missing decorator
The 'name' property lacks the @Field() decorator, so it won't be part of the GraphQL input type.Final Answer:
Missing @Field() decorator on the 'name' property -> Option AQuick Check:
All input fields need @Field() [OK]
- Forgetting @Field() on some properties
- Confusing @InputType() with @ObjectType()
- Thinking number types are invalid in input types
@InputType()
class UpdateUserInput {
@Field({ nullable: true })
name?: string;
@Field({ nullable: true })
age?: number;
}Solution
Step 1: Understand optional fields in GraphQL input types
To allow optional fields, use{ nullable: true }in @Field() and mark properties optional with '?'.Step 2: Confirm correct decorator usage
Use @InputType() for input data in mutations, not @ObjectType().Final Answer:
Use @Field({ nullable: true }) and optional properties with '?' to allow partial updates -> Option DQuick Check:
Nullable fields + optional properties = optional input fields [OK]
- Using @ObjectType() for input types
- Not marking fields nullable for optional inputs
- Making all fields required and complicating resolver logic
Ready to prove it?
Pass the test and this pattern moves unknown
