Bird
Raised Fist0
NestJSframework~10 mins

Schema-first approach in NestJS - Interactive Code Practice

Choose your learning style10 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.

Practice

(1/5)
1. What is the main idea behind the Schema-first approach in NestJS GraphQL?
easy
A. Use REST endpoints before adding GraphQL
B. Write resolvers first, then generate the schema automatically
C. Define the GraphQL schema first, then write resolvers to match it
D. Skip schema and use only TypeScript interfaces

Solution

  1. Step 1: Understand Schema-first approach concept

    Schema-first means you write the GraphQL schema (types, queries, mutations) before coding the logic.
  2. Step 2: Match resolvers to schema

    Resolvers are then implemented to fulfill the schema's fields and operations.
  3. Final Answer:

    Define the GraphQL schema first, then write resolvers to match it -> Option C
  4. Quick Check:

    Schema-first = Define schema first [OK]
Hint: Schema-first means schema before code [OK]
Common Mistakes:
  • Thinking resolvers come before schema
  • Confusing schema-first with code-first
  • Assuming REST is involved
  • Skipping schema definition
2. Which of the following is the correct way to define a GraphQL type in a schema-first approach SDL file?
easy
A. type User { id: ID! name: String! }
B. class User { id: number; name: string; }
C. interface User { id: ID; name: String }
D. schema User { id: ID! name: String! }

Solution

  1. Step 1: Recognize GraphQL SDL syntax

    GraphQL SDL defines types with the keyword type followed by fields and their types.
  2. Step 2: Check each option

    type User { id: ID! name: String! } uses correct SDL syntax. Options B and C use TypeScript syntax, and A uses invalid keyword schema for type definition.
  3. Final Answer:

    type User { id: ID! name: String! } -> Option A
  4. Quick Check:

    SDL type syntax = type User { id: ID! name: String! } [OK]
Hint: SDL uses 'type' keyword with fields and types [OK]
Common Mistakes:
  • Using TypeScript syntax instead of SDL
  • Confusing 'schema' keyword with 'type'
  • Omitting exclamation marks for required fields
3. Given this schema snippet:
type Query { hello: String! }

And this resolver in NestJS:
@Resolver('Query')
export class HelloResolver {
  @Query()
  hello() {
    return 'Hi there!';
  }
}

What will be the result of querying { hello }?
medium
A. "Hi there!"
B. null
C. Error: Resolver method missing
D. undefined

Solution

  1. Step 1: Match schema and resolver

    The schema defines a Query field hello returning a non-null String. The resolver implements a method hello() returning 'Hi there!'.
  2. Step 2: Query execution result

    When querying { hello }, the resolver returns 'Hi there!', so the response is the string "Hi there!".
  3. Final Answer:

    "Hi there!" -> Option A
  4. Quick Check:

    Schema and resolver match = returns 'Hi there!' [OK]
Hint: Resolver method name matches schema field [OK]
Common Mistakes:
  • Not matching resolver method name to schema field
  • Returning undefined or null in resolver
  • Missing @Query decorator
4. Identify the error in this schema-first resolver code snippet:
@Resolver('Query')
export class UserResolver {
  @Query()
  user(id: string) {
    return { id, name: 'Alice' };
  }
}

Assuming the schema defines user(id: ID!): User.
medium
A. Resolver class name must be UserQueryResolver
B. Missing @Args decorator for the id parameter
C. Return type must be a string, not an object
D. Schema should not have arguments in schema-first

Solution

  1. Step 1: Check resolver method parameters

    The resolver method user expects an argument id. In NestJS schema-first, arguments must be decorated with @Args() to map schema args.
  2. Step 2: Identify missing decorator

    The code misses @Args('id') before the id parameter, so NestJS won't pass the argument correctly.
  3. Final Answer:

    Missing @Args decorator for the id parameter -> Option B
  4. Quick Check:

    Arguments need @Args decorator [OK]
Hint: Use @Args() for resolver method arguments [OK]
Common Mistakes:
  • Forgetting @Args decorator on parameters
  • Wrong resolver class naming (not required)
  • Returning wrong types
  • Thinking schema can't have arguments
5. You have this schema-first GraphQL SDL:
type Mutation {
  updateUser(id: ID!, name: String): User
}

type User {
  id: ID!
  name: String!
}

Which resolver implementation correctly updates the user name and returns the updated user in NestJS?
hard
A. @Mutation() updateUser(@Args('id') id: string, @Args('name') name: string) { return { id, name }; }
B. @Mutation() updateUser(id: string, name: string) { const user = users.find(u => u.id === id); user.name = name; return user; }
C. @Mutation() updateUser(@Args() args) { const user = users.find(u => u.id === args.id); user.name = args.name; return user; }
D. @Mutation() updateUser(@Args('id') id: string, @Args('name') name?: string) { const user = users.find(u => u.id === id); if (user && name) user.name = name; return user; }

Solution

  1. Step 1: Match resolver signature to schema

    The schema defines updateUser(id: ID!, name: String): User. The resolver must use @Args('id') and optional @Args('name') parameters.
  2. Step 2: Check update logic and return

    @Mutation() updateUser(@Args('id') id: string, @Args('name') name?: string) { const user = users.find(u => u.id === id); if (user && name) user.name = name; return user; } finds the user by id, updates name only if provided, and returns the user object, matching schema and logic.
  3. Step 3: Analyze other options

    @Mutation() updateUser(id: string, name: string) { const user = users.find(u => u.id === id); user.name = name; return user; } misses @Args decorators, so arguments won't map. @Mutation() updateUser(@Args() args) { const user = users.find(u => u.id === args.id); user.name = args.name; return user; } uses @Args() args but does not check if user exists before setting user.name, causing runtime error if user not found. @Mutation() updateUser(@Args('id') id: string, @Args('name') name: string) { return { id, name }; } returns a new object without updating the stored user.
  4. Final Answer:

    The resolver using @Args('id') id: string, @Args('name') name?: string with conditional update logic -> Option D
  5. Quick Check:

    Use @Args and update logic correctly [OK]
Hint: Use @Args for each param and update data before return [OK]
Common Mistakes:
  • Missing @Args decorators
  • Not checking if user exists before update
  • Returning new object without updating data
  • Using @Args() without destructuring
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard