0
0
NestJSframework~10 mins

Resolvers 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 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'
Aconsole.log('Hello World!')
BHello World!
C'Hello World!'
Dreturn 'Hello World!'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the string
Using console.log instead of return
Returning a non-string value
2fill in blank
medium

Complete 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'
Asample_service
Bservice
CSampleService
DsampleService
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'
3fill in blank
hard

Fix 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'
AMutation
BSubscription
CQuery
DResolveField
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Query instead of @Mutation
Using @Subscription for mutations
Forgetting to import the decorator
4fill in blank
hard

Fill 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'
AArgs
Bname
CArg
Dusername
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Arg instead of @Args
Mismatching argument and parameter names
Forgetting the decorator
5fill in blank
hard

Fill 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'
AArgs
Bmin
C>=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>=' in filter
Mismatching argument and parameter names
Forgetting the @Args decorator