update many graphql field

This commit is contained in:
2024-10-12 16:34:43 +07:00
parent 244a6f3015
commit ba77bd4e1c
29 changed files with 466 additions and 56 deletions

View File

@@ -0,0 +1,9 @@
import { Global, Module } from '@nestjs/common';
import { CategorySchema } from './category.schema';
@Global()
@Module({
providers: [CategorySchema],
exports: [CategorySchema],
})
export class CategoryModule {}

View File

@@ -0,0 +1,59 @@
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 CategorySchema extends PothosSchema {
constructor(
@Inject(SchemaBuilderToken) private readonly builder: Builder,
private readonly prisma: PrismaService,
) {
super();
}
@PothosRef()
category() {
return this.builder.prismaObject('Category', {
fields: (t) => ({
id: t.exposeID('id'),
name: t.exposeString('name'),
serviceAndCategory: t.relation('serviceAndCategory'),
}),
});
}
@PothosRef()
init(): void {
this.builder.queryFields((t) => ({
categories: t.prismaField({
type: [this.category()],
args: this.builder.generator.findManyArgs('Category'),
resolve: async (query, root, args, ctx, info) => {
return await this.prisma.category.findMany({
...query,
skip: args.skip ?? undefined,
take: args.take ?? 10,
orderBy: args.orderBy ?? undefined,
where: args.filter ?? undefined,
});
},
}),
category: t.prismaField({
type: this.category(),
args: this.builder.generator.findUniqueArgs('Category'),
resolve: async (query, root, args, ctx, info) => {
return await this.prisma.category.findUnique({
...query,
where: args.where ?? undefined,
});
},
}),
}));
}
}