Object types and input types help define the shape of data in NestJS GraphQL. They make sure data sent and received is clear and correct.
Object types and input types in NestJS
import { ObjectType, Field, InputType } from '@nestjs/graphql'; @ObjectType() export class ExampleObject { @Field() propertyName: string; } @InputType() export class ExampleInput { @Field() inputProperty: string; }
@ObjectType() marks a class as a GraphQL object type for outputs.
@InputType() marks a class as a GraphQL input type for inputs.
import { ObjectType, Field } from '@nestjs/graphql'; @ObjectType() export class User { @Field() id: string; @Field() name: string; }
import { InputType, Field } from '@nestjs/graphql'; @InputType() export class CreateUserInput { @Field() name: string; @Field() email: string; }
This example shows a simple NestJS GraphQL resolver with an object type Book and an input type CreateBookInput. You can query all books or add a new book using the input type.
import { Resolver, Query, Mutation, Args } from '@nestjs/graphql'; import { ObjectType, Field, InputType } from '@nestjs/graphql'; @ObjectType() class Book { @Field() title: string; @Field() author: string; } @InputType() class CreateBookInput { @Field() title: string; @Field() author: string; } @Resolver() export class BookResolver { private books: Book[] = []; @Query(() => [Book]) getBooks(): Book[] { return this.books; } @Mutation(() => Book) addBook(@Args('input') input: CreateBookInput): Book { const newBook = new Book(); newBook.title = input.title; newBook.author = input.author; this.books.push(newBook); return newBook; } }
Always decorate each property with @Field() to expose it in GraphQL schema.
Input types are used for data coming into the server, object types for data going out.
You can reuse object types as input types if needed, but separate classes keep things clear.
Object types define the shape of data returned by GraphQL queries and mutations.
Input types define the shape of data clients send to the server.
Use decorators @ObjectType() and @InputType() to create these types in NestJS.
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
