Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a basic GraphQL resolver method.
NestJS
import { Resolver, Query } from '@nestjs/graphql'; @Resolver() export class SampleResolver { @Query(() => String) hello() { return [1]; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the string
Using console.log instead of return
Returning a non-string value
✗ Incorrect
The resolver method should return a string value. The correct syntax is to return a string literal like 'Hello World!'.
2fill in blank
mediumComplete the code to inject a service into the resolver constructor.
NestJS
import { Resolver } from '@nestjs/graphql'; import { SampleService } from './sample.service'; @Resolver() export class SampleResolver { constructor(private readonly [1]: SampleService) {} }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class name with uppercase first letter as variable
Using underscores in variable names
Using generic names like 'service'
✗ Incorrect
The injected service should be named in camelCase, typically matching the service class name but starting with a lowercase letter.
3fill in blank
hardFix the error in the resolver method decorator to correctly define a mutation.
NestJS
import { Resolver, [1] } from '@nestjs/graphql'; @Resolver() export class SampleResolver { @[1](() => Boolean) async updateData() { return true; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Query instead of @Mutation
Using @Subscription for mutations
Forgetting to import the decorator
✗ Incorrect
To define a mutation in a GraphQL resolver, use the @Mutation decorator imported from '@nestjs/graphql'.
4fill in blank
hardFill both blanks to define a resolver method that accepts an argument.
NestJS
import { Resolver, Query, Args } from '@nestjs/graphql'; @Resolver() export class SampleResolver { @Query(() => String) greet(@[1]('name') [2]: string) { return `Hello, ${name}!`; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Arg instead of @Args
Mismatching argument and parameter names
Forgetting the decorator
✗ Incorrect
The @Args decorator is used to get arguments from the query, and the parameter name should match the argument name.
5fill in blank
hardFill all three blanks to create a resolver method that returns a list of items filtered by a minimum value argument.
NestJS
import { Resolver, Query, Args } from '@nestjs/graphql'; @Resolver() export class SampleResolver { @Query(() => [Number]) getNumbers(@[1]('min') [2]: number) { const numbers = [1, 5, 10, 20]; return numbers.filter(n => n [3] min); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>=' in filter
Mismatching argument and parameter names
Forgetting the @Args decorator
✗ Incorrect
Use @Args to get the 'min' argument, name the parameter 'min', and filter numbers greater than or equal to min.