作者 陈志颖

feat:添加模块

... ... @@ -2197,6 +2197,19 @@
"uuid": "8.3.2"
}
},
"@nestjs/config": {
"version": "0.6.3",
"resolved": "https://registry.npmjs.org/@nestjs/config/-/config-0.6.3.tgz",
"integrity": "sha512-JxvvUpmH0/WOrTB+zh8dEkxSUQXhB7V3d/qeQXyCnMiEFjaq89+fNFztpWjz4DlOfdS4/eYTzIEy9PH2uGnfzA==",
"requires": {
"dotenv": "8.2.0",
"dotenv-expand": "5.1.0",
"lodash.get": "4.4.2",
"lodash.has": "4.5.2",
"lodash.set": "4.3.2",
"uuid": "8.3.2"
}
},
"@nestjs/core": {
"version": "7.6.12",
"resolved": "https://registry.npmjs.org/@nestjs/core/-/core-7.6.12.tgz",
... ... @@ -5354,6 +5367,16 @@
}
}
},
"dotenv": {
"version": "8.2.0",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz",
"integrity": "sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw=="
},
"dotenv-expand": {
"version": "5.1.0",
"resolved": "https://registry.npmjs.org/dotenv-expand/-/dotenv-expand-5.1.0.tgz",
"integrity": "sha512-YXQl1DSa4/PQyRfgrv6aoNjhasp/p4qs9FjJ4q4cQk+8m4r6k4ZSiEyytKG8f8W9gi8WsQtIObNmKd+tMzNTmA=="
},
"ecc-jsbn": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/ecc-jsbn/-/ecc-jsbn-0.1.2.tgz",
... ... @@ -8041,6 +8064,21 @@
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz",
"integrity": "sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA=="
},
"lodash.get": {
"version": "4.4.2",
"resolved": "https://registry.npmjs.org/lodash.get/-/lodash.get-4.4.2.tgz",
"integrity": "sha1-LRd/ZS+jHpObRDjVNBSZ36OCXpk="
},
"lodash.has": {
"version": "4.5.2",
"resolved": "https://registry.npmjs.org/lodash.has/-/lodash.has-4.5.2.tgz",
"integrity": "sha1-0Z9NwQlQWMzL4rDN9O4P5Ko3yGI="
},
"lodash.set": {
"version": "4.3.2",
"resolved": "https://registry.npmjs.org/lodash.set/-/lodash.set-4.3.2.tgz",
"integrity": "sha1-2HV7HagH3eJIFrDWqEvqGnYjCyM="
},
"lodash.sortby": {
"version": "4.7.0",
"resolved": "https://registry.npmjs.org/lodash.sortby/-/lodash.sortby-4.7.0.tgz",
... ... @@ -8480,6 +8518,19 @@
"integrity": "sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==",
"dev": true
},
"nestjs-config": {
"version": "1.4.7",
"resolved": "https://registry.npmjs.org/nestjs-config/-/nestjs-config-1.4.7.tgz",
"integrity": "sha512-lD/tW5UZ3J+awJSUOjjCjJ6QwXVoJBYuigDJONQj4FETxImPLeB30Brxgt6ZyGX8o3BvA5osDdNfYO2iKQUXRQ==",
"requires": {
"dotenv": "^8.0.0",
"glob": "^7.1.2",
"lodash.get": "^4.4.2",
"lodash.set": "^4.3.2",
"reflect-metadata": "^0.1.12",
"rxjs": "^6.0.0"
}
},
"nice-try": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/nice-try/-/nice-try-1.0.5.tgz",
... ...
... ... @@ -22,12 +22,14 @@
},
"dependencies": {
"@nestjs/common": "^7.5.1",
"@nestjs/config": "^0.6.3",
"@nestjs/core": "^7.5.1",
"@nestjs/graphql": "^7.9.9",
"@nestjs/platform-express": "^7.5.1",
"apollo-server-express": "^2.21.0",
"graphql": "^15.5.0",
"graphql-tools": "^7.0.4",
"nestjs-config": "^1.4.7",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^6.6.3"
... ...
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { GraphQLModule } from '@nestjs/graphql';
import { ConfigModule, ConfigService } from 'nestjs-config';
// import { AppController } from './app.controller';
// import { AppService } from './app.service';
import * as path from 'path';
@Module({
imports: [],
controllers: [AppController],
providers: [AppService],
imports: [
ConfigModule.load(path.resolve(__dirname, 'config', '**/!(*.d).{ts,js}')),
GraphQLModule.forRootAsync({
imports: [ConfigModule],
useFactory: (config: ConfigService) => config.get('graphql'),
inject: [ConfigService],
}),
],
// controllers: [AppController],
// providers: [AppService],
})
export class AppModule {}
// export class AppModule {}
export class ApplicationModule {}
... ...
import * as path from 'path';
export default {
autoSchemaFile: path.join(process.cwd(), 'src/schema.gql'), // 最后生成`Schema 文件,不可修改`
installSubscriptionHandlers: true, // 启动订阅
};
... ...
import { ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ApplicationModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const app = await NestFactory.create(ApplicationModule);
app.useGlobalPipes(new ValidationPipe());
await app.listen(3000);
console.log(`Application is running on: ${await app.getUrl()}`);
}
bootstrap();
... ...
import { Module } from '@nestjs/common';
import { AuthorService } from './author.service';
import { AuthorResolver } from './author.resolver';
@Module({
providers: [AuthorService, AuthorResolver],
})
export class AuthorModule {}
... ...
import {
Args,
Mutation,
Query,
Resolver,
Subscription,
ResolveField,
Parent,
Int,
} from '@nestjs/graphql';
import { PubSub } from 'graphql-subscriptions';
import { Author } from './models/aothor.model';
import { Post } from './models/post.model';
import { AuthorService } from './author.service';
const pubSub = new PubSub();
@Resolver(() => Author)
export class AuthorResolver {
constructor(private authorsService: AuthorService) {}
// 根据id查询作者信息
@Query((returns) => Author, {
name: 'author',
description: 'get author info by id',
nullable: false,
})
async getAuthor(
@Args('id', {
type: () => Int,
description: 'author id',
nullable: false,
})
id: number,
): Promise<any> {
return {
id,
firstName: 'wu',
lastName: 'pat',
};
}
// 修改作者信息
@Mutation((returns) => Author, {
name: 'changeAuthor',
description: 'change author info by id',
nullable: false,
})
async changeAuthor(
@Args('id') id: number,
@Args('firstName') firstName: string,
@Args('lastName') lastName: string,
): Promise<any> {
return {
id,
firstName,
lastName,
};
}
// 解析posts字段
@ResolveField()
async posts(@Parent() author: Author): Promise<any> {
const { id } = author;
return [
{
id: 4,
title: 'hello',
votes: 2112,
},
];
}
// 新增文章
@Mutation((returns) => Post)
async addPost() {
const newPost = {
id: 1,
title: '新增文章',
};
// 新增成功后,通知更新
await pubSub.publish('postAdded', { postAdded: newPost });
return newPost;
}
// 监听变更
@Subscription((returns) => Post, {
name: 'postAdded',
})
async postAdded(/*@Args('title') title: string*/) {
return pubSub.asyncIterator('postAdded');
}
}
... ...
import { Injectable } from '@nestjs/common';
@Injectable()
export class AuthorService {
async findOneById() {}
}
\ No newline at end of file
... ...
export class GetAuthorArgs {}
... ...
import { Field, Int, ObjectType } from '@nestjs/graphql';
import { Post } from './post.model';
@ObjectType({ description: 'Author model' })
export class Author {
@Field((type) => Int, {
description: '作者id',
})
id: number;
@Field({
nullable: true,
description: '作者姓氏',
})
firstName?: string;
@Field({
nullable: true,
description: '作者名字',
})
lastName?: string;
@Field((type) => [Post], {
nullable: 'items',
description: '作者发表的文章',
})
posts: Post[];
}
... ...
import { Field, Int, ObjectType } from '@nestjs/graphql';
@ObjectType()
export class Post {
@Field((type) => Int)
id: number;
@Field() title: string;
@Field((type) => Int, {
nullable: true,
})
votes?: number;
}
... ...