0
0
NestJSframework~10 mins

Code-first approach in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Code-first approach
Define TypeScript classes
Decorate classes with GraphQL decorators
NestJS reads classes
Generate GraphQL schema from classes
Run GraphQL server with generated schema
You write TypeScript classes with decorators, NestJS reads them to create the GraphQL schema automatically, then runs the server.
Execution Sample
NestJS
import { ObjectType, Field, Int } from '@nestjs/graphql';

@ObjectType()
class Recipe {
  @Field(() => Int)
  id: number;

  @Field()
  title: string;
}
Defines a Recipe GraphQL type with id and title fields using decorators.
Execution Table
StepActionInputOutput/Effect
1Define class Recipeclass Recipe { id, title }TypeScript class created
2Add @ObjectType decoratorDecorate RecipeMarks Recipe as GraphQL ObjectType
3Add @Field decoratorsDecorate id and titleMarks fields for GraphQL schema
4NestJS reads classesRecipe class with decoratorsGenerates GraphQL schema type Recipe { id: Int, title: String }
5Start GraphQL serverSchema generatedServer runs with Recipe type available
6Client queries RecipeQuery for RecipeServer returns data matching Recipe type
7EndNo more actionsExecution stops
💡 All classes processed and schema generated; server running with schema.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
Recipe classDefined with id, titleMarked as ObjectTypeFields marked with @FieldSchema type Recipe generatedUsed by GraphQL server
Key Moments - 3 Insights
Why do we use decorators like @ObjectType and @Field?
Decorators tell NestJS which classes and properties to include in the GraphQL schema, as shown in steps 2 and 3 of the execution_table.
How does NestJS create the GraphQL schema from code?
NestJS reads the decorated classes at runtime (step 4) and generates the schema automatically without writing schema files.
What happens if a class property lacks @Field decorator?
That property is ignored in the GraphQL schema, so it won't be queryable, as implied between steps 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output after step 4?
ATypeScript class created
BGraphQL schema type Recipe generated
CServer starts running
DClient queries Recipe
💡 Hint
Check the Output/Effect column for step 4 in the execution_table.
At which step does NestJS read the decorated classes to generate the schema?
AStep 2
BStep 6
CStep 4
DStep 1
💡 Hint
Look for the step mentioning NestJS reading classes in the execution_table.
If you remove @Field decorator from a property, what changes in the execution?
AThe property is ignored in the generated schema
BThe property is included in the schema anyway
CNestJS throws an error at step 4
DThe server fails to start
💡 Hint
Refer to the key_moments explanation about missing @Field decorators.
Concept Snapshot
Code-first approach in NestJS:
- Write TypeScript classes
- Use @ObjectType on classes
- Use @Field on properties
- NestJS auto-generates GraphQL schema
- Run server with generated schema
- No separate schema files needed
Full Transcript
In the code-first approach with NestJS, you start by defining TypeScript classes representing your GraphQL types. You decorate these classes with @ObjectType and their properties with @Field to mark them for inclusion in the GraphQL schema. NestJS reads these decorated classes at runtime and automatically generates the GraphQL schema from them. This means you do not have to write schema files manually. Once the schema is generated, the GraphQL server runs and can respond to client queries based on the defined types. Properties without the @Field decorator are ignored in the schema and won't be queryable. This approach keeps your schema and code in sync and simplifies development.