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