An Object Type defines the shape of data that the GraphQL API returns. It is a class decorated with @ObjectType() that describes fields and their types for the API response.
Click to reveal answer
beginner
What is an Input Type in NestJS GraphQL and how is it used?
An Input Type is a class decorated with @InputType() used to define the shape of data sent by clients to the API, such as arguments for mutations or queries.
Click to reveal answer
beginner
How do you define a simple Object Type with two fields: id (number) and name (string)?
Use a class with @ObjectType() and decorate fields with @Field(). Example:
@ObjectType()
class User {
@Field()
id: number;
@Field()
name: string;
}
Click to reveal answer
intermediate
Why should Input Types be separate from Object Types in NestJS GraphQL?
Separating Input Types from Object Types helps keep input validation clear and prevents exposing internal fields in API responses. It also avoids circular references.
Click to reveal answer
intermediate
How do you reuse fields between Object Types and Input Types in NestJS?
You can create a base class with shared fields and extend it in both @ObjectType() and @InputType() classes to avoid repeating code.
Click to reveal answer
Which decorator is used to define an Object Type in NestJS GraphQL?
A@ObjectType()
B@InputType()
C@Entity()
D@Controller()
✗ Incorrect
The @ObjectType() decorator marks a class as a GraphQL Object Type for API responses.
What decorator marks a class as an Input Type in NestJS GraphQL?
A@Field()
B@Input()
C@ArgsType()
D@InputType()
✗ Incorrect
The @InputType() decorator defines a class used for input data in GraphQL queries or mutations.
Why should Input Types and Object Types be separate in NestJS GraphQL?
ATo improve database performance
BTo reduce server memory usage
CTo avoid exposing internal fields and circular references
DTo enable REST API compatibility
✗ Incorrect
Separating them helps keep input validation clear and prevents exposing internal data or circular dependencies.
Which decorator is used to define fields inside Object or Input Types?
A@Column()
B@Field()
C@Property()
D@Param()
✗ Incorrect
@Field() marks a property as a GraphQL field in Object or Input Types.
How can you reuse fields between Object Types and Input Types in NestJS?
ABy using inheritance with a shared base class
BBy copying code manually
CBy using interfaces only
DBy using decorators only
✗ Incorrect
Inheritance with a shared base class allows reusing fields in both Object and Input Types.
Explain the difference between Object Types and Input Types in NestJS GraphQL and why both are important.
Think about what data the server sends versus what it receives.
You got /4 concepts.
Describe how to create a reusable set of fields for both Object Types and Input Types in NestJS.
Consider how inheritance works in classes.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using @ObjectType() in NestJS GraphQL?
easy
A. To define the shape of data returned by queries and mutations
B. To define the shape of data clients send to the server
C. To create database entities automatically
D. To handle HTTP request validation
Solution
Step 1: Understand the role of @ObjectType()
This decorator is used to define the structure of data that GraphQL queries and mutations return to clients.
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 A
Quick Check:
ObjectType = output data shape [OK]
Hint: ObjectType decorates output data, InputType decorates input data [OK]
Common Mistakes:
Confusing @ObjectType() with @InputType()
Thinking @ObjectType() creates database tables
Assuming @ObjectType() validates HTTP requests
2. Which of the following is the correct way to define an input type in NestJS GraphQL?
easy
A. @ObjectType() class CreateUserInput { name: string; }
B. @InputType() interface CreateUserInput { name: string; }
C. @InputType() class CreateUserInput { @Field() name: string; }
D. @Input() class CreateUserInput { name: string; }
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 C
Quick Check:
InputType + @Field + class = correct input type [OK]
Hint: Input types need @InputType() and @Field() on class properties [OK]
Common Mistakes:
Using @ObjectType() instead of @InputType()
Using interfaces instead of classes for input types
Omitting @Field() decorator on fields
3. Given the following code, what will be the GraphQL output type for the user query?
@ObjectType()
class User {
@Field()
id: string;
@Field()
name: string;
}
@Resolver()
class UserResolver {
@Query(() => User)
user() {
return { id: '1', name: 'Alice' };
}
}
medium
A. An error because id is not a number
B. A User object with fields id and name
C. A list of User objects
D. Null because no data source is connected
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 B
Quick Check:
Query returns User object with id and name [OK]
Hint: Return object matches @ObjectType class shape for query output [OK]
Common Mistakes:
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
4. Identify the error in this input type definition:
@InputType()
class UpdateUserInput {
name: string;
@Field()
age: number;
}
medium
A. Missing @Field() decorator on the 'name' property
B. Cannot use number type in input types
C. Class must be decorated with @ObjectType() instead
D. Input types cannot have more than one property
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 A
Quick Check:
All input fields need @Field() [OK]
Hint: Every input field needs @Field() decorator [OK]
Common Mistakes:
Forgetting @Field() on some properties
Confusing @InputType() with @ObjectType()
Thinking number types are invalid in input types
5. You want to create a mutation to update a user's profile with optional fields. Which approach correctly defines the input type to allow optional fields in NestJS GraphQL?