前言:为了学习nestjsgraphqlgrpc微服务的知识,后面会详细分析grpc和graphql的具体语法。1创建项目npmi-g@nestjs/clinestnewproject-name2添加graphql创建graphql-config.service.ts文件,用于配置graphql和编写过滤逻辑@Injectable()exportclassGraphQLConfigServiceimplementsGqlOptionsFactory{constructor(){}createGqlOptions():GqlModuleOptions{return{typePaths:[join(process.cwd(),"./graphqls/*.graphql")],//配置的graphql文件地址installSubscriptionHandlers:true,definitions:{path:join(process.cwd(),"src/graphql.schema.ts"),//解析后的文件地址outputAs:"class"},context:async({req})=>{//filterletuser=Jwt.verifyToken(req.headers.authorization);//业务逻辑return{user};}};}}添加到app.module.ts@Module({imports:[GraphQLModule.forRootAsync({imports:[ApplicationModule],useClass:GraphQLConfigService}),],})exportclassApplicationModule{}创建文件xxx.resolvers.ts@查询('getUser')为异步getUser(){返回{};}3添加grpc首先创建一个子项目xxx创建一个grpc.options.ts文件用于init连接配置exportconstgrpcClientOptions:ClientOptions={transport:Transport.GRPC,options:{url:“localhost:50051”,//服务地址包:“xxx”,protoPath:加入(__dirname,'./xxx.proto'),},};创建接口,注意这里首字母大写会自动安装@GrpcMethod("UserService")asyncaddUser(data:User):Promise
