This is an example of backend implementation by Node.js for the purpose of learning Clean Architecture.
The TODO app is provided as a specific example.
yarn
yarn build
yarn docker
yarn db:migrate
yarn db:seed
yarn dev
yarn fix
yarn test
yarn build
yarn start
It has a Monorepo structure, and the projects are divided according to roles.
By explicitly separating the projects, it has the meaning of enforcing Clean Architecture.
GraphQL schema defines the contract between frontend and backend. Technically, the graphql file specifies the data type (type, input, enum) and API (query, mutation).
graphql-codegen outputs TypeScript type information from graphql file. Since not only the entity type but also the functional type information of the resolver is output, it is possible to develop while maintaining high type safety.
Also, based on this schema information, it is possible to generate type information optimized for Apollo Client on the front end side as well. In addition to expecting DX improvement through code completion, high-quality service development protected by type information can be expected.
It implements the business logic that is the core of the project, and implements value-object, entity, usecase, etc. as elements for that. It also exposes the repository, presenter, and usecase interfaces for the outer layers.
The use case is complete within the domain-model, and the implementation is such that the use case holds even if there is no outer layer. In fact, the backend project only implements a usecase for business logic via the controller.
We use tests to verify the correctness of behavior. The outer layer (repository, presenter) uses a mock to check mainly the core state transitions and validations expected for usecases.
Since the core business logic is provided by the domain-model side, the main purpose of this project is to work with external layers. Specifically, we provide a server implementation that supports GraphQL and a persistence layer.
MySQL (Docker) is used for the persistence layer, and the repository pattern is implemented by TypeORM. TypeORM is very suitable for Clean Architecture because it provides a repository pattern as standard. It’s a slapstick, but migration is excellent, and it’s very easy because it automatically generates a migration file by looking at the difference between the ORM code and the DB.
Author: suzukalight
Source Code: https://github.com/suzukalight/clean-architecture-nodejs-graphql-codegen
#nodejs #node #javascript