import { Prisma } from '@prisma/client' ; import { Resolver , Query , Arg , Int , Mutation } from 'type-graphql' ; import { CreateChapterInputs , UpdateChapterInputs } from './inputs' ; import { Chapter , ChapterWithRelations } from 'src/graphql-types' ; import { prisma } from 'src/prisma' ; @ Resolver ( ) export class ChapterResolver { @ Query ( ( ) => [ Chapter ] ) async chapters ( ) { return await prisma . chapters . findMany ( ) ; } @ Query ( ( ) => ChapterWithRelations , { nullable : true } ) async chapter ( @ Arg ( 'id' , ( ) => Int ) id ) { return await prisma . chapters . findUnique ( { where : { id } , include : { events : { include : { tags : true } } , users : { include : { user : true } } , } , } ) ; } @ Mutation ( ( ) => Chapter ) async createChapter ( @ Arg ( 'data' ) data ) { const user = await prisma . users . findFirst ( ) ; const chapterData = { ... data , creator_id : user ?. id , } ; return prisma . chapters . create ( { data : chapterData } ) ; } @ Mutation ( ( ) => Chapter ) async updateChapter ( @ Arg ( 'id' , ( ) => Int ) id , @ Arg ( 'data' ) data ) { const chapterData = data ; return prisma . chapters . update ( { where : { id } , data : chapterData } ) ; } @ Mutation ( ( ) => Boolean ) async deleteChapter ( @ Arg ( 'id' , ( ) => Int ) id ) { await prisma . chapters . delete ( { where : { id } } ) ; return true ; } }