Simple API built with nest.js, GraphQL, TypeORM and PostgreSQL for showcasing operations of watch online shop.
The project contains four modules to showcase a simple checkout action:
- watch
- discount
- user
- cart
Considering that the API would be consumed by a "dumb" client, the backend holds the state of a user's cart. So instead of creating an endpoint that calculates a total cost of a list of watches the client can access and update the cart's state through the respective queries/mutations. To obtain the total of a user's cart the following query can be used:
query {
cart(id: 'cart-id') {
total
}
}
To add items to a user's cart and obtain the updated total of the cart, the client can use the following mutation:
mutation {
addCardItem(id: 'card-id', newItem: 'watch-id') {
total
}
}
To run the API a docker-compose
is provided. First create a .env
file, add the vars from .env.example
and source the .env file locally for the PostgreSQL service:
source .env
The run the following command to fire up the API and a PostgreSQL:
docker-compose up
The GraphQL playground can be accessed on http://localhost:3000/graphql.
To seed the db run the seed query in the playground:
query {
seed
}
This will create all necessary items to test out all endpoints. The seeded data can be found in seed.resolver.ts
.
For tests the Jest test framework is used. Before running build, tests are automatically run when building the docker image.
Tests can be run locally with the following command:
npm run test
As of now only the cart.service.spec.ts
tests are implemented and the endpoint for creating a new cart item considered to contain the vital business logic is tested. Tests for the other services would be written in the same manner.
There are several things that could be improved here to enhance usability of the API:
- Increase test coverage
- Add authorization and authentication for API
- Add user management
- Refactor watch module to be product module to achieve a more generic data model
- Add error handling for adding undefined products/watches to cart
- Add and endpoint to remove items from the cart
- Refactor cart item data structure to already include quantity in the structure (will remove computational expensive calculations)