Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Mutation instead of @Query
Using @Controller which is for REST, not GraphQL
✗ Incorrect
The @Query() decorator defines a GraphQL query method in NestJS.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Query instead of @Mutation
Using @Get which is for REST controllers
✗ Incorrect
The @Mutation() decorator defines a GraphQL mutation method in NestJS.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Mutation for a query method
Using @Subscribe which is not a valid decorator
✗ Incorrect
To define a query, the method must use the @Query() decorator.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Query instead of @Mutation
Using @Param which is for REST, not GraphQL
✗ Incorrect
The @Mutation() decorator defines the mutation, and @Args() extracts the argument.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Mutation instead of @Query
Using @Param instead of @Args
Omitting the return type
✗ Incorrect
The method is a query, so use @Query(). Arguments come from @Args(), and the return type is string.