add service mutation

This commit is contained in:
2024-10-11 18:07:03 +07:00
parent a454a91e0a
commit 244a6f3015

View File

@@ -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;
// },
// }),
// }));
// }
}