From c1f24da32899e60bf85a1f4b7465229218eb6f90 Mon Sep 17 00:00:00 2001 From: zhumeisongsong Date: Mon, 18 Nov 2024 12:58:11 +0900 Subject: [PATCH 1/7] =?UTF-8?q?refactor:=20=E2=99=BB=EF=B8=8F=20@users/pre?= =?UTF-8?q?sentation=20to=20@users/presentation-resolver?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/users/presentation/resolver/README.md | 7 ++ .../presentation/resolver/eslint.config.js | 3 + .../presentation/resolver/jest.config.ts | 10 +++ libs/users/presentation/resolver/project.json | 9 +++ libs/users/presentation/resolver/src/index.ts | 1 + .../resolver/src/lib/users.resolver.spec.ts | 69 +++++++++++++++++++ .../resolver/src/lib/users.resolver.ts | 21 ++++++ .../users/presentation/resolver/tsconfig.json | 22 ++++++ .../presentation/resolver/tsconfig.lib.json | 11 +++ .../presentation/resolver/tsconfig.spec.json | 14 ++++ 10 files changed, 167 insertions(+) create mode 100644 libs/users/presentation/resolver/README.md create mode 100644 libs/users/presentation/resolver/eslint.config.js create mode 100644 libs/users/presentation/resolver/jest.config.ts create mode 100644 libs/users/presentation/resolver/project.json create mode 100644 libs/users/presentation/resolver/src/index.ts create mode 100644 libs/users/presentation/resolver/src/lib/users.resolver.spec.ts create mode 100644 libs/users/presentation/resolver/src/lib/users.resolver.ts create mode 100644 libs/users/presentation/resolver/tsconfig.json create mode 100644 libs/users/presentation/resolver/tsconfig.lib.json create mode 100644 libs/users/presentation/resolver/tsconfig.spec.json diff --git a/libs/users/presentation/resolver/README.md b/libs/users/presentation/resolver/README.md new file mode 100644 index 0000000..30d3458 --- /dev/null +++ b/libs/users/presentation/resolver/README.md @@ -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). diff --git a/libs/users/presentation/resolver/eslint.config.js b/libs/users/presentation/resolver/eslint.config.js new file mode 100644 index 0000000..cdd3cba --- /dev/null +++ b/libs/users/presentation/resolver/eslint.config.js @@ -0,0 +1,3 @@ +const baseConfig = require('../../../../eslint.config.js'); + +module.exports = [...baseConfig]; diff --git a/libs/users/presentation/resolver/jest.config.ts b/libs/users/presentation/resolver/jest.config.ts new file mode 100644 index 0000000..96ea3cb --- /dev/null +++ b/libs/users/presentation/resolver/jest.config.ts @@ -0,0 +1,10 @@ +export default { + displayName: 'users-presentation-resolver', + preset: '../../../../jest.preset.js', + testEnvironment: 'node', + transform: { + '^.+\\.[tj]s$': ['ts-jest', { tsconfig: '/tsconfig.spec.json' }], + }, + moduleFileExtensions: ['ts', 'js', 'html'], + coverageDirectory: '../../../../coverage/libs/users/presentation/resolver', +}; diff --git a/libs/users/presentation/resolver/project.json b/libs/users/presentation/resolver/project.json new file mode 100644 index 0000000..ec728b4 --- /dev/null +++ b/libs/users/presentation/resolver/project.json @@ -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": {} +} diff --git a/libs/users/presentation/resolver/src/index.ts b/libs/users/presentation/resolver/src/index.ts new file mode 100644 index 0000000..281b820 --- /dev/null +++ b/libs/users/presentation/resolver/src/index.ts @@ -0,0 +1 @@ +export * from './lib/users.resolver'; diff --git a/libs/users/presentation/resolver/src/lib/users.resolver.spec.ts b/libs/users/presentation/resolver/src/lib/users.resolver.spec.ts new file mode 100644 index 0000000..411129c --- /dev/null +++ b/libs/users/presentation/resolver/src/lib/users.resolver.spec.ts @@ -0,0 +1,69 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { UsersService } from '@users/application'; +import { User } from '@user/domain'; + +import { UsersResolver } from './users.resolver'; + +describe('UsersResolver', () => { + let resolver: UsersResolver; + let service: UsersService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + UsersResolver, + { + provide: UsersService, + useValue: { + findById: jest.fn(), + }, + }, + ], + }).compile(); + + resolver = module.get(UsersResolver); + service = module.get(UsersService); + }); + + it('should be defined', () => { + expect(resolver).toBeDefined(); + }); + + describe('getUser', () => { + it('should return a user by id', () => { + const user: User = { id: '1', name: 'John Doe' }; + jest.spyOn(service, 'findById').mockReturnValue(user); + + expect(resolver.getUser('1')).toEqual(user); + expect(service.findById).toHaveBeenCalledWith('1'); + }); + + it('should return undefined if user not found', () => { + jest.spyOn(service, 'findById').mockReturnValue(undefined); + + expect(resolver.getUser('2')).toBeUndefined(); + expect(service.findById).toHaveBeenCalledWith('2'); + }); + }); + + describe('resolveReference', () => { + it('should return a user by reference id', () => { + const user: User = { id: '1', name: 'John Doe' }; + jest.spyOn(service, 'findById').mockReturnValue(user); + + expect( + resolver.resolveReference({ __typename: 'User', id: '1' }), + ).toEqual(user); + expect(service.findById).toHaveBeenCalledWith('1'); + }); + + it('should return undefined if user not found by reference id', () => { + jest.spyOn(service, 'findById').mockReturnValue(undefined); + + expect( + resolver.resolveReference({ __typename: 'User', id: '2' }), + ).toBeUndefined(); + expect(service.findById).toHaveBeenCalledWith('2'); + }); + }); +}); diff --git a/libs/users/presentation/resolver/src/lib/users.resolver.ts b/libs/users/presentation/resolver/src/lib/users.resolver.ts new file mode 100644 index 0000000..88ac9fa --- /dev/null +++ b/libs/users/presentation/resolver/src/lib/users.resolver.ts @@ -0,0 +1,21 @@ +import { Args, ID, Query, Resolver, ResolveReference } from '@nestjs/graphql'; +import { UsersService } from '@users/application'; +import { User } from '@user/domain'; + +@Resolver(() => User) +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); + } + + @ResolveReference() + resolveReference(reference: { + __typename: string; + id: string; + }): User | undefined { + return this.usersService.findById(reference.id); + } +} diff --git a/libs/users/presentation/resolver/tsconfig.json b/libs/users/presentation/resolver/tsconfig.json new file mode 100644 index 0000000..07e0ec6 --- /dev/null +++ b/libs/users/presentation/resolver/tsconfig.json @@ -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" + } + ] +} diff --git a/libs/users/presentation/resolver/tsconfig.lib.json b/libs/users/presentation/resolver/tsconfig.lib.json new file mode 100644 index 0000000..28369ef --- /dev/null +++ b/libs/users/presentation/resolver/tsconfig.lib.json @@ -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"] +} diff --git a/libs/users/presentation/resolver/tsconfig.spec.json b/libs/users/presentation/resolver/tsconfig.spec.json new file mode 100644 index 0000000..6668655 --- /dev/null +++ b/libs/users/presentation/resolver/tsconfig.spec.json @@ -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" + ] +} From 771df624372b9c5790208cec4852cc36524ba342 Mon Sep 17 00:00:00 2001 From: zhumeisongsong Date: Mon, 18 Nov 2024 12:58:53 +0900 Subject: [PATCH 2/7] =?UTF-8?q?refactor:=20=E2=99=BB=EF=B8=8F=20@users/pre?= =?UTF-8?q?sentation=20to=20@users/presentation-resolver?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/users/src/users/users.module.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/users/src/users/users.module.ts b/apps/users/src/users/users.module.ts index 5bb60d5..cf7fb4e 100644 --- a/apps/users/src/users/users.module.ts +++ b/apps/users/src/users/users.module.ts @@ -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'; From 2cebfb350a864502d712236696738d495b722af7 Mon Sep 17 00:00:00 2001 From: zhumeisongsong Date: Mon, 18 Nov 2024 12:59:28 +0900 Subject: [PATCH 3/7] =?UTF-8?q?feat:=20=E2=9C=A8=20add=20@users/presentati?= =?UTF-8?q?on-dto?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/users/presentation/README.md | 7 -- libs/users/presentation/dto/README.md | 7 ++ libs/users/presentation/dto/eslint.config.js | 3 + .../presentation/{ => dto}/jest.config.ts | 6 +- libs/users/presentation/dto/project.json | 9 +++ libs/users/presentation/dto/src/index.ts | 1 + .../src/lib/users-presentation-dto.spec.ts | 7 ++ .../dto/src/lib/users-presentation-dto.ts | 3 + .../presentation/{ => dto}/tsconfig.json | 2 +- .../presentation/{ => dto}/tsconfig.lib.json | 2 +- .../presentation/{ => dto}/tsconfig.spec.json | 2 +- libs/users/presentation/eslint.config.js | 3 - libs/users/presentation/project.json | 9 --- libs/users/presentation/src/index.ts | 1 - .../src/lib/users.resolver.spec.ts | 69 ------------------- .../presentation/src/lib/users.resolver.ts | 21 ------ tsconfig.base.json | 5 +- 17 files changed, 40 insertions(+), 117 deletions(-) delete mode 100644 libs/users/presentation/README.md create mode 100644 libs/users/presentation/dto/README.md create mode 100644 libs/users/presentation/dto/eslint.config.js rename libs/users/presentation/{ => dto}/jest.config.ts (55%) create mode 100644 libs/users/presentation/dto/project.json create mode 100644 libs/users/presentation/dto/src/index.ts create mode 100644 libs/users/presentation/dto/src/lib/users-presentation-dto.spec.ts create mode 100644 libs/users/presentation/dto/src/lib/users-presentation-dto.ts rename libs/users/presentation/{ => dto}/tsconfig.json (90%) rename libs/users/presentation/{ => dto}/tsconfig.lib.json (85%) rename libs/users/presentation/{ => dto}/tsconfig.spec.json (84%) delete mode 100644 libs/users/presentation/eslint.config.js delete mode 100644 libs/users/presentation/project.json delete mode 100644 libs/users/presentation/src/index.ts delete mode 100644 libs/users/presentation/src/lib/users.resolver.spec.ts delete mode 100644 libs/users/presentation/src/lib/users.resolver.ts diff --git a/libs/users/presentation/README.md b/libs/users/presentation/README.md deleted file mode 100644 index 255bbba..0000000 --- a/libs/users/presentation/README.md +++ /dev/null @@ -1,7 +0,0 @@ -# users-presentation - -This library was generated with [Nx](https://nx.dev). - -## Running unit tests - -Run `nx test users-presentation` to execute the unit tests via [Jest](https://jestjs.io). diff --git a/libs/users/presentation/dto/README.md b/libs/users/presentation/dto/README.md new file mode 100644 index 0000000..447a3e3 --- /dev/null +++ b/libs/users/presentation/dto/README.md @@ -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). diff --git a/libs/users/presentation/dto/eslint.config.js b/libs/users/presentation/dto/eslint.config.js new file mode 100644 index 0000000..cdd3cba --- /dev/null +++ b/libs/users/presentation/dto/eslint.config.js @@ -0,0 +1,3 @@ +const baseConfig = require('../../../../eslint.config.js'); + +module.exports = [...baseConfig]; diff --git a/libs/users/presentation/jest.config.ts b/libs/users/presentation/dto/jest.config.ts similarity index 55% rename from libs/users/presentation/jest.config.ts rename to libs/users/presentation/dto/jest.config.ts index 9fb66bd..dce5b33 100644 --- a/libs/users/presentation/jest.config.ts +++ b/libs/users/presentation/dto/jest.config.ts @@ -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: '/tsconfig.spec.json' }], }, moduleFileExtensions: ['ts', 'js', 'html'], - coverageDirectory: '../../../coverage/libs/users/presentation', + coverageDirectory: '../../../../coverage/libs/users/presentation/dto', }; diff --git a/libs/users/presentation/dto/project.json b/libs/users/presentation/dto/project.json new file mode 100644 index 0000000..a87f550 --- /dev/null +++ b/libs/users/presentation/dto/project.json @@ -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": {} +} diff --git a/libs/users/presentation/dto/src/index.ts b/libs/users/presentation/dto/src/index.ts new file mode 100644 index 0000000..75f50c5 --- /dev/null +++ b/libs/users/presentation/dto/src/index.ts @@ -0,0 +1 @@ +export * from './lib/users-presentation-dto'; diff --git a/libs/users/presentation/dto/src/lib/users-presentation-dto.spec.ts b/libs/users/presentation/dto/src/lib/users-presentation-dto.spec.ts new file mode 100644 index 0000000..ccccc62 --- /dev/null +++ b/libs/users/presentation/dto/src/lib/users-presentation-dto.spec.ts @@ -0,0 +1,7 @@ +import { usersPresentationDto } from './users-presentation-dto'; + +describe('usersPresentationDto', () => { + it('should work', () => { + expect(usersPresentationDto()).toEqual('users-presentation-dto'); + }); +}); diff --git a/libs/users/presentation/dto/src/lib/users-presentation-dto.ts b/libs/users/presentation/dto/src/lib/users-presentation-dto.ts new file mode 100644 index 0000000..e5b7672 --- /dev/null +++ b/libs/users/presentation/dto/src/lib/users-presentation-dto.ts @@ -0,0 +1,3 @@ +export function usersPresentationDto(): string { + return 'users-presentation-dto'; +} diff --git a/libs/users/presentation/tsconfig.json b/libs/users/presentation/dto/tsconfig.json similarity index 90% rename from libs/users/presentation/tsconfig.json rename to libs/users/presentation/dto/tsconfig.json index 0dc79ca..07e0ec6 100644 --- a/libs/users/presentation/tsconfig.json +++ b/libs/users/presentation/dto/tsconfig.json @@ -1,5 +1,5 @@ { - "extends": "../../../tsconfig.base.json", + "extends": "../../../../tsconfig.base.json", "compilerOptions": { "module": "commonjs", "forceConsistentCasingInFileNames": true, diff --git a/libs/users/presentation/tsconfig.lib.json b/libs/users/presentation/dto/tsconfig.lib.json similarity index 85% rename from libs/users/presentation/tsconfig.lib.json rename to libs/users/presentation/dto/tsconfig.lib.json index e583571..28369ef 100644 --- a/libs/users/presentation/tsconfig.lib.json +++ b/libs/users/presentation/dto/tsconfig.lib.json @@ -2,7 +2,7 @@ "extends": "./tsconfig.json", "compilerOptions": { "module": "commonjs", - "outDir": "../../../dist/out-tsc", + "outDir": "../../../../dist/out-tsc", "declaration": true, "types": ["node"] }, diff --git a/libs/users/presentation/tsconfig.spec.json b/libs/users/presentation/dto/tsconfig.spec.json similarity index 84% rename from libs/users/presentation/tsconfig.spec.json rename to libs/users/presentation/dto/tsconfig.spec.json index 69a251f..6668655 100644 --- a/libs/users/presentation/tsconfig.spec.json +++ b/libs/users/presentation/dto/tsconfig.spec.json @@ -1,7 +1,7 @@ { "extends": "./tsconfig.json", "compilerOptions": { - "outDir": "../../../dist/out-tsc", + "outDir": "../../../../dist/out-tsc", "module": "commonjs", "types": ["jest", "node"] }, diff --git a/libs/users/presentation/eslint.config.js b/libs/users/presentation/eslint.config.js deleted file mode 100644 index 07e518f..0000000 --- a/libs/users/presentation/eslint.config.js +++ /dev/null @@ -1,3 +0,0 @@ -const baseConfig = require('../../../eslint.config.js'); - -module.exports = [...baseConfig]; diff --git a/libs/users/presentation/project.json b/libs/users/presentation/project.json deleted file mode 100644 index b60d286..0000000 --- a/libs/users/presentation/project.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "name": "users-presentation", - "$schema": "../../../node_modules/nx/schemas/project-schema.json", - "sourceRoot": "libs/users/presentation/src", - "projectType": "library", - "tags": [], - "// targets": "to see all targets run: nx show project users-presentation --web", - "targets": {} -} diff --git a/libs/users/presentation/src/index.ts b/libs/users/presentation/src/index.ts deleted file mode 100644 index 281b820..0000000 --- a/libs/users/presentation/src/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './lib/users.resolver'; diff --git a/libs/users/presentation/src/lib/users.resolver.spec.ts b/libs/users/presentation/src/lib/users.resolver.spec.ts deleted file mode 100644 index 411129c..0000000 --- a/libs/users/presentation/src/lib/users.resolver.spec.ts +++ /dev/null @@ -1,69 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { UsersService } from '@users/application'; -import { User } from '@user/domain'; - -import { UsersResolver } from './users.resolver'; - -describe('UsersResolver', () => { - let resolver: UsersResolver; - let service: UsersService; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - providers: [ - UsersResolver, - { - provide: UsersService, - useValue: { - findById: jest.fn(), - }, - }, - ], - }).compile(); - - resolver = module.get(UsersResolver); - service = module.get(UsersService); - }); - - it('should be defined', () => { - expect(resolver).toBeDefined(); - }); - - describe('getUser', () => { - it('should return a user by id', () => { - const user: User = { id: '1', name: 'John Doe' }; - jest.spyOn(service, 'findById').mockReturnValue(user); - - expect(resolver.getUser('1')).toEqual(user); - expect(service.findById).toHaveBeenCalledWith('1'); - }); - - it('should return undefined if user not found', () => { - jest.spyOn(service, 'findById').mockReturnValue(undefined); - - expect(resolver.getUser('2')).toBeUndefined(); - expect(service.findById).toHaveBeenCalledWith('2'); - }); - }); - - describe('resolveReference', () => { - it('should return a user by reference id', () => { - const user: User = { id: '1', name: 'John Doe' }; - jest.spyOn(service, 'findById').mockReturnValue(user); - - expect( - resolver.resolveReference({ __typename: 'User', id: '1' }), - ).toEqual(user); - expect(service.findById).toHaveBeenCalledWith('1'); - }); - - it('should return undefined if user not found by reference id', () => { - jest.spyOn(service, 'findById').mockReturnValue(undefined); - - expect( - resolver.resolveReference({ __typename: 'User', id: '2' }), - ).toBeUndefined(); - expect(service.findById).toHaveBeenCalledWith('2'); - }); - }); -}); diff --git a/libs/users/presentation/src/lib/users.resolver.ts b/libs/users/presentation/src/lib/users.resolver.ts deleted file mode 100644 index 88ac9fa..0000000 --- a/libs/users/presentation/src/lib/users.resolver.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { Args, ID, Query, Resolver, ResolveReference } from '@nestjs/graphql'; -import { UsersService } from '@users/application'; -import { User } from '@user/domain'; - -@Resolver(() => User) -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); - } - - @ResolveReference() - resolveReference(reference: { - __typename: string; - id: string; - }): User | undefined { - return this.usersService.findById(reference.id); - } -} diff --git a/tsconfig.base.json b/tsconfig.base.json index 7c2161d..54ea840 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -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"] From 2048584d81922d46faa71ff9cc3522aa08fac7a7 Mon Sep 17 00:00:00 2001 From: zhumeisongsong Date: Mon, 18 Nov 2024 13:16:12 +0900 Subject: [PATCH 4/7] =?UTF-8?q?feat:=20=E2=9C=A8=20add=20UserDto?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/users/presentation/dto/src/index.ts | 2 +- libs/users/presentation/dto/src/lib/user.dto.ts | 16 ++++++++++++++++ .../dto/src/lib/users-presentation-dto.spec.ts | 7 ------- .../dto/src/lib/users-presentation-dto.ts | 3 --- 4 files changed, 17 insertions(+), 11 deletions(-) create mode 100644 libs/users/presentation/dto/src/lib/user.dto.ts delete mode 100644 libs/users/presentation/dto/src/lib/users-presentation-dto.spec.ts delete mode 100644 libs/users/presentation/dto/src/lib/users-presentation-dto.ts diff --git a/libs/users/presentation/dto/src/index.ts b/libs/users/presentation/dto/src/index.ts index 75f50c5..a234d86 100644 --- a/libs/users/presentation/dto/src/index.ts +++ b/libs/users/presentation/dto/src/index.ts @@ -1 +1 @@ -export * from './lib/users-presentation-dto'; +export * from './lib/user.dto'; diff --git a/libs/users/presentation/dto/src/lib/user.dto.ts b/libs/users/presentation/dto/src/lib/user.dto.ts new file mode 100644 index 0000000..567e367 --- /dev/null +++ b/libs/users/presentation/dto/src/lib/user.dto.ts @@ -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; + } +} diff --git a/libs/users/presentation/dto/src/lib/users-presentation-dto.spec.ts b/libs/users/presentation/dto/src/lib/users-presentation-dto.spec.ts deleted file mode 100644 index ccccc62..0000000 --- a/libs/users/presentation/dto/src/lib/users-presentation-dto.spec.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { usersPresentationDto } from './users-presentation-dto'; - -describe('usersPresentationDto', () => { - it('should work', () => { - expect(usersPresentationDto()).toEqual('users-presentation-dto'); - }); -}); diff --git a/libs/users/presentation/dto/src/lib/users-presentation-dto.ts b/libs/users/presentation/dto/src/lib/users-presentation-dto.ts deleted file mode 100644 index e5b7672..0000000 --- a/libs/users/presentation/dto/src/lib/users-presentation-dto.ts +++ /dev/null @@ -1,3 +0,0 @@ -export function usersPresentationDto(): string { - return 'users-presentation-dto'; -} From 264a605b8febe8fcd4c79e78974ffa3d09c32ffb Mon Sep 17 00:00:00 2001 From: zhumeisongsong Date: Mon, 18 Nov 2024 13:16:49 +0900 Subject: [PATCH 5/7] =?UTF-8?q?feat:=20=E2=9C=A8=20Use=20DTO=20in=20resolv?= =?UTF-8?q?er?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../presentation/resolver/src/lib/users.resolver.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/libs/users/presentation/resolver/src/lib/users.resolver.ts b/libs/users/presentation/resolver/src/lib/users.resolver.ts index 88ac9fa..1b5a730 100644 --- a/libs/users/presentation/resolver/src/lib/users.resolver.ts +++ b/libs/users/presentation/resolver/src/lib/users.resolver.ts @@ -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() From d4b46712227dad3bd60111dcdf9bb9115116f246 Mon Sep 17 00:00:00 2001 From: zhumeisongsong Date: Mon, 18 Nov 2024 13:17:08 +0900 Subject: [PATCH 6/7] =?UTF-8?q?docs:=20=F0=9F=93=9D=20add=20presentation?= =?UTF-8?q?=20doc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index fe8cfde..ccea7a4 100644 --- a/README.md +++ b/README.md @@ -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.
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.
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.
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.
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 From 68b3e36e7ca619bb79f269b5fba4114c38688634 Mon Sep 17 00:00:00 2001 From: zhumeisongsong Date: Mon, 18 Nov 2024 13:30:01 +0900 Subject: [PATCH 7/7] =?UTF-8?q?test:=20=F0=9F=A7=AA=20pass=20test=20cases?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../users/presentation/dto/src/lib/user.dto.spec.ts | 13 +++++++++++++ .../resolver/src/lib/users.resolver.spec.ts | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 libs/users/presentation/dto/src/lib/user.dto.spec.ts diff --git a/libs/users/presentation/dto/src/lib/user.dto.spec.ts b/libs/users/presentation/dto/src/lib/user.dto.spec.ts new file mode 100644 index 0000000..aa7e379 --- /dev/null +++ b/libs/users/presentation/dto/src/lib/user.dto.spec.ts @@ -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); + }); +}); diff --git a/libs/users/presentation/resolver/src/lib/users.resolver.spec.ts b/libs/users/presentation/resolver/src/lib/users.resolver.spec.ts index 411129c..2ce2a45 100644 --- a/libs/users/presentation/resolver/src/lib/users.resolver.spec.ts +++ b/libs/users/presentation/resolver/src/lib/users.resolver.spec.ts @@ -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'); }); });