Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a GraphQL schema using the schema-first approach in NestJS.
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 to put quotes around the string.
Using console.log instead of returning a value.
✗ Incorrect
The resolver method should return a string value. The correct syntax is to return the string 'Hello World!'.
2fill in blank
mediumComplete the code to import the GraphQL module with schema-first setup in NestJS.
NestJS
import { Module } from '@nestjs/common'; import { GraphQLModule } from '@nestjs/graphql'; @Module({ imports: [ GraphQLModule.forRoot({ [1]: ['./src/schema.graphql'], }), ], }) export class AppModule {}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'typeDefs' which is for code-first approach.
Using incorrect option names like 'schemaPath'.
✗ Incorrect
In schema-first approach, the GraphQLModule uses the 'typePaths' option to specify the path to the schema file(s).
3fill in blank
hardFix the error in the resolver method to correctly use the schema-first approach in NestJS.
NestJS
import { Resolver, Query } from '@nestjs/graphql'; @Resolver('User') export class UserResolver { [1] getUser() { return { id: 1, name: 'Alice' }; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the return type in @Query decorator.
Using incorrect return types like String when returning an object.
✗ Incorrect
The @Query decorator needs to specify the return type matching the schema. Using @Query(() => 'User') tells NestJS the return type is User.
4fill in blank
hardFill both blanks to correctly define a GraphQL type and resolver method in schema-first approach.
NestJS
import { Resolver, Query } from '@nestjs/graphql'; @Resolver('User') export class UserResolver { @Query(() => [1]) getUser() { return { id: 1, name: [2] }; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong type name in the decorator.
Not using quotes around the string name.
✗ Incorrect
The resolver method returns a User object, so the return type is 'User'. The name field should be a string like 'Alice'.
5fill in blank
hardFill all three blanks to create a schema-first resolver with a query returning a list of users.
NestJS
import { Resolver, Query } from '@nestjs/graphql'; @Resolver('User') export class UserResolver { @Query(() => [[1]]) getUsers() { return [ { id: 1, name: [2] }, { id: 2, name: [3] }, ]; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect type names like UserType.
Forgetting quotes around string names.
✗ Incorrect
The query returns a list of User objects, so the return type is ['User']. The names are string literals 'Alice' and 'Bob'.