Files
epess-web-backend/src/workshop/workshop.schema.ts

116 lines
3.2 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 WorkshopSchema extends PothosSchema {
constructor(
@Inject(SchemaBuilderToken) private readonly builder: Builder,
private readonly prisma: PrismaService,
) {
super();
}
@PothosRef()
workshop() {
return this.builder.prismaObject('Workshop', {
fields: (t) => ({
id: t.exposeID('id'),
title: t.exposeString('title'),
description: t.exposeString('description'),
staffId: t.exposeID('staffId'),
serviceId: t.exposeID('serviceId'),
date: t.expose('date', {
type: 'DateTime',
}),
createdAt: t.expose('createdAt', {
type: 'DateTime',
}),
updatedAt: t.expose('updatedAt', {
type: 'DateTime',
}),
service: t.relation('service'),
workshopOrganization: t.relation('workshopOrganization'),
workshopSubscription: t.relation('workshopSubscription'),
staff: t.relation('staff'),
}),
});
}
@Pothos()
init(): void {
this.builder.queryFields((t) => ({
workshop: t.prismaField({
type: this.workshop(),
args: this.builder.generator.findUniqueArgs('Workshop'),
resolve: async (query, root, args, ctx, info) => {
return await this.prisma.workshop.findUnique({
...query,
where: args.where,
});
},
}),
workshops: t.prismaField({
type: [this.workshop()],
args: this.builder.generator.findManyArgs('Workshop'),
resolve: async (query, root, args, ctx, info) => {
return await this.prisma.workshop.findMany({
...query,
skip: args.skip ?? 0,
take: args.take ?? 10,
orderBy: args.orderBy ?? undefined,
where: args.filter ?? undefined,
});
},
}),
}));
// Mutations section
this.builder.mutationFields((t) => ({
createWorkshop: t.prismaField({
type: this.workshop(),
args: {
input: t.arg({
type: this.builder.generator.getCreateInput('Workshop'),
required: true,
}),
},
resolve: async (query, root, args, ctx, info) => {
return await this.prisma.workshop.create({
...query,
data: args.input,
});
},
}),
updateWorkshop: t.prismaField({
type: this.workshop(),
args: {
input: t.arg({
type: this.builder.generator.getUpdateInput('Workshop'),
required: true,
}),
where: t.arg({
type: this.builder.generator.getWhereUnique('Workshop'),
required: true,
}),
},
resolve: async (query, root, args, ctx, info) => {
return await this.prisma.workshop.update({
...query,
where: args.where,
data: args.input,
});
},
}),
}));
}
}