feat: prisma typed select (#347)

* feat: wip prisma gql select

* feat: stronger api using decorator

* feat: add PrismaSelect everywhere

* fix: remove unused

* fix: remove seed debug
This commit is contained in:
Jérémy M
2023-06-22 11:17:31 +02:00
committed by GitHub
parent eb9be6894e
commit ca283a2196
35 changed files with 1081 additions and 578 deletions

View File

@ -2,40 +2,28 @@ import * as TypeGraphQL from '@nestjs/graphql';
import { CommentThread } from 'src/core/@generated/comment-thread/comment-thread.model';
import { Comment } from 'src/core/@generated/comment/comment.model';
import { Company } from 'src/core/@generated/company/company.model';
import { User } from 'src/core/@generated/user/user.model';
import { CompanyService } from './company.service';
import { CommentThreadService } from '../comment/services/comment-thread.service';
import { CommentService } from '../comment/services/comment.service';
import {
PrismaSelect,
PrismaSelector,
} from 'src/decorators/prisma-select.decorator';
@TypeGraphQL.Resolver(() => Company)
export class CompanyRelationsResolver {
constructor(
private readonly companyService: CompanyService,
private readonly commentThreadService: CommentThreadService,
private readonly commentService: CommentService,
) {}
@TypeGraphQL.ResolveField(() => User, {
nullable: true,
})
async accountOwner(
@TypeGraphQL.Parent() company: Company,
): Promise<User | null> {
return this.companyService
.findUniqueOrThrow({
where: {
id: company.id,
},
})
.accountOwner({});
}
@TypeGraphQL.ResolveField(() => [CommentThread], {
nullable: false,
})
async commentThreads(
@TypeGraphQL.Root() company: Company,
): Promise<CommentThread[]> {
@PrismaSelector({ modelName: 'CommentThread' })
prismaSelect: PrismaSelect<'CommentThread'>,
): Promise<Partial<CommentThread>[]> {
return this.commentThreadService.findMany({
where: {
commentThreadTargets: {
@ -45,13 +33,18 @@ export class CompanyRelationsResolver {
},
},
},
select: prismaSelect.value,
});
}
@TypeGraphQL.ResolveField(() => [Comment], {
nullable: false,
})
async comments(@TypeGraphQL.Root() company: Company): Promise<Comment[]> {
async comments(
@TypeGraphQL.Root() company: Company,
@PrismaSelector({ modelName: 'Comment' })
prismaSelect: PrismaSelect<'Comment'>,
): Promise<Partial<Comment>[]> {
return this.commentService.findMany({
where: {
commentThread: {
@ -63,6 +56,7 @@ export class CompanyRelationsResolver {
},
},
},
select: prismaSelect.value,
});
}

View File

@ -15,6 +15,10 @@ import { DeleteManyGuard } from '../../guards/delete-many.guard';
import { CreateOneGuard } from '../../guards/create-one.guard';
import { CompanyService } from './company.service';
import { prepareFindManyArgs } from 'src/utils/prepare-find-many';
import {
PrismaSelect,
PrismaSelector,
} from 'src/decorators/prisma-select.decorator';
@UseGuards(JwtAuthGuard)
@Resolver(() => Company)
@ -25,12 +29,17 @@ export class CompanyResolver {
async findManyCompany(
@Args() args: FindManyCompanyArgs,
@AuthWorkspace() workspace: Workspace,
) {
@PrismaSelector({ modelName: 'Company' })
prismaSelect: PrismaSelect<'Company'>,
): Promise<Partial<Company>[]> {
const preparedArgs = prepareFindManyArgs<FindManyCompanyArgs>(
args,
workspace,
);
return this.companyService.findMany(preparedArgs);
return this.companyService.findMany({
...preparedArgs,
select: prismaSelect.value,
});
}
@UseGuards(UpdateOneGuard)
@ -39,14 +48,17 @@ export class CompanyResolver {
})
async updateOneCompany(
@Args() args: UpdateOneCompanyArgs,
): Promise<Company | null> {
@PrismaSelector({ modelName: 'Company' })
prismaSelect: PrismaSelect<'Company'>,
): Promise<Partial<Company> | null> {
if (!args.data.accountOwner?.connect?.id) {
args.data.accountOwner = { disconnect: true };
}
return this.companyService.update({
...args,
} satisfies UpdateOneCompanyArgs as Prisma.CompanyUpdateArgs);
select: prismaSelect.value,
} as Prisma.CompanyUpdateArgs);
}
@UseGuards(DeleteManyGuard)
@ -68,12 +80,15 @@ export class CompanyResolver {
async createOneCompany(
@Args() args: CreateOneCompanyArgs,
@AuthWorkspace() workspace: Workspace,
): Promise<Company> {
@PrismaSelector({ modelName: 'Company' })
prismaSelect: PrismaSelect<'Company'>,
): Promise<Partial<Company>> {
return this.companyService.create({
data: {
...args.data,
...{ workspace: { connect: { id: workspace.id } } },
},
} satisfies CreateOneCompanyArgs as Prisma.CompanyCreateArgs);
select: prismaSelect.value,
} as Prisma.CompanyCreateArgs);
}
}