Skip to content

Commit

Permalink
feat(prisma): make resolvers testable with PrismaSelectService
Browse files Browse the repository at this point in the history
undefined
  • Loading branch information
ZenSoftware committed Jan 30, 2023
1 parent 01e8f4c commit 1300ddd
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 53 deletions.
25 changes: 13 additions & 12 deletions apps/api/src/app/graphql/resolvers/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { GraphQLResolveInfo } from 'graphql';
import { gql } from 'graphql-tag';

import { AppAbility, AuthService, DEFAULT_FIELDS_TOKEN } from '../../auth';
import { DefaultFields, PrismaSelectArgs, PrismaService, User } from '../../prisma';
import { DefaultFields, PrismaSelectService, PrismaService, User } from '../../prisma';
import {
AggregateUserArgs,
CreateOneUserArgs,
Expand Down Expand Up @@ -34,6 +34,7 @@ export class UserResolver {
constructor(
@Inject(DEFAULT_FIELDS_TOKEN) private readonly defaultFields: DefaultFields,
private readonly prisma: PrismaService,
private readonly prismaSelect: PrismaSelectService,
private readonly auth: AuthService
) {}

Expand All @@ -55,7 +56,7 @@ export class UserResolver {
@CaslAbility() ability: AppAbility
) {
const record = await this.prisma.user.findUnique(
PrismaSelectArgs(info, args, this.defaultFields)
this.prismaSelect.getArgs(info, args, this.defaultFields)
);
if (ability.cannot('read', subject('User', record as User))) throw new ForbiddenException();
return record;
Expand All @@ -68,7 +69,7 @@ export class UserResolver {
@CaslAbility() ability: AppAbility
) {
const record = await this.prisma.user.findFirst(
PrismaSelectArgs(info, args, this.defaultFields)
this.prismaSelect.getArgs(info, args, this.defaultFields)
);
if (ability.cannot('read', subject('User', record as User))) throw new ForbiddenException();
return record;
Expand All @@ -81,7 +82,7 @@ export class UserResolver {
@CaslAbility() ability: AppAbility
) {
const records = await this.prisma.user.findMany(
PrismaSelectArgs(info, args, this.defaultFields)
this.prismaSelect.getArgs(info, args, this.defaultFields)
);
for (const record of records) {
if (ability.cannot('read', subject('User', record))) throw new ForbiddenException();
Expand All @@ -96,7 +97,7 @@ export class UserResolver {
@CaslAbility() ability: AppAbility
) {
if (ability.cannot('read', 'User')) throw new ForbiddenException();
return this.prisma.user.count(PrismaSelectArgs(info, args));
return this.prisma.user.count(this.prismaSelect.getArgs(info, args));
}

@Query()
Expand All @@ -106,7 +107,7 @@ export class UserResolver {
@CaslAbility() ability: AppAbility
) {
if (ability.cannot('read', 'User')) throw new ForbiddenException();
return this.prisma.user.aggregate(PrismaSelectArgs(info, args));
return this.prisma.user.aggregate(this.prismaSelect.getArgs(info, args));
}

@Mutation()
Expand All @@ -116,7 +117,7 @@ export class UserResolver {
@CaslAbility() ability: AppAbility
) {
if (ability.cannot('create', subject('User', args.data as any))) throw new ForbiddenException();
return this.prisma.user.create(PrismaSelectArgs(info, args));
return this.prisma.user.create(this.prismaSelect.getArgs(info, args));
}

@Mutation()
Expand All @@ -130,7 +131,7 @@ export class UserResolver {
select: this.defaultFields.User,
});
if (ability.cannot('update', subject('User', record as User))) throw new ForbiddenException();
return this.prisma.user.update(PrismaSelectArgs(info, args));
return this.prisma.user.update(this.prismaSelect.getArgs(info, args));
}

@Mutation()
Expand All @@ -146,7 +147,7 @@ export class UserResolver {
for (const record of records) {
if (ability.cannot('update', subject('User', record as User))) throw new ForbiddenException();
}
return this.prisma.user.updateMany(PrismaSelectArgs(info, args));
return this.prisma.user.updateMany(this.prismaSelect.getArgs(info, args));
}

@Mutation()
Expand All @@ -165,7 +166,7 @@ export class UserResolver {
) {
throw new ForbiddenException();
}
return this.prisma.user.upsert(PrismaSelectArgs(info, args));
return this.prisma.user.upsert(this.prismaSelect.getArgs(info, args));
}

@Mutation()
Expand All @@ -179,7 +180,7 @@ export class UserResolver {
select: this.defaultFields.User,
});
if (ability.cannot('delete', subject('User', record as User))) throw new ForbiddenException();
return this.prisma.user.delete(PrismaSelectArgs(info, args));
return this.prisma.user.delete(this.prismaSelect.getArgs(info, args));
}

@Mutation()
Expand All @@ -195,6 +196,6 @@ export class UserResolver {
for (const record of records) {
if (ability.cannot('delete', subject('User', record as User))) throw new ForbiddenException();
}
return this.prisma.user.deleteMany(PrismaSelectArgs(info, args));
return this.prisma.user.deleteMany(this.prismaSelect.getArgs(info, args));
}
}
2 changes: 1 addition & 1 deletion apps/api/src/app/prisma/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './default-fields';
export * from './generated';
export * from './prisma-select-args';
export * from './prisma-select.service';
export * from './prisma.module';
export * from './prisma.service';
25 changes: 0 additions & 25 deletions apps/api/src/app/prisma/prisma-select-args.ts

This file was deleted.

25 changes: 25 additions & 0 deletions apps/api/src/app/prisma/prisma-select.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Injectable } from '@nestjs/common';
import { PrismaSelect } from '@paljs/plugins';
import { GraphQLResolveInfo } from 'graphql';

import { PalDefaultFields } from './default-fields';
import { Prisma } from './generated';

@Injectable()
export class PrismaSelectService {
getArgs(info: GraphQLResolveInfo, args: object, defaultFields?: PalDefaultFields) {
const result = new PrismaSelect(info, {
defaultFields: defaultFields as any,
dmmf: [Prisma.dmmf],
}).value;

if (!result.select || Object.keys(result.select).length > 0) {
return {
...args,
...result,
};
}

return args;
}
}
5 changes: 3 additions & 2 deletions apps/api/src/app/prisma/prisma.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Module } from '@nestjs/common';

import { PrismaSelectService } from './prisma-select.service';
import { PrismaService } from './prisma.service';

@Module({
providers: [PrismaService],
exports: [PrismaService],
providers: [PrismaService, PrismaSelectService],
exports: [PrismaService, PrismaSelectService],
})
export class PrismaModule {}
27 changes: 14 additions & 13 deletions tools/templates/graphql-resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { CaslAbility, CaslGuard } from '@zen/nest-auth';
import { GraphQLResolveInfo } from 'graphql';
import { AppAbility, DEFAULT_FIELDS_TOKEN } from '../../auth';
import { DefaultFields, PrismaSelectArgs, PrismaService, ${name} } from '../../prisma';
import { DefaultFields, PrismaSelectService, PrismaService, ${name} } from '../../prisma';
import {
Aggregate${name}Args,
CreateOne${name}Args,
Expand Down Expand Up @@ -42,7 +42,8 @@ export const typeDefs = null;
export class ${name}Resolver {
constructor(
@Inject(DEFAULT_FIELDS_TOKEN) private readonly defaultFields: DefaultFields,
private readonly prisma: PrismaService
private readonly prisma: PrismaService,
private readonly prismaSelect: PrismaSelectService
) {}
@Query()
Expand All @@ -52,7 +53,7 @@ export class ${name}Resolver {
@CaslAbility() ability: AppAbility
) {
const record = await this.prisma.${lowercase(name)}.findUnique(
PrismaSelectArgs(info, args, this.defaultFields)
this.prismaSelect.getArgs(info, args, this.defaultFields)
);
if (ability.cannot('read', subject('${name}', record as ${name}))) throw new ForbiddenException();
return record;
Expand All @@ -65,7 +66,7 @@ export class ${name}Resolver {
@CaslAbility() ability: AppAbility
) {
const record = await this.prisma.${lowercase(name)}.findFirst(
PrismaSelectArgs(info, args, this.defaultFields)
this.prismaSelect.getArgs(info, args, this.defaultFields)
);
if (ability.cannot('read', subject('${name}', record as ${name}))) throw new ForbiddenException();
return record;
Expand All @@ -78,7 +79,7 @@ export class ${name}Resolver {
@CaslAbility() ability: AppAbility
) {
const records = await this.prisma.${lowercase(name)}.findMany(
PrismaSelectArgs(info, args, this.defaultFields)
this.prismaSelect.getArgs(info, args, this.defaultFields)
);
for (const record of records) {
if (ability.cannot('read', subject('${name}', record))) throw new ForbiddenException();
Expand All @@ -93,7 +94,7 @@ export class ${name}Resolver {
@CaslAbility() ability: AppAbility
) {
if (ability.cannot('read', '${name}')) throw new ForbiddenException();
return this.prisma.${lowercase(name)}.count(PrismaSelectArgs(info, args));
return this.prisma.${lowercase(name)}.count(this.prismaSelect.getArgs(info, args));
}
@Query()
Expand All @@ -103,7 +104,7 @@ export class ${name}Resolver {
@CaslAbility() ability: AppAbility
) {
if (ability.cannot('read', '${name}')) throw new ForbiddenException();
return this.prisma.${lowercase(name)}.aggregate(PrismaSelectArgs(info, args));
return this.prisma.${lowercase(name)}.aggregate(this.prismaSelect.getArgs(info, args));
}
@Mutation()
Expand All @@ -113,7 +114,7 @@ export class ${name}Resolver {
@CaslAbility() ability: AppAbility
) {
if (ability.cannot('create', subject('${name}', args.data as any))) throw new ForbiddenException();
return this.prisma.${lowercase(name)}.create(PrismaSelectArgs(info, args));
return this.prisma.${lowercase(name)}.create(this.prismaSelect.getArgs(info, args));
}
@Mutation()
Expand All @@ -127,7 +128,7 @@ export class ${name}Resolver {
select: this.defaultFields.${name},
});
if (ability.cannot('update', subject('${name}', record as ${name}))) throw new ForbiddenException();
return this.prisma.${lowercase(name)}.update(PrismaSelectArgs(info, args));
return this.prisma.${lowercase(name)}.update(this.prismaSelect.getArgs(info, args));
}
@Mutation()
Expand All @@ -143,7 +144,7 @@ export class ${name}Resolver {
for (const record of records) {
if (ability.cannot('update', subject('${name}', record as ${name}))) throw new ForbiddenException();
}
return this.prisma.${lowercase(name)}.updateMany(PrismaSelectArgs(info, args));
return this.prisma.${lowercase(name)}.updateMany(this.prismaSelect.getArgs(info, args));
}
@Mutation()
Expand All @@ -162,7 +163,7 @@ export class ${name}Resolver {
) {
throw new ForbiddenException();
}
return this.prisma.${lowercase(name)}.upsert(PrismaSelectArgs(info, args));
return this.prisma.${lowercase(name)}.upsert(this.prismaSelect.getArgs(info, args));
}
@Mutation()
Expand All @@ -176,7 +177,7 @@ export class ${name}Resolver {
select: this.defaultFields.${name},
});
if (ability.cannot('delete', subject('${name}', record as ${name}))) throw new ForbiddenException();
return this.prisma.${lowercase(name)}.delete(PrismaSelectArgs(info, args));
return this.prisma.${lowercase(name)}.delete(this.prismaSelect.getArgs(info, args));
}
@Mutation()
Expand All @@ -192,7 +193,7 @@ export class ${name}Resolver {
for (const record of records) {
if (ability.cannot('delete', subject('${name}', record as ${name}))) throw new ForbiddenException();
}
return this.prisma.${lowercase(name)}.deleteMany(PrismaSelectArgs(info, args));
return this.prisma.${lowercase(name)}.deleteMany(this.prismaSelect.getArgs(info, args));
}
}
`;
Expand Down

0 comments on commit 1300ddd

Please sign in to comment.