Clean Architecture Nodejs Graphql Codegen

clean-architecture-nodejs-graphql-codegen

This is an example of backend implementation by Node.js for the purpose of learning Clean Architecture.

Overview

The TODO app is provided as a specific example.

Operation check

setup

  • Requires docker-compose and node.js
  • I’m using yarn
  • Since it is necessary to build the dependent files in advance, build it once as well.
yarn
yarn build
yarn docker
yarn db:migrate
yarn db:seed

development

yarn dev

test

yarn fix
yarn test
  • fix = typecheck(tsc) + lint(eslint) + format(prettier)
  • test = is (ts-is)

production

yarn build
yarn start

Project structure

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.

  • common
  • Commonly used by all projects, such as custom error objects
  • schema
  • Describe the project-wide schema and API configuration in a .graphql file
  • graphql-codegen automatically generates DTO, schema, resolver type information from graphql file
  • domain-model
  • Implement domain layer and usecase layer
  • Write only business logic, independent of specific backend implementation
  • backend
  • Implement interface layer and infrastructure layer
  • Uses TypeORM for persistence layer operations and provides GraphQL API as a web server
  • frontend (WIP)
  • Implementing frontend in React (Next.js)
  • Currently only supports login / logout, Done / Undone and CreateTodo use cases

Feature

schema

GraphQL schema defines the contract between frontend and backend. Technically, the graphql file specifies the data type (type, input, enum) and API (query, mutation).

Automatic generation of TypeScript type information by graphql-codegen

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.

domain-model

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.

Write only business logic, independent of specific backend implementation

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.

backend

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.

Use TypeORM to manipulate 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.

Download Details:

Author: suzukalight

Source Code: https://github.com/suzukalight/clean-architecture-nodejs-graphql-codegen

#nodejs #node #javascript

Clean Architecture Nodejs Graphql Codegen
35.35 GEEK