作者 陈志颖

feat:增加common

... ... @@ -2873,6 +2873,11 @@
"resolved": "https://registry.npmjs.org/@types/ungap__global-this/-/ungap__global-this-0.3.1.tgz",
"integrity": "sha512-+/DsiV4CxXl6ZWefwHZDXSe1Slitz21tom38qPCaG0DYCS1NnDPIQDTKcmQ/tvK/edJUKkmuIDBJbmKDiB0r/g=="
},
"@types/validator": {
"version": "13.1.3",
"resolved": "https://registry.npmjs.org/@types/validator/-/validator-13.1.3.tgz",
"integrity": "sha512-DaOWN1zf7j+8nHhqXhIgNmS+ltAC53NXqGxYuBhWqWgqolRhddKzfZU814lkHQSTG0IUfQxU7Cg0gb8fFWo2mA=="
},
"@types/webpack": {
"version": "4.41.26",
"resolved": "https://registry.npmjs.org/@types/webpack/-/webpack-4.41.26.tgz",
... ... @@ -3642,6 +3647,155 @@
"@apollo/protobufjs": "^1.0.3"
}
},
"apollo-server": {
"version": "2.21.0",
"resolved": "https://registry.npmjs.org/apollo-server/-/apollo-server-2.21.0.tgz",
"integrity": "sha512-OqngjOSB0MEH6VKGWHcrqt4y39HlhYh9CrMvn4PhadTt53IPYRmBglk5qSRA8xMorGqy60iKrOReqj5YfCjTOg==",
"requires": {
"apollo-server-core": "^2.21.0",
"apollo-server-express": "^2.21.0",
"express": "^4.0.0",
"graphql-subscriptions": "^1.0.0",
"graphql-tools": "^4.0.8",
"stoppable": "^1.1.0"
},
"dependencies": {
"@types/node-fetch": {
"version": "2.5.7",
"resolved": "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.5.7.tgz",
"integrity": "sha512-o2WVNf5UhWRkxlf6eq+jMZDu7kjgpgJfl4xVNlvryc95O/6F2ld8ztKX+qu+Rjyet93WAWm5LjeX9H5FGkODvw==",
"requires": {
"@types/node": "*",
"form-data": "^3.0.0"
}
},
"apollo-env": {
"version": "0.6.6",
"resolved": "https://registry.npmjs.org/apollo-env/-/apollo-env-0.6.6.tgz",
"integrity": "sha512-hXI9PjJtzmD34XviBU+4sPMOxnifYrHVmxpjykqI/dUD2G3yTiuRaiQqwRwB2RCdwC1Ug/jBfoQ/NHDTnnjndQ==",
"requires": {
"@types/node-fetch": "2.5.7",
"core-js": "^3.0.1",
"node-fetch": "^2.2.0",
"sha.js": "^2.4.11"
}
},
"apollo-graphql": {
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/apollo-graphql/-/apollo-graphql-0.6.1.tgz",
"integrity": "sha512-ZRXAV+k+hboCVS+FW86FW/QgnDR7gm/xMUwJPGXEbV53OLGuQQdIT0NCYK7AzzVkCfsbb7NJ3mmEclkZY9uuxQ==",
"requires": {
"apollo-env": "^0.6.6",
"lodash.sortby": "^4.7.0"
}
},
"apollo-server-core": {
"version": "2.21.0",
"resolved": "https://registry.npmjs.org/apollo-server-core/-/apollo-server-core-2.21.0.tgz",
"integrity": "sha512-GtIiq2F0dVDLzzIuO5+dK/pGq/sGxYlKCqAuQQqzYg0fvZ7fukyluXtcTe0tMI+FJZjU0j0WnKgiLsboCoAlPQ==",
"requires": {
"@apollographql/apollo-tools": "^0.4.3",
"@apollographql/graphql-playground-html": "1.6.26",
"@apollographql/graphql-upload-8-fork": "^8.1.3",
"@types/ws": "^7.0.0",
"apollo-cache-control": "^0.11.6",
"apollo-datasource": "^0.7.3",
"apollo-graphql": "^0.6.0",
"apollo-reporting-protobuf": "^0.6.2",
"apollo-server-caching": "^0.5.3",
"apollo-server-env": "^3.0.0",
"apollo-server-errors": "^2.4.2",
"apollo-server-plugin-base": "^0.10.4",
"apollo-server-types": "^0.6.3",
"apollo-tracing": "^0.12.2",
"async-retry": "^1.2.1",
"fast-json-stable-stringify": "^2.0.0",
"graphql-extensions": "^0.12.8",
"graphql-tag": "^2.11.0",
"graphql-tools": "^4.0.8",
"loglevel": "^1.6.7",
"lru-cache": "^6.0.0",
"sha.js": "^2.4.11",
"subscriptions-transport-ws": "^0.9.11",
"uuid": "^8.0.0",
"ws": "^6.0.0"
}
},
"apollo-server-env": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/apollo-server-env/-/apollo-server-env-3.0.0.tgz",
"integrity": "sha512-tPSN+VttnPsoQAl/SBVUpGbLA97MXG990XIwq6YUnJyAixrrsjW1xYG7RlaOqetxm80y5mBZKLrRDiiSsW/vog==",
"requires": {
"node-fetch": "^2.1.2",
"util.promisify": "^1.0.0"
}
},
"apollo-server-plugin-base": {
"version": "0.10.4",
"resolved": "https://registry.npmjs.org/apollo-server-plugin-base/-/apollo-server-plugin-base-0.10.4.tgz",
"integrity": "sha512-HRhbyHgHFTLP0ImubQObYhSgpmVH4Rk1BinnceZmwudIVLKrqayIVOELdyext/QnSmmzg5W7vF3NLGBcVGMqDg==",
"requires": {
"apollo-server-types": "^0.6.3"
}
},
"apollo-server-types": {
"version": "0.6.3",
"resolved": "https://registry.npmjs.org/apollo-server-types/-/apollo-server-types-0.6.3.tgz",
"integrity": "sha512-aVR7SlSGGY41E1f11YYz5bvwA89uGmkVUtzMiklDhZ7IgRJhysT5Dflt5IuwDxp+NdQkIhVCErUXakopocFLAg==",
"requires": {
"apollo-reporting-protobuf": "^0.6.2",
"apollo-server-caching": "^0.5.3",
"apollo-server-env": "^3.0.0"
}
},
"apollo-tracing": {
"version": "0.12.2",
"resolved": "https://registry.npmjs.org/apollo-tracing/-/apollo-tracing-0.12.2.tgz",
"integrity": "sha512-SYN4o0C0wR1fyS3+P0FthyvsQVHFopdmN3IU64IaspR/RZScPxZ3Ae8uu++fTvkQflAkglnFM0aX6DkZERBp6w==",
"requires": {
"apollo-server-env": "^3.0.0",
"apollo-server-plugin-base": "^0.10.4"
}
},
"form-data": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.1.tgz",
"integrity": "sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==",
"requires": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.8",
"mime-types": "^2.1.12"
}
},
"graphql-tools": {
"version": "4.0.8",
"resolved": "https://registry.npmjs.org/graphql-tools/-/graphql-tools-4.0.8.tgz",
"integrity": "sha512-MW+ioleBrwhRjalKjYaLQbr+920pHBgy9vM/n47sswtns8+96sRn5M/G+J1eu7IMeKWiN/9p6tmwCHU7552VJg==",
"requires": {
"apollo-link": "^1.2.14",
"apollo-utilities": "^1.0.1",
"deprecated-decorator": "^0.1.6",
"iterall": "^1.1.3",
"uuid": "^3.1.0"
},
"dependencies": {
"uuid": {
"version": "3.4.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz",
"integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A=="
}
}
},
"ws": {
"version": "6.2.1",
"resolved": "https://registry.npmjs.org/ws/-/ws-6.2.1.tgz",
"integrity": "sha512-GIyAXC2cB7LjvpgMt9EKS2ldqr0MTrORaleiOno6TweZ6r3TKtoFQWay/2PceJ3RuBasOHzXNn5Lrw1X0bEjqA==",
"requires": {
"async-limiter": "~1.0.0"
}
}
}
},
"apollo-server-caching": {
"version": "0.5.3",
"resolved": "https://registry.npmjs.org/apollo-server-caching/-/apollo-server-caching-0.5.3.tgz",
... ... @@ -4804,6 +4958,11 @@
"integrity": "sha512-uc2Vix1frTfnuzxxu1Hp4ktSvM3QaI4oXl4ZUqL1wjTu/BGki9TrCWoqLTg/drR1KwAEarXuRFCG2Svr1GxPFw==",
"dev": true
},
"class-transformer": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/class-transformer/-/class-transformer-0.4.0.tgz",
"integrity": "sha512-ETWD/H2TbWbKEi7m9N4Km5+cw1hNcqJSxlSYhsLsNjQzWWiZIYA1zafxpK9PwVfaZ6AqR5rrjPVUBGESm5tQUA=="
},
"class-utils": {
"version": "0.3.6",
"resolved": "https://registry.npmjs.org/class-utils/-/class-utils-0.3.6.tgz",
... ... @@ -4827,6 +4986,16 @@
}
}
},
"class-validator": {
"version": "0.13.1",
"resolved": "https://registry.npmjs.org/class-validator/-/class-validator-0.13.1.tgz",
"integrity": "sha512-zWIeYFhUitvAHBwNhDdCRK09hWx+P0HUwFE8US8/CxFpMVzkUK8RJl7yOIE+BVu2lxyPNgeOaFv78tLE47jBIg==",
"requires": {
"@types/validator": "^13.1.3",
"libphonenumber-js": "^1.9.7",
"validator": "^13.5.2"
}
},
"clean-stack": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/clean-stack/-/clean-stack-2.2.0.tgz",
... ... @@ -8016,6 +8185,11 @@
"type-check": "~0.4.0"
}
},
"libphonenumber-js": {
"version": "1.9.11",
"resolved": "https://registry.npmjs.org/libphonenumber-js/-/libphonenumber-js-1.9.11.tgz",
"integrity": "sha512-ussVs6j3k0NEU4PNwWmVNGgmZQ88YrqzAw80ztmBfEhIQr55FpjFzPoDk5sWIfOmPuY1jmCKrxWCIemkBKqSPw=="
},
"lines-and-columns": {
"version": "1.1.6",
"resolved": "https://registry.npmjs.org/lines-and-columns/-/lines-and-columns-1.1.6.tgz",
... ... @@ -10339,6 +10513,11 @@
"integrity": "sha1-NbCYdbT/SfJqd35QmzCQoyJr8ks=",
"dev": true
},
"stoppable": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/stoppable/-/stoppable-1.1.0.tgz",
"integrity": "sha512-KXDYZ9dszj6bzvnEMRYvxgeTHU74QBFL54XKtP3nyMuJ81CFYtABZ3bAzL2EdFUaEwJOBOgENyFj3R7oTzDyyw=="
},
"streamsearch": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-0.1.2.tgz",
... ... @@ -11283,6 +11462,11 @@
"spdx-expression-parse": "^3.0.0"
}
},
"validator": {
"version": "13.5.2",
"resolved": "https://registry.npmjs.org/validator/-/validator-13.5.2.tgz",
"integrity": "sha512-mD45p0rvHVBlY2Zuy3F3ESIe1h5X58GPfAtslBjY7EtTqGquZTj+VX/J4RnHWN8FKq0C9WRVt1oWAcytWRuYLQ=="
},
"vary": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
... ...
... ... @@ -26,7 +26,10 @@
"@nestjs/core": "^7.5.1",
"@nestjs/graphql": "^7.9.9",
"@nestjs/platform-express": "^7.5.1",
"apollo-server": "^2.21.0",
"apollo-server-express": "^2.21.0",
"class-transformer": "^0.4.0",
"class-validator": "^0.13.1",
"graphql": "^15.5.0",
"graphql-tools": "^7.0.4",
"nestjs-config": "^1.4.7",
... ...
import { Module } from '@nestjs/common';
import { AuthorsService } from './authors.service';
import { AuthorsResolver } from './authors.resolver';
@Module({
providers: [AuthorsService, AuthorsResolver],
})
export class AuthorsModule {}
... ...
... ... @@ -9,15 +9,25 @@ import {
Int,
} from '@nestjs/graphql';
import { PubSub } from 'graphql-subscriptions';
import { Author } from './models/aothor.model';
import { Author } from './models/author.model';
import { Post } from './models/post.model';
import { AuthorService } from './author.service';
import { AuthorsService } from './authors.service';
import { PostsService } from './posts.service';
const pubSub = new PubSub();
@Resolver(() => Author)
export class AuthorResolver {
constructor(private authorsService: AuthorService) {}
export class AuthorsResolver {
constructor(
private authorsService: AuthorsService,
private postsService: PostsService,
) {}
@Query((returns) => Author)
async author(@Args('id', { type: () => Int }) id: number) {
return this.authorsService.findOneById(id);
}
// 根据id查询作者信息
@Query((returns) => Author, {
name: 'author',
... ...
import { Injectable } from '@nestjs/common';
@Injectable()
export class AuthorService {
export class AuthorsService {
async findOneById() {}
}
\ No newline at end of file
}
... ...
import { ArgsType, Field, Int } from '@nestjs/graphql';
import { MinLength } from 'class-validator';
@ArgsType()
class GetAuthorArgs {
@Field({ nullable: true })
firstName?: string;
@Field({ defaultValue: '' })
@MinLength(3)
lastName: string;
}
... ...
import { SchemaDirectiveVisitor } from 'apollo-server';
import { defaultFieldResolver, GraphQLField } from 'graphql';
export class UpperCaseDirective extends SchemaDirectiveVisitor {
visitFieldDefinition(field: GraphQLField<any, any>) {
const { resolve = defaultFieldResolver } = field;
field.resolve = async function (...args) {
const result = await resolve.apply(this, args);
if (typeof result === 'string') {
return result.toUpperCase();
}
return result;
};
}
}
... ...
import { Plugin } from '@nestjs/graphql';
import {
ApolloServerPlugin,
GraphQLRequestListener,
} from 'apollo-server-plugin-base';
@Plugin()
export class LoggingPlugin implements ApolloServerPlugin {
requestDidStart(): GraphQLRequestListener {
console.log('Request started');
return {
willSendResponse() {
console.log('Will send response');
},
};
}
}
... ...
import { CustomScalar, Scalar } from '@nestjs/graphql';
import { Kind } from 'graphql';
@Scalar('Date')
export class DateScalar implements CustomScalar<number, Date> {
description = 'Date custom scalar type';
parseValue(value: number): Date {
return new Date(value); // value from the client
}
serialize(value: Date): number {
return value.getTime(); // value sent to the client
}
parseLiteral(ast: any): Date {
if (ast.kind === Kind.INT) {
return new Date(ast.value);
}
return null;
}
}
... ...
import { Module } from '@nestjs/common';
import { AuthorService } from './author.service';
import { AuthorResolver } from './author.resolver';
@Module({
providers: [AuthorService, AuthorResolver],
})
export class AuthorModule {}
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';
import { ApplicationModule } from './../src/app.module';
describe('AppController (e2e)', () => {
let app: INestApplication;
beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
imports: [ApplicationModule],
}).compile();
app = moduleFixture.createNestApplication();
... ...