Files
epess-web-backend/src/Service/service.schema.ts
2024-10-17 15:02:25 +07:00

147 lines
4.3 KiB
TypeScript

import { Inject, Injectable } from '@nestjs/common';
import {
Pothos,
PothosRef,
PothosSchema,
SchemaBuilderToken,
} from '@smatch-corp/nestjs-pothos';
import { Builder } from '../Graphql/graphql.builder';
import { PrismaService } from '../Prisma/prisma.service';
@Injectable()
export class ServiceSchema extends PothosSchema {
constructor(
@Inject(SchemaBuilderToken) private readonly builder: Builder,
private readonly prisma: PrismaService,
) {
super();
}
@PothosRef()
service() {
return this.builder.prismaObject('Service', {
description: 'A service offered by a center.',
fields: (t) => ({
id: t.exposeID('id'),
name: t.exposeString('name'),
description: t.exposeString('description'),
price: t.exposeFloat('price'),
rating: t.exposeFloat('rating'),
imageFile: t.relation('imageFile'),
imageFileId: t.exposeID('imageFileId'),
imageFileUrl: t.exposeString('imageFileUrl'),
createdAt: t.expose('createdAt', {
type: 'DateTime',
nullable: true,
}),
updatedAt: t.expose('updatedAt', {
type: 'DateTime',
nullable: true,
}),
center: t.relation('center'),
centerId: t.exposeID('centerId'),
workshop: t.relation('workshop'),
milestone: t.relation('milestone'),
schedule: t.relation('schedule'),
serviceAndCategory: t.relation('serviceAndCategory'),
workshopOrganization: t.relation('workshopOrganization'),
user: t.relation('user'),
userId: t.exposeID('userId'),
}),
});
}
@Pothos()
init() {
this.builder.queryFields((t) => ({
services: t.prismaField({
description:
'Retrieve a list of services with optional filtering, ordering, and pagination.',
type: [this.service()],
args: this.builder.generator.findManyArgs('Service'),
resolve: async (query, root, args, ctx, info) => {
return await this.prisma.service.findMany({
...query,
where: args.filter ?? undefined,
orderBy: args.orderBy ?? undefined,
skip: args.skip ?? 0,
take: args.take ?? 10,
});
},
}),
service: t.prismaField({
description: 'Retrieve a single service by its unique identifier.',
type: this.service(),
args: {
input: t.arg({
type: this.builder.generator.getWhereUnique('Service'),
required: true,
}),
},
resolve: async (query, root, args, ctx, info) => {
return await this.prisma.service.findUnique({
...query,
where: args.input,
});
},
}),
}));
// Mutation section
this.builder.mutationFields((t) => ({
createService: t.prismaField({
description: 'Create a new service.',
type: this.service(),
args: {
input: t.arg({
type: this.builder.generator.getCreateInput('Service'),
required: true,
}),
},
resolve: async (query, root, args, ctx, info) => {
return await this.prisma.service.create({
data: args.input,
});
},
}),
updateService: t.prismaField({
description: 'Update an existing service.',
type: this.service(),
args: {
input: t.arg({
type: this.builder.generator.getUpdateInput('Service'),
required: true,
}),
where: t.arg({
type: this.builder.generator.getWhereUnique('Service'),
required: true,
}),
},
resolve: async (query, root, args, ctx, info) => {
return await this.prisma.service.update({
...query,
where: args.where,
data: args.input,
});
},
}),
deleteService: t.prismaField({
description: 'Delete an existing service.',
type: this.service(),
args: {
where: t.arg({
type: this.builder.generator.getWhereUnique('Service'),
required: true,
}),
},
resolve: async (query, root, args, ctx, info) => {
return await this.prisma.service.delete({
...query,
where: args.where,
});
},
}),
}));
}
}