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', { 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({ 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({ 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({ 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({ 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({ 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, }); }, }), })); } }