0
0
NestJSframework~10 mins

Object types and input types in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Object types and input types
Define ObjectType class
Define InputType class
Use ObjectType for output
Use InputType for input validation
Resolver methods use InputType as args
Resolver methods return ObjectType instances
This flow shows how NestJS GraphQL ObjectType and InputType classes are defined and used in resolvers for output and input respectively.
Execution Sample
NestJS
import { ObjectType, Field, InputType } from '@nestjs/graphql';

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

@InputType()
class CreateUserInput {
  @Field()
  name: string;
}
Defines a User object type for output and a CreateUserInput input type for input validation.
Execution Table
StepActionClass CreatedDecorator UsedPurpose
1Define User classUser@ObjectTypeOutput type for GraphQL schema
2Add name field to UserUser@FieldExpose name in GraphQL output
3Define CreateUserInput classCreateUserInput@InputTypeInput type for GraphQL schema
4Add name field to CreateUserInputCreateUserInput@FieldExpose name in GraphQL input
5Use CreateUserInput in resolver argumentN/AN/AValidate input data
6Return User instance from resolverN/AN/ASend output data
7ExitN/AN/AAll types defined and used correctly
💡 All object and input types are defined and used properly in resolver methods.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
User classundefinedDefined with @ObjectTypeDefinedDefined with @ObjectType and @Field
CreateUserInput classundefinedundefinedDefined with @InputTypeDefined with @InputType and @Field
Key Moments - 2 Insights
Why do we use @ObjectType for User but @InputType for CreateUserInput?
Because @ObjectType marks classes used for output in GraphQL responses, while @InputType marks classes used for input validation in GraphQL queries or mutations, as shown in steps 1 and 3 of the execution_table.
Can we use the same class for both input and output?
No, NestJS recommends separate classes for input and output to clearly separate validation and response shapes, as seen by separate User and CreateUserInput classes in steps 1 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which decorator is used to define the output type class?
A@InputType
B@ObjectType
C@Field
D@Resolver
💡 Hint
Check Step 1 in the execution_table where User class is defined.
At which step is the input type class created?
AStep 1
BStep 5
CStep 3
DStep 6
💡 Hint
Look at the execution_table row describing CreateUserInput class creation.
If we add a new field to CreateUserInput, which decorator must we use?
A@Field
B@InputType
C@ObjectType
D@Resolver
💡 Hint
Fields inside both ObjectType and InputType classes use @Field decorator as shown in steps 2 and 4.
Concept Snapshot
NestJS GraphQL uses @ObjectType to define output data shapes.
Use @InputType to define input data shapes for validation.
Fields inside these classes use @Field decorator.
Resolvers accept InputType classes as arguments and return ObjectType instances.
Separate classes keep input and output clear and safe.
Full Transcript
In NestJS GraphQL, we define classes to describe the shape of data sent and received. We use @ObjectType to mark classes that represent output data, like what the server sends back. We use @InputType to mark classes that represent input data, like what the client sends in queries or mutations. Each field inside these classes uses the @Field decorator to expose it in the GraphQL schema. In the example, the User class is an ObjectType with a name field, and CreateUserInput is an InputType with a name field. Resolver methods take CreateUserInput as input and return User instances. This separation helps keep input validation and output clear and organized.