Files
epess-web-backend/src/ServiceAndCategory/serviceandcategory.schema.ts
2024-10-28 02:56:36 +07:00

62 lines
1.9 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 ServiceAndCategorySchema extends PothosSchema {
constructor(
@Inject(SchemaBuilderToken) private readonly builder: Builder,
private readonly prisma: PrismaService,
) {
super();
}
@PothosRef()
serviceAndCategory() {
return this.builder.prismaObject('ServiceAndCategory', {
description: 'A service and category in the system.',
fields: (t) => ({
serviceId: t.exposeID('serviceId', {
description: 'The ID of the service.',
}),
service: t.relation('service', {
description: 'The service for the service and category.',
}),
subCategory: t.relation('subCategory', {
description: 'The sub category for the service and category.',
}),
subCategoryId: t.exposeID('subCategoryId', {
description: 'The ID of the sub category.',
}),
}),
});
}
@Pothos()
init(): void {
this.builder.queryFields((t) => ({
serviceAndCategories: t.prismaField({
type: [this.serviceAndCategory()],
args: this.builder.generator.findManyArgs('ServiceAndCategory'),
description:
'Retrieve a list of service and categories with optional filtering, ordering, and pagination.',
resolve: async (query, root, args, ctx, info) => {
return await this.prisma.serviceAndCategory.findMany({
...query,
skip: args.skip ?? undefined,
take: args.take ?? undefined,
orderBy: args.orderBy ?? undefined,
where: args.filter ?? undefined,
});
},
}),
}));
}
}