Challenge - 5 Problems
NestJS Object & Input Types Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this NestJS GraphQL resolver with object types?
Consider this NestJS GraphQL resolver code snippet. What will be the output when the
getUser query is called with id: 1?NestJS
import { Resolver, Query, Args, ObjectType, Field, Int } from '@nestjs/graphql'; @ObjectType() class User { @Field(type => Int) id: number; @Field() name: string; } @Resolver(of => User) export class UserResolver { private users = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]; @Query(returns => User, { nullable: true }) getUser(@Args('id', { type: () => Int }) id: number): User | undefined { return this.users.find(user => user.id === id); } }
Attempts:
2 left
💡 Hint
Think about how the resolver finds the user by id and returns the matching object.
✗ Incorrect
The resolver searches the users array for a user with id 1 and returns the matching object. The GraphQL query then returns this user data in the response.
📝 Syntax
intermediate2:00remaining
Which option correctly defines an input type for NestJS GraphQL mutation?
You want to create an input type for a mutation to create a new user with
name and age. Which option is syntactically correct?Attempts:
2 left
💡 Hint
Input types must use the @InputType decorator with parentheses.
✗ Incorrect
Option D correctly uses @InputType() decorator and defines fields with @Field decorators. Option D uses @ObjectType which is for output types. Option D misses parentheses on @InputType. Option D lacks any decorator.
🔧 Debug
advanced2:00remaining
What error does this NestJS GraphQL input type cause?
Given this input type code, what error will occur when running the NestJS app?
NestJS
import { InputType, Field } from '@nestjs/graphql'; @InputType() export class UpdateUserInput { @Field() name: string; age: number; }
Attempts:
2 left
💡 Hint
All fields in input types must have @Field decorator to be exposed in GraphQL schema.
✗ Incorrect
The 'age' field lacks the @Field decorator, so NestJS GraphQL will throw an error indicating this field is missing the decorator.
❓ state_output
advanced2:00remaining
What is the value of the input object in this NestJS mutation resolver?
In this mutation resolver, what is the value of the
input parameter when the mutation is called with { "name": "Eve", "age": 30 }?NestJS
import { Resolver, Mutation, Args, InputType, Field, Int } from '@nestjs/graphql'; @InputType() class CreateUserInput { @Field() name: string; @Field(type => Int) age: number; } @Resolver() export class UserResolver { @Mutation(returns => Boolean) async createUser(@Args('input') input: CreateUserInput): Promise<boolean> { console.log(input); return true; } }
Attempts:
2 left
💡 Hint
The @Args decorator extracts the input object directly.
✗ Incorrect
The input parameter receives the object passed to the mutation under the 'input' argument, so it contains { name: 'Eve', age: 30 }.
🧠 Conceptual
expert2:00remaining
Which statement about NestJS GraphQL object and input types is correct?
Select the correct statement about the difference between object types and input types in NestJS GraphQL.
Attempts:
2 left
💡 Hint
Think about the purpose of input types versus object types in GraphQL.
✗ Incorrect
Object types describe the data structure returned by GraphQL operations. Input types describe the data structure accepted as input arguments. They use different decorators (@ObjectType vs @InputType).