diff --git a/src/service/service.schema.ts b/src/service/service.schema.ts index 0fca557..f53c8a5 100644 --- a/src/service/service.schema.ts +++ b/src/service/service.schema.ts @@ -5,9 +5,8 @@ import { PothosSchema, SchemaBuilderToken, } from '@smatch-corp/nestjs-pothos'; -import { Builder, BuilderTypes } from '../graphql/graphql.builder'; +import { Builder } from '../graphql/graphql.builder'; import { PrismaService } from 'src/prisma/prisma.service'; -import { PrismaCrudGenerator } from 'src/graphql/graphql.generator'; @Injectable() export class ServiceSchema extends PothosSchema { @@ -44,62 +43,83 @@ export class ServiceSchema extends PothosSchema { this.builder.queryFields((t) => ({ services: t.prismaField({ type: [this.service()], - args: { - skip: t.arg.int(), - take: t.arg.int(), - cursor: t.arg.string(), - where: t.arg.string(), - orderBy: t.arg.string(), - }, + args: this.builder.generator.findManyArgs('Service'), resolve: async (query, root, args, ctx, info) => { - const { skip, take, cursor, where, orderBy } = args; - const services = await this.prisma.service.findMany({ - skip: skip || 0, - take: take || 10, - cursor: cursor ? { id: cursor } : undefined, - where: where ? JSON.parse(where) : undefined, - orderBy: orderBy ? JSON.parse(orderBy) : undefined, + return await this.prisma.service.findMany({ + ...query, + where: args.filter ?? undefined, + orderBy: args.orderBy ?? undefined, + skip: args.skip ?? undefined, + take: args.take ?? undefined, }); - return services; }, }), service: t.prismaField({ type: this.service(), args: { - id: t.arg.string({ + input: t.arg({ + type: this.builder.generator.getWhereUnique('Service'), required: true, }), }, resolve: async (query, root, args, ctx, info) => { - const { id } = args; - const service = await this.prisma.service.findUnique({ - where: { id }, + 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, }); - return service; }, }), })); } - - // @Pothos() - // initMutation() { - // this.builder.mutationFields((t) => ({ - // createService: t.prismaField({ - // type: this.service(), - // args: { - // input: t.arg({ - // type: this.generator.getCreateInput('Service'), - // required: true, - // }), - // }, - // resolve: async (query, root, args, ctx, info) => { - // const { input } = args; - // const service = await this.prisma.service.create({ - // data: input, - // }); - // return service; - // }, - // }), - // })); - // } }