I created new module in nest using the command nest g module UserModule
src/user-module/user.resolver.ts
is –
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 –
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
–
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
–
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 –
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.