When working with Nest, I created a new module using the command nest g module UserModule
src/user-module/user.resolver.ts
contains the following code:
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();
}
}
and then
src/user-module/user-module.module.ts
looks like this -
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
defines the UserService:
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
includes the PrismaService:
import { Injectable, OnModuleInit } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';
@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
async onModuleInit() {
await this.$connect();
}
}
Starting the service with npm start
, I ran a query and encountered an error:
query {
users {
id
name
email
}
}
The error message received is as follows:
[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') ...
After reviewing my code on GitHub, I am still unsure why my injection is not functioning correctly. This differs from the issue discussed in this Stack Overflow post.