0
0
NestJSframework~10 mins

Schema-first approach 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 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'
Areturn 'Hello World!'
BHello World!
Cconsole.log('Hello World!')
D'Hello World!'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the string.
Using console.log instead of returning a value.
2fill in blank
medium

Complete 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'
AtypePaths
BtypeDefsFile
CschemaPath
DtypeDefs
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'typeDefs' which is for code-first approach.
Using incorrect option names like 'schemaPath'.
3fill in blank
hard

Fix 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'
A@Query('getUser')
B@Query(() => String)
C@Query(() => 'User')
D@Query(() => [User])
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the return type in @Query decorator.
Using incorrect return types like String when returning an object.
4fill in blank
hard

Fill 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'
A'User'
B'Alice'
CUserType
D'Bob'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong type name in the decorator.
Not using quotes around the string name.
5fill in blank
hard

Fill 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'
A'User'
B'Alice'
C'Bob'
DUserType
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect type names like UserType.
Forgetting quotes around string names.