sdy

update newMessage resolver

import { isAuthenticated, prisma } from "../../../utils";
import { NEW_MESSAGE_TOPIC } from "../../../topics";
import { ONE_TO_ONE_MESSAGE } from "../../../topics";
export default {
Mutation: {
newMessage: async (_, args, { request, pubsub }) => {
// isAuthenticated(request);
isAuthenticated(request);
const { user } = request;
const { receiverId, message, roomId } = args;
let room;
let room = await prisma.room.findOne({
where: {
id: roomId,
},
});
room = await prisma.room.update({
where: {
id: roomId,
},
data: {
participants: {
connect: [{ id: user.id }, { id: receiverId }],
},
},
});
// 방이 없는 경우
if (roomId === undefined) {
if (room === undefined || room === null) {
// 보내는 사람과 받는 사람이 다른 경우
if (user.id !== receiverId) {
room = await prisma.room.create({
......@@ -29,20 +43,11 @@ export default {
},
});
}
} else {
// 방이 있는 경우
room = await prisma.room.findOne({
where: {
id: roomId,
},
});
}
if (!room) {
throw new Error("There is no room");
}
// 메시지 생성부분. (지금은 1명 밖에 안들어간 구조.)
const newMessage = await prisma.message.create({
const subMessage = await prisma.message.create({
data: {
text: message,
to: {
......@@ -62,8 +67,9 @@ export default {
},
},
});
pubsub.publish(NEW_MESSAGE_TOPIC, { newMessage });
return newMessage;
console.log(subMessage);
pubsub.publish(ONE_TO_ONE_MESSAGE, subMessage);
return subMessage;
},
},
};
......