23. deleteComment

김종민·2022년 4월 26일
0

insta-backend

목록 보기
23/37

1. deleteComment.typeDefs.js

import { gql } from 'apollo-server'

export default gql`
  type DeleteCommentResult {
    ok: Boolean!
    error: String
  }
  type Mutation {
    deleteComment(id: Int!): DeleteCommentResult
  }
`

2. deleteComment.resolvers.js

import prisma from '../../client'
import { protectedResolver } from '../../users/users.util'

export default {
  Mutation: {
    deleteComment: protectedResolver(async (_, { id }, { loggedInUser }) => {
      const comment = await prisma.comment.findUnique({
        where: { id },
        select: { userId: true },
      })
      if (!comment) {
        return {
          ok: false,
          error: 'Comment not found',
        }
      } else if (comment.userId !== loggedInUser.id) {
        return {
          ok: false,
          error: 'Not Authorized',
        }
      } else {
        await prisma.comment.delete({
          where: { id },
        })
        return {
          ok: true,
        }
      }
    }),
  },
}

크게 어려운건 없지만, 머리가 나쁘니 한번씩 코드 읽어 봅시다~

profile
코딩하는초딩쌤

3개의 댓글

comment-user-thumbnail
2023년 6월 26일

Papa's Pizzeria teaches you the fine art of customer service as you enjoy freshly baked pizzas. https://papaspizzeriagame.io

답글 달기
comment-user-thumbnail
2023년 7월 21일

The information you share is excellent and exciting, and thanks to that, I know more valuable things. Keep posting interesting things and I will keep an eye on your posts.

답글 달기
comment-user-thumbnail
2023년 7월 21일
답글 달기