Skip to content

Commit

Permalink
Merge pull request #57 from zhumeisongsong/feature/users-interface-ad…
Browse files Browse the repository at this point in the history
…apters

refactor: ♻️ Move dto and resolver to interface-adapters
  • Loading branch information
zhumeisongsong authored Nov 19, 2024
2 parents df86e39 + b97a295 commit 8f1b3fb
Show file tree
Hide file tree
Showing 32 changed files with 55 additions and 127 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,15 @@ You can use `npx nx list` to get a list of installed plugins. Then, run `npx nx

## Architecture

| Layer | Description |
| ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| dto(presentation) | Define DTOs for GraphQL schema. |
| resolver(presentation) | Define GraphQL schema and resolver. |
| mongoose(infrastructure) | Implements the repository interfaces defined in the domain layer using Mongoose as the ODM (Object Document Mapper). <br/>Includes Mongoose Schema definitions, database connection management, and concrete implementations of repository interfaces (e.g., MongooseUserRepository). |
| use-case(application) | Define business use cases and encapsulate business logic. |
| entity(domain) | Define core business entities and business rules.<br/> Maintain entity independence from database and framework. |
| repository(domain) | Interfaces (or abstract classes), which define methods for manipulating data without concern for specific database implementations. <br/>By defining this interface, we can decouple database access: the specific details of data access will be done by implementation classes, such as specific implementations using tools like Mongoose, TypeORM, Prisma, and so on. |
| Layer | Description |
| ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| resolver(interface-adapters) | Define GraphQL schema and resolver. |
| dto(interface-adapters) | Define DTOs for GraphQL schema. |
| mongoose(infrastructure) | Implements the repository interfaces defined in the domain layer using Mongoose as the ODM (Object Document Mapper). <br/>Includes Mongoose Schema definitions, database connection management, and concrete implementations of repository interfaces (e.g., MongooseUserRepository). |
| service(application) | As the core of the application layer, it mainly interacts with the domain layer and interface-adapter layer.<br/> If you migrate to a non-NestJS architecture in the future (e.g. other frameworks or microservices), the application tier code can be left unaffected. |
| use-case(application) | Define business use cases and encapsulate business logic. |
| entity(domain) | Define core business entities and business rules.<br/> Maintain entity independence from database and framework. |
| repository(domain) | Interfaces (or abstract classes), which define methods for manipulating data without concern for specific database implementations. <br/>By defining this interface, we can decouple database access: the specific details of data access will be done by implementation classes, such as specific implementations using tools like Mongoose, TypeORM, Prisma, and so on. |

## Useful links

Expand Down
2 changes: 1 addition & 1 deletion apps/users-e2e/src/users/users.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ describe('GET /', () => {
const res = await axios.get(`/`);

expect(res.status).toBe(200);
expect(res.data).toEqual({ message: 'Hello User Service' });
expect(res.data).toEqual({ message: 'Hello Users Module' });
});
});
4 changes: 2 additions & 2 deletions apps/users/src/app/app.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ describe('AppController', () => {
});

describe('getData', () => {
it('should return "Hello User Service"', () => {
it('should return "Hello Users Module"', () => {
const appController = app.get<AppController>(AppController);
expect(appController.getData()).toEqual({
message: 'Hello User Service',
message: 'Hello Users Module',
});
});
});
Expand Down
5 changes: 3 additions & 2 deletions apps/users/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { databaseConfig, userAppConfig } from '@shared/config';
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { databaseConfig, userAppConfig } from '@shared/config';

import { UsersModule } from '../users/users.module';

import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UsersModule } from '../users/users.module';

@Module({
imports: [
Expand Down
4 changes: 2 additions & 2 deletions apps/users/src/app/app.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ describe('AppService', () => {
});

describe('getData', () => {
it('should return "Hello User Service"', () => {
expect(service.getData()).toEqual({ message: 'Hello User Service' });
it('should return "Hello Users Module"', () => {
expect(service.getData()).toEqual({ message: 'Hello Users Module' });
});
});
});
2 changes: 1 addition & 1 deletion apps/users/src/app/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
getData(): { message: string } {
return { message: 'Hello User Service' };
return { message: 'Hello Users Module' };
}
}
4 changes: 2 additions & 2 deletions apps/users/src/users/users.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { ApolloServerPluginInlineTrace } from '@apollo/server/plugin/inlineTrace';
import {
ApolloFederationDriver,
ApolloFederationDriverConfig,
} from '@nestjs/apollo';
import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { UsersResolver } from '@users/presentation-resolver';
import { UsersService } from '@users/application';
import { ApolloServerPluginInlineTrace } from '@apollo/server/plugin/inlineTrace';
import { UsersResolver } from '@users/interface-adapters';
import { DatabaseModule } from '@shared/infrastructure-mongoose';

@Module({
Expand Down
7 changes: 7 additions & 0 deletions libs/users/interface-adapters/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# users-interface-adapters

This library was generated with [Nx](https://nx.dev).

## Running unit tests

Run `nx test users-interface-adapters` to execute the unit tests via [Jest](https://jestjs.io).
3 changes: 3 additions & 0 deletions libs/users/interface-adapters/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const baseConfig = require('../../../eslint.config.js');

module.exports = [...baseConfig];
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export default {
displayName: 'users-presentation-dto',
preset: '../../../../jest.preset.js',
displayName: 'users-interface-adapters',
preset: '../../../jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../../../coverage/libs/users/presentation/dto',
coverageDirectory: '../../../coverage/libs/users/interface-adapters',
};
9 changes: 9 additions & 0 deletions libs/users/interface-adapters/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "users-interface-adapters",
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/users/interface-adapters/src",
"projectType": "library",
"tags": [],
"// targets": "to see all targets run: nx show project users-interface-adapters --web",
"targets": {}
}
4 changes: 4 additions & 0 deletions libs/users/interface-adapters/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// dto
export * from './lib/dto/user.dto';
// resolver
export * from './lib/resolver/users.resolver';
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Args, ID, Query, Resolver, ResolveReference } from '@nestjs/graphql';
import { UsersService } from '@users/application';
import { User } from '@users/domain';
import { UserDto } from '@users/presentation-dto';

import { UserDto } from '../dto/user.dto';

@Resolver(() => UserDto)
export class UsersResolver {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../../../../tsconfig.base.json",
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs",
"forceConsistentCasingInFileNames": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "../../../../dist/out-tsc",
"outDir": "../../../dist/out-tsc",
"declaration": true,
"types": ["node"]
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../../dist/out-tsc",
"outDir": "../../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
Expand Down
7 changes: 0 additions & 7 deletions libs/users/presentation/dto/README.md

This file was deleted.

3 changes: 0 additions & 3 deletions libs/users/presentation/dto/eslint.config.js

This file was deleted.

9 changes: 0 additions & 9 deletions libs/users/presentation/dto/project.json

This file was deleted.

1 change: 0 additions & 1 deletion libs/users/presentation/dto/src/index.ts

This file was deleted.

11 changes: 0 additions & 11 deletions libs/users/presentation/dto/tsconfig.lib.json

This file was deleted.

14 changes: 0 additions & 14 deletions libs/users/presentation/dto/tsconfig.spec.json

This file was deleted.

7 changes: 0 additions & 7 deletions libs/users/presentation/resolver/README.md

This file was deleted.

3 changes: 0 additions & 3 deletions libs/users/presentation/resolver/eslint.config.js

This file was deleted.

10 changes: 0 additions & 10 deletions libs/users/presentation/resolver/jest.config.ts

This file was deleted.

9 changes: 0 additions & 9 deletions libs/users/presentation/resolver/project.json

This file was deleted.

1 change: 0 additions & 1 deletion libs/users/presentation/resolver/src/index.ts

This file was deleted.

22 changes: 0 additions & 22 deletions libs/users/presentation/resolver/tsconfig.json

This file was deleted.

9 changes: 4 additions & 5 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@
"@shared/infrastructure-mongoose": [
"libs/shared/infrastructure/mongoose/src/index.ts"
],
"@users/domain": ["libs/users/domain/src/index.ts"],
"@users/application": ["libs/users/application/src/index.ts"],
"@users/domain": ["libs/users/domain/src/index.ts"],
"@users/infrastructure-mongoose": [
"libs/users/infrastructure/mongoose/src/index.ts"
],
"@users/presentation-dto": ["libs/users/presentation/dto/src/index.ts"],
"@users/presentation-resolver": [
"libs/users/presentation/resolver/src/index.ts"
]
"@users/interface-adapters": [
"libs/users/interface-adapters/src/index.ts"
],
}
},
"exclude": ["node_modules", "tmp"]
Expand Down

0 comments on commit 8f1b3fb

Please sign in to comment.