I created new module in nest using the command nest g module UserModule
我使用Nest g模块UserModule命令在Nest中创建了新模块
src/user-module/user.resolver.ts
is -
SRC/USER-MODULE/USER.RESOVER.TS为-
import { Query, Resolver } from '@nestjs/graphql';
import { UserService } from './user.service';
export class UserResolver {
constructor (private readonly user: UserService) {
this.user = user;
}
@Query('users')
async users() {
return this.user.getUsers();
}
}
then src/user-module/user-module.module.ts
is -
则src/用户模块/用户模块.模块.ts是-
import { Module } from '@nestjs/common';
import { UserResolver } from './user.resolver';
import { UserService } from './user.service';
import { PrismaService } from './prisma.services';
@Module({
providers: [UserResolver, UserService, PrismaService]
})
export class UserModuleModule {}
the file src/user-module/user.service.ts
-
文件src/用户模块/user.service.ts-
import { Injectable } from "@nestjs/common";
import { PrismaService } from "./prisma.services";
import { User } from "src/graphql";
@Injectable()
export class UserService {
constructor(private readonly prisma: PrismaService) {
}
async getUsers(): Promise<User[]> {
return this.prisma.prismaUsers.findMany({})
}
}
the src/user-module/prisma.services.ts
-
Src/USER-MODULE/prisma.services.ts-
import { Injectable, OnModuleInit } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';
@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
async onModuleInit() {
await this.$connect();
}
}
I started the service with npm start
. I queried the service using below command -
我用NPM Start启动了这项服务。我使用以下命令查询了该服务-
query {
users {
id
name
email
}
}
I get the below error -
我得到下面的错误-
[Nest] 24682 - 09/09/2023, 10:59:34 AM ERROR [ExceptionsHandler] Cannot read properties of undefined (reading'getUsers')
TypeError: Cannot read properties of undefined (reading 'getUsers')
at UserResolver.users (/home/raj/Coding/ts/prisma-gql-express-demo/src/user-module/user.resolver.ts:12:22)
at /home/raj/Coding/ts/prisma-gql-express-demo/node_modules/@nestjs/core/helpers/external-context-creator.js:69:29
at InterceptorsConsumer.intercept (/home/raj/Coding/ts/prisma-gql-express-demo/node_modules/@nestjs/core/interceptors/interceptors-consumer.js:12:20)
at target (/home/raj/Coding/ts/prisma-gql-express-demo/node_modules/@nestjs/core/helpers/external-context-creator.js:74:60)
at Object.users (/home/raj/Coding/ts/prisma-gql-express-demo/node_modules/@nestjs/core/helpers/external-proxy.js:9:30)
at field.resolve (/home/raj/Coding/ts/prisma-gql-express-demo/node_modules/@apollo/server/src/utils/schemaInstrumentation.ts:82:22)
at executeField (/home/raj/Coding/ts/prisma-gql-express-demo/node_modules/graphql/execution/execute.js:492:20)
at executeFields (/home/raj/Coding/ts/prisma-gql-express-demo/node_modules/graphql/execution/execute.js:414:22)
at executeOperation (/home/raj/Coding/ts/prisma-gql-express-demo/node_modules/graphql/execution/execute.js:344:14)
at execute (/home/raj/Coding/ts/prisma-gql-express-demo/node_modules/graphql/execution/execute.js:136:20)
What is wrong with my injection and why it is not working ?
my complete code is here in github.
I seen this stackoverflow but my error seems to be due to different.
我的注射有什么问题,为什么不起作用?我的完整代码在GitHub中。我看到这个堆栈溢出,但我的错误似乎是由于不同。
更多回答
Your UserResolver
class needs to have @Resolver()
decorating it, so that Nest knows it's a resolver and so Typescript knows to emit the type metadata in the constructor for the class. It's just like needing to add @Injectable()
to a regular provider, it tells Typescript to emit the metadata
您的UserResolver类需要有@Resolver()来装饰它,以便Nest知道它是一个解析器,因此TypeScrip知道在类的构造函数中发出类型元数据。这就像需要将@Inputable()添加到常规提供程序中一样,它会告诉TypeScrip发出元数据
更多回答
我是一名优秀的程序员,十分优秀!