0
0
NestJSframework~10 mins

Queries and mutations 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 query in NestJS.

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

@Resolver()
export class SampleResolver {
  @[1]()
  getHello(): string {
    return 'Hello World!';
  }
}
Drag options to blanks, or click blank then click option'
AMutation
BQuery
CSubscription
DController
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Mutation instead of @Query
Using @Controller which is for REST, not GraphQL
2fill in blank
medium

Complete the code to define a GraphQL mutation in NestJS.

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

@Resolver()
export class SampleResolver {
  @[1]()
  createItem(): string {
    return 'Item created';
  }
}
Drag options to blanks, or click blank then click option'
AMutation
BQuery
CSubscription
DGet
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Query instead of @Mutation
Using @Get which is for REST controllers
3fill in blank
hard

Fix the error in the resolver method decorator to correctly define a query.

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

@Resolver()
export class SampleResolver {
  @[1]()
  fetchData(): string {
    return 'Data fetched';
  }
}
Drag options to blanks, or click blank then click option'
AMutation
BSubscribe
CController
DQuery
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Mutation for a query method
Using @Subscribe which is not a valid decorator
4fill in blank
hard

Fill both blanks to define a mutation method with an argument in NestJS GraphQL.

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

@Resolver()
export class SampleResolver {
  @[1]()
  addUser(@[2]('name') name: string): string {
    return `User ${name} added`;
  }
}
Drag options to blanks, or click blank then click option'
AMutation
BQuery
CArgs
DParam
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Query instead of @Mutation
Using @Param which is for REST, not GraphQL
5fill in blank
hard

Fill all three blanks to define a query method with an argument and return type in NestJS GraphQL.

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

@Resolver()
export class SampleResolver {
  @[1]()
  getUser(@[2]('id') id: string): [3] {
    return `User with ID ${id}`;
  }
}
Drag options to blanks, or click blank then click option'
AMutation
BArgs
Cstring
DQuery
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Mutation instead of @Query
Using @Param instead of @Args
Omitting the return type