Để giao tiếp với server sử dụng Apollo Server, client có thể sử dụng Apollo Client. Với angular có thể sử dụng Apollo Angular.
Install:
Install theo hướng dẫn sau https://apollo-angular.com/docs/get-started
Để giao tiếp với server sử dụng Apollo Server, client có thể sử dụng Apollo Client. Với angular có thể sử dụng Apollo Angular.
Install theo hướng dẫn sau https://apollo-angular.com/docs/get-started
GraphQL:
GraphQL là 1 query language cho APIs. Được phát triển bởi Facebook và có thể thay thế REST.
NestJs:
Một framework của NodeJs, kết hợp bởi OOP(Object Oriented Programming), FP(Functional Programming), FRP(Functional Reactive Programming). Và có hổ trợ GraphQL.
Angular:
Một javascript framework có khả năng scale, sử dụng typescript tương đồng như NestJs nên nếu team cùng phát triển cả client và server thì cũng dễ dàng hơn.
Authentication sử dụng jwt-token
JWTService:
import { JwtService } from '@nestjs/jwt';
import { jwtCredentials } from '../config';
export const jwtService = new JwtService({
signOptions: {
algorithm: 'RS256',
},
...jwtCredentials,
});
Credential:
import * as Path from 'path';
import * as FS from 'fs-extra';
export const jwtCredentials = {
privateKey: FS.readFileSync(
Path.resolve(__dirname, '..', '..', '..', 'etc', 'cert', 'private.key'),
'utf8',
),
publicKey: FS.readFileSync(
Path.resolve(__dirname, '..', '..', '..', 'etc', 'cert', 'public.key'),
'utf8',
),
};
Structure (UserModule)
...
├── modules/
| ├── dto/
| | ├── user-create.dto.ts
| | ├── user-update.dto.ts
| | └── ...
| |
| ├── entities/
| | ├── user.entity.ts
| | └── ...
| |
| ├── repositories
| | ├── user.repository.ts
| | └── ...
| |
| ├── services
| | ├── user.service.ts
| | └── ...
| |
| ├── user.graphql
| ├── user.module.ts
| ├── user.resolver.ts
...
Để cài đặt NestJs có thể clone git hoặc thông qua NestCLI
## Clone git
$ git clone https://github.com/nestjs/typescript-starter.git project
$ cd project
$ npm install
$ npm run start
## NestCLI
$ npm i -g @nestjs/cli
$ nest new project-name
Tạo kết nối realtime giữa client và server. Đầu tiên cần Enable giá trị installSubscriptionHandlers: true
trong GraphQlService và subscriptions
để verify token.
VD tạo subscription gửi thông báo cho client.
Để validate input có thể validate tại graphql schema hoặc dùng class-validator
tại resolver.
Graphql Schema có 1 số scalar có thể dùng để validation cho input.
input UserCreateInput {
name: String!
password: String!
email: String!
}