Complete the code to define a GraphQL resolver using the code-first approach in NestJS.
import { Resolver, Query } from '@nestjs/graphql'; @Resolver() export class SampleResolver { @Query(() => String) getHello() { return [1]; } }
The query method should return a string value. Using a string literal like 'Hello World!' is correct.
Complete the code to define a GraphQL object type using the code-first approach.
import { ObjectType, Field, Int } from '@nestjs/graphql'; @ObjectType() export class [1] { @Field() name: string; @Field(() => Int) age: number; }
The class name should represent the GraphQL object type. 'User' is a common example.
Fix the error in the code to correctly define a mutation using the code-first approach.
import { Resolver, Mutation, Args } from '@nestjs/graphql'; @Resolver() export class SampleResolver { @Mutation(() => Boolean) async updateName(@Args('name') [1]: string) { // update logic here return true; } }
The argument name in @Args decorator and the parameter name should match exactly and be lowercase.
Fill both blanks to create a GraphQL input type and use it in a mutation.
import { InputType, Field } from '@nestjs/graphql'; @[1]() export class UpdateUserInput { @Field() name: string; } @Resolver() export class UserResolver { @Mutation(() => Boolean) async updateUser(@Args('[2]') input: UpdateUserInput) { // update logic return true; } }
The class should be decorated with @InputType. The argument name in @Args should match the input class name.
Fill all three blanks to define a GraphQL resolver method that returns a list of users with proper decorators.
import { Resolver, Query } from '@nestjs/graphql'; import { User } from './user.model'; @Resolver(() => User) export class UserResolver { @Query(() => [[1]]) async [2]() : Promise<[3][]> { // fetch users logic return []; } }
The query returns a list of User objects. The method name is typically plural like 'users'. The return type is Promise of User array.