0
0
NestJSframework~10 mins

Code-first approach in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a GraphQL resolver using the code-first approach in NestJS.

NestJS
import { Resolver, Query } from '@nestjs/graphql';

@Resolver()
export class SampleResolver {
  @Query(() => String)
  getHello() {
    return [1];
  }
}
Drag options to blanks, or click blank then click option'
A'Hello World!'
BHello World!
Creturn 'Hello World!'
Dconsole.log('Hello World!')
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the string
Using console.log instead of returning a value
2fill in blank
medium

Complete the code to define a GraphQL object type using the code-first approach.

NestJS
import { ObjectType, Field, Int } from '@nestjs/graphql';

@ObjectType()
export class [1] {
  @Field()
  name: string;

  @Field(() => Int)
  age: number;
}
Drag options to blanks, or click blank then click option'
AMutation
BUser
CResolver
DQuery
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Query' or 'Mutation' as object type names
Using decorator names as class names
3fill in blank
hard

Fix the error in the code to correctly define a mutation using the code-first approach.

NestJS
import { Resolver, Mutation, Args } from '@nestjs/graphql';

@Resolver()
export class SampleResolver {
  @Mutation(() => Boolean)
  async updateName(@Args('name') [1]: string) {
    // update logic here
    return true;
  }
}
Drag options to blanks, or click blank then click option'
Aname
BName
CnewName
DupdateName
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase or different names than the @Args string
Mismatching argument names
4fill in blank
hard

Fill both blanks to create a GraphQL input type and use it in a mutation.

NestJS
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;
  }
}
Drag options to blanks, or click blank then click option'
AInputType
BUpdateUserInput
CMutationInput
DUserInput
Attempts:
3 left
💡 Hint
Common Mistakes
Using @ObjectType instead of @InputType
Mismatching argument names in @Args
5fill in blank
hard

Fill all three blanks to define a GraphQL resolver method that returns a list of users with proper decorators.

NestJS
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 [];
  }
}
Drag options to blanks, or click blank then click option'
AUser
Busers
DUserEntity
Attempts:
3 left
💡 Hint
Common Mistakes
Using singular method names for list queries
Mismatching return types and decorator types