-
Notifications
You must be signed in to change notification settings - Fork 1
3. First feature
When a hero slays a dragon, he gets a new item in his inventory, but you may have noticed that, for now, he cannot do anything with it. Let's change that ! Here's your ticket: As a Hero, I can equip an item and it improves my attack
. Try to do it on your own then check the commits if you need help. The full PR is available here.
git checkout -b feature-hero-equip-item
Let's start with the core part of the feature.
- In
heroes/core/domain/hero.entity.ts
, add anequippedItem
property to the Hero entity - Modify the
getHeroAttackValue
function to take it in account (keep it simple for now). - Commit your first changes: First commit.
Now we want to modify this new property. Let's keep it simple for now, we won't check if the hero or the item exists, nor that the item belongs to the hero. We can use the DDD Hexagonal Generator to create a new command EquipItem
.
3 files should have been created, let's get rid of the equip-item.port.ts
for now.
- You will likely need the id of the Hero equipping the item and the id of the Item: add them in the payload of the command in
equip-item.command.ts
. - In
equip-item.command-handler.ts
, change the port being injected in the constructor to@Inject(Hero) private readonly heroPorts: UpdatePort<Hero>
. This means that you will use a port of the entity Hero to update it. There are similar ports to Get, Create, or Delete an entity. - In the execute method of the handler, use the payload to make the correct update.
- In
heroes/core/application/hero.applications.ts
, don't forget to add your new application. - Commit your changes: Second commit
Now you should change the infrastructure; we want the update made to the Hero entity to be persisted in the database.
- Add an equippedItem column in the Hero Typeorm entity
heroes/infrastructure/typeorm/hero.orm-entity.ts
. - Create a migration for this change, you can find more information on migrations here:
npm run typeorm:migration:generate -- AlterHeroTableAddEquippedItemColumn
Before moving on to the next task, you can check your migration, it should look like this.
If something went wrong, you can revert the migration with npm run typeorm:migration:revert
, and delete the migration in /migrations
before regenerating it. When your migration is done, don't forget to run it with npm run typeorm:migration:run
.
- Update the mapper between the domain entity and the database entity:
heroes/infrastructure/typeorm/hero.orm-mapper.ts
. - Commit your changes: Third commit
Now we want to add a GraphQL mutation to call this Command application.
- Add a mutation
equipItem(heroId: ID!, itemId: ID!): Boolean!
in the GraphQL schema fileheroes/interface/graphql/hero.graphql
. - Generate the new schema typings:
npm run gql-typings:generate
- Add a mutation in the GraphQL resolver file
heroes/interface/graphql/hero.resolver.ts
that will execute the command. - Commit your changes: Fourth commit
You should already be able to test the mutation: Try it with the right ids and check that the equippedItem
column is filled in the database.
Now we also want this information to appear when querying the hero.
- Add the field in the Hero type in the GraphQL schema file.
- Generate the new schema typings.
- Update the mapper between the domain entity and the GraphQL schema:
heroes/interface/graphql/hero.gql-mapper.ts
. - Commit your changes: Fifth commit
That's it !