To configure a GraphQL API based on TypeGraphQL and Webpack in order to automate the Hot Reload when the code is edited.
The basic demanded modules in order to build a project based on this library (check its Installation section).
The most important part in order to automate the hot reloading process is the definition of the Nodemon Webpack Plugin located at the webpack.dev.js, plugins section.
new NodemonPlugin({
watch: path.resolve('./dist'),
script: './dist/bundle.js',
verbose: true,
ext: 'js'
}),
This way, Nodemon is going to be tracking the compiled file and this module will be in charge to manage the server reloading after every change in the code.
-
Clone the repository and install its dependencies.
$ git clone https://github.com/ddialar/typegraphql.hot.reload $ cd typegraphql.hot.reload $ npm i
-
Run the server.
$ npm run build:dev
You will get something like that:
-
Surf to the http://localhost:4500/ URL and run the next query in the Playground environment:
query { recipes { title description } }
-
Open the
recipe-resolver.ts
file and edit therecipes()
resolver commenting the line number #25. Save the code and check that the server has been reloaded. -
Run the Playground's query again and you will see that at this time, the
[TRACE]
comment doesn't appear. -
Now open the
recipe-type.ts
file and edit theRecipe
class commenting thedescription
field (lines #12 and #13).import { Field, ObjectType, Int } from 'type-graphql'; @ObjectType() class Recipe { @Field() title: string = ""; // @Field({ nullable: true, description: 'The recipe description with preparation info.' }) // description?: string; @Field(type => [Int]) ratings: number[] = []; @Field() creationData: Date = new Date(); } export default Recipe;
Save the code and check that the server has been reloaded.
-
Run the Playground's query again and you will receive an error due to the
description
field is not defined.