프로젝트
[인스타그램 클론코딩] #3.21 editPost deletePost Resolver
jhkang-dev
2020. 9. 18. 00:13
안녕하세요 강정호 입니다
오늘은 포스팅을 수정, 삭제하는 API를 개발하겠습니다.
editPost.graphql
// ACTIONS 라는 enum 타입 객체 생성
enum ACTIONS {
EDIT
DELETE
}
// Resolver에 아래와 같이 수정할 데이터변수(caption, location) 넣고, action 변수를 생성한다.
// action변수는 enum 데이터로 Resolver에서 어떻게 처리해주는지 행동 옵션
type Mutation {
editPost(id: String! ,caption: String ,location: String ,action: ACTIONS): Post
}
editPost.js
import { prisma } from "../../../../generated/prisma-client";
// 삭제, 수정을 상수값으로 선언
const DELETE = "DELETE";
const EDIT = "EDIT";
export default {
Mutation: {
editPost: async (parent, args, { request, isAuthenticated }, info) => {
isAuthenticated(request);
//4개의 아규먼트를 받았다.
const { id, caption, location, action } = args;
const user = request;
// 본인이 작성한 post만 수정해야 하므로 $exists로 본인이 작성한 post가 맞는지 조회
const post = await prisma.$exists.post({ id, user: { id: user.id } });
// post가 존재한다면
if(post){
// action 옵션이 EDIT면 수정
if(action === EDIT){
return await prisma.updatePost({
where: { id },
data: {
caption,
location
}
});
}else if(action === DELETE){ //action 옵션이 DELETE면 삭제
return await prisma.deletePost({id});
}
}else{ //post가 존재하지 않으면 오류
throw Error("Post doesnt exist. Try again");
}
}
}
}
테스트 editPost 수정

결과 editPost 포스팅 수정

테스트 포스팅 삭제

포스팅 삭제 결과
