import { Arg , Authorized , Ctx , Mutation , Resolver } from 'type-graphql' ; import { ContextType } from '@root/server' ; import Post from '@entities/Post' ; import { upvoteDownvote } from '@utils/votes/upvoteDownvoteEntity' ; import type User from '@entities/User' ; const action = async ( upvote , postId , user ) => upvoteDownvote < Post > ( await Post . findOne ( postId ) , user , upvote , ! upvote , ( user ) => user . upvotedPosts , ( user ) => user . downvotedPosts , ( user , entities ) => ( user . upvotedPosts = Promise . resolve ( entities ) ) , ( user , entities ) => ( user . downvotedPosts = Promise . resolve ( entities ) ) ) ; @ Resolver ( ( ) => Post ) export default class UpvoteDownvotePostResolver { @ Mutation ( ( ) => Boolean ) @ Authorized ( ) async upvotePost ( @ Arg ( 'postId' ) postId , @ Ctx ( ) { getUser } ) { return action ( true , postId , await getUser ( ) ) ; } @ Mutation ( ( ) => Boolean ) @ Authorized ( ) async downvotePost ( @ Arg ( 'postId' ) postId , @ Ctx ( ) { getUser } ) { return action ( false , postId , await getUser ( ) ) ; } }