Skip to content

Commit

Permalink
Resolving issue with User resolver and database connection
Browse files Browse the repository at this point in the history
  • Loading branch information
quinnjr committed Oct 23, 2023
1 parent 8eb44ac commit c6e1666
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 47 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/articles/articles.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { User } from '../../graphql/user/user.model';

@Resolver((of: any) => Article)
export class ArticlesResolver {
constructor(/*/*private readonly $databaseService: DatabaseService*/*/) {}
constructor(/*/*private readonly $databaseService: DatabaseService*/) {}

@ResolveField('author', (returns) => User)
public async getAuthor(@Parent() article: Article): Promise<User | null> {
Expand Down
6 changes: 3 additions & 3 deletions server/database/database.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import {
OnModuleInit,
OnModuleDestroy
} from '@nestjs/common';
import { PrismaClient } from '@prisma/client';
//import { PrismaClient } from '@prisma/client';

@Injectable()
export class DatabaseService
extends PrismaClient
// extends PrismaClient
implements OnModuleDestroy, OnModuleInit
{
public async onModuleInit() {
// await this.$connect();
}

public async onModuleDestroy() {
await this.$disconnect();
// await this.$disconnect();
}
}
88 changes: 47 additions & 41 deletions server/users/users.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,69 +15,75 @@ export class UsersResolver {
constructor(/*private readonly $databaseService: DatabaseService*/) {}

@Query((returns) => User, { name: 'user' })
public async getUser(@Args('email') email: string): Promise<User> {
const user = await this.$databaseService.user.findUnique({
where: {
email
}
});
public async getUser(@Args('email') email: string): Promise<User | null> {
// const user = await this.$databaseService.user.findUnique({
// where: {
// email
// }
// });

if (!user) {
throw new Error('No user was found');
} else {
return user;
}
// if (!user) {
// throw new Error('No user was found');
// } else {
// return user;
// }
return null;
}

@ResolveField('articles', (returns) => [Article])
public async getArticles(@Parent() user: User): Promise<Article[]> {
public async getArticles(@Parent() user: User): Promise<Article[] | null> {
const { id } = user;
return this.$databaseService.article.findMany({
where: {
authorId: id
}
});
// return this.$databaseService.article.findMany({
// where: {
// authorId: id
// }
// });
return null;
}

@ResolveField('certifications', (returns) => [Certification])
public async getCertifications(
@Parent() user: User
): Promise<Certification[]> {
): Promise<Certification[] | null> {
const { id } = user;
return this.$databaseService.certification.findMany({
where: {
userId: id
}
});
// return this.$databaseService.certification.findMany({
// where: {
// userId: id
// }
// });
return null;
}

@ResolveField('educations', (returns) => [Education])
public async getEducations(@Parent() user: User): Promise<Education[]> {
public async getEducations(@Parent() user: User): Promise<Education[] | null> {
const { id } = user;
return this.$databaseService.education.findMany({
where: {
userId: id
}
});
// return this.$databaseService.education.findMany({
// where: {
// userId: id
// }
// });
return null;
}

@ResolveField('experiences', (returns) => [Experience])
public async getExperiences(@Parent() user: User): Promise<Experience[]> {
public async getExperiences(@Parent() user: User): Promise<Experience[] | null> {
const { id } = user;
return this.$databaseService.experience.findMany({
where: {
userId: id
}
});
// return this.$databaseService.experience.findMany({
// where: {
// userId: id
// }
// });
return null;
}

@ResolveField('projects', (returns) => [Project])
public async getProjects(@Parent() user: User): Promise<Project[]> {
public async getProjects(@Parent() user: User): Promise<Project[] | null> {
const { id } = user;
return this.$databaseService.project.findMany({
where: {
userId: id
}
});
// return this.$databaseService.project.findMany({
// where: {
// userId: id
// }
// });
return null;
}
}

0 comments on commit c6e1666

Please sign in to comment.