import { Inject, Injectable } from '@nestjs/common'; import { PothosRef, PothosSchema, SchemaBuilderToken, } from '@smatch-corp/nestjs-pothos'; import { Builder } from 'src/Graphql/graphql.builder'; import { AppConfigService } from './appconfig.service'; import { PrismaService } from 'src/Prisma/prisma.service'; @Injectable() export class AppConfigSchema extends PothosSchema { constructor( @Inject(SchemaBuilderToken) private readonly builder: Builder, private readonly appConfigService: AppConfigService, private readonly prisma: PrismaService, ) { super(); } @PothosRef() appConfig() { return this.builder.prismaObject('Config', { description: 'An app config', fields: (t) => ({ id: t.exposeID('id', { description: 'The unique identifier for the config', }), name: t.exposeString('name', { description: 'The name of the config', }), key: t.exposeString('key', { description: 'The key of the config', }), value: t.exposeString('value', { description: 'The value of the config', }), visible: t.exposeBoolean('visible', { description: 'Whether the config is visible', }), }), }); } @PothosRef() init(): void { this.builder.queryFields((t) => ({ appConfigs: t.prismaField({ type: [this.appConfig()], description: 'Get all app configs', args: this.builder.generator.findManyArgs('Config'), resolve: async (query, root, args) => { return await this.prisma.config.findMany({ ...query, where: args.filter ?? undefined, orderBy: args.orderBy ?? undefined, cursor: args.cursor ?? undefined, skip: args.skip ?? undefined, take: args.take ?? undefined, }); }, }), appConfig: t.prismaField({ type: this.appConfig(), description: 'Get an app config by key', args: this.builder.generator.findUniqueArgs('Config'), resolve: async (query, root, args) => { return await this.prisma.config.findUnique({ ...query, where: args.where ?? undefined, }); }, }), })); // Mutations this.builder.mutationFields((t) => ({ createAppConfig: t.prismaField({ type: this.appConfig(), description: 'Create an app config', args: { input: t.arg({ type: this.builder.generator.getCreateInput('Config'), required: true, }), }, resolve: async (query, root, args) => { return await this.prisma.config.create({ ...query, data: args.input, }); }, }), createAppConfigs: t.prismaField({ type: [this.appConfig()], description: 'Create multiple app configs', args: { input: t.arg({ type: this.builder.generator.getCreateManyInput('Config'), required: true, }), }, resolve: async (query, root, args) => { return await this.prisma.config.createManyAndReturn({ ...query, data: args.input, }); }, }), })); } }