Skip to content

Commit

Permalink
Merge pull request #48 from zhumeisongsong/feature/user-dto
Browse files Browse the repository at this point in the history
feat: ✨ add UserDto
  • Loading branch information
zhumeisongsong authored Nov 18, 2024
2 parents a943204 + 68b3e36 commit 1f18384
Show file tree
Hide file tree
Showing 26 changed files with 150 additions and 37 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ You can use `npx nx list` to get a list of installed plugins. Then, run `npx nx

## Architecture

| Layer | Description |
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| 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 |
| --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| dto(presentation) | Define DTOs for GraphQL schema. |
| resolver(presentation) | Define GraphQL schema and resolver. |
| 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/src/users/users.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from '@nestjs/apollo';
import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { UsersResolver } from '@users/presentation';
import { UsersResolver } from '@users/presentation-resolver';
import { UsersService } from '@users/application';
import { ApolloServerPluginInlineTrace } from '@apollo/server/plugin/inlineTrace';

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

This file was deleted.

7 changes: 7 additions & 0 deletions libs/users/presentation/dto/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# users-presentation-dto

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

## Running unit tests

Run `nx test users-presentation-dto` to execute the unit tests via [Jest](https://jestjs.io).
3 changes: 3 additions & 0 deletions libs/users/presentation/dto/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',
preset: '../../../jest.preset.js',
displayName: 'users-presentation-dto',
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',
coverageDirectory: '../../../../coverage/libs/users/presentation/dto',
};
9 changes: 9 additions & 0 deletions libs/users/presentation/dto/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "users-presentation-dto",
"$schema": "../../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/users/presentation/dto/src",
"projectType": "library",
"tags": [],
"// targets": "to see all targets run: nx show project users-presentation-dto --web",
"targets": {}
}
1 change: 1 addition & 0 deletions libs/users/presentation/dto/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/user.dto';
13 changes: 13 additions & 0 deletions libs/users/presentation/dto/src/lib/user.dto.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { UserDto } from './user.dto';

describe('UserDto', () => {
it('should create a new UserDto instance', () => {
const id = '123';
const name = 'Test User';
const userDto = new UserDto(id, name);

expect(userDto).toBeDefined();
expect(userDto.id).toBe(id);
expect(userDto.name).toBe(name);
});
});
16 changes: 16 additions & 0 deletions libs/users/presentation/dto/src/lib/user.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Directive, Field, ID, ObjectType } from '@nestjs/graphql';

@ObjectType()
@Directive('@key(fields: "id")')
export class UserDto {
@Field(() => ID)
id: string;

@Field()
name: string;

constructor(id: string, name: string) {
this.id = id;
this.name = name;
}
}
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
3 changes: 0 additions & 3 deletions libs/users/presentation/eslint.config.js

This file was deleted.

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

This file was deleted.

7 changes: 7 additions & 0 deletions libs/users/presentation/resolver/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# users-presentation-resolver

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

## Running unit tests

Run `nx test users-presentation-resolver` to execute the unit tests via [Jest](https://jestjs.io).
3 changes: 3 additions & 0 deletions libs/users/presentation/resolver/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];
10 changes: 10 additions & 0 deletions libs/users/presentation/resolver/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default {
displayName: 'users-presentation-resolver',
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/resolver',
};
9 changes: 9 additions & 0 deletions libs/users/presentation/resolver/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "users-presentation-resolver",
"$schema": "../../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/users/presentation/resolver/src",
"projectType": "library",
"tags": [],
"// targets": "to see all targets run: nx show project users-presentation-resolver --web",
"targets": {}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('UsersResolver', () => {
it('should return undefined if user not found', () => {
jest.spyOn(service, 'findById').mockReturnValue(undefined);

expect(resolver.getUser('2')).toBeUndefined();
expect(resolver.getUser('2')).toBeNull();
expect(service.findById).toHaveBeenCalledWith('2');
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { Args, ID, Query, Resolver, ResolveReference } from '@nestjs/graphql';
import { UsersService } from '@users/application';
import { User } from '@user/domain';
import { UserDto } from '@users/presentation-dto';

@Resolver(() => User)
@Resolver(() => UserDto)
export class UsersResolver {
constructor(private usersService: UsersService) {}

@Query(() => User, { nullable: true })
getUser(@Args({ name: 'id', type: () => ID }) id: string): User | undefined {
return this.usersService.findById(id);
@Query(() => UserDto, { nullable: true })
getUser(@Args({ name: 'id', type: () => ID }) id: string): UserDto | null {
const user: User | undefined = this.usersService.findById(id); // Domain entity
return user ? new UserDto(user.id, user.name) : null;
}

@ResolveReference()
Expand Down
22 changes: 22 additions & 0 deletions libs/users/presentation/resolver/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"extends": "../../../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noPropertyAccessFromIndexSignature": true
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
11 changes: 11 additions & 0 deletions libs/users/presentation/resolver/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "../../../../dist/out-tsc",
"declaration": true,
"types": ["node"]
},
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"],
"include": ["src/**/*.ts"]
}
14 changes: 14 additions & 0 deletions libs/users/presentation/resolver/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
}
5 changes: 4 additions & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
"@prompt/domain": ["libs/prompt/domain/src/index.ts"],
"@user/domain": ["libs/user/domain/src/index.ts"],
"@users/application": ["libs/users/application/src/index.ts"],
"@users/presentation": ["libs/users/presentation/src/index.ts"]
"@users/presentation-dto": ["libs/users/presentation/dto/src/index.ts"],
"@users/presentation-resolver": [
"libs/users/presentation/resolver/src/index.ts"
]
}
},
"exclude": ["node_modules", "tmp"]
Expand Down

0 comments on commit 1f18384

Please sign in to comment.