sdy

update newMessage resolver

1 import { isAuthenticated, prisma } from "../../../utils"; 1 import { isAuthenticated, prisma } from "../../../utils";
2 -import { NEW_MESSAGE_TOPIC } from "../../../topics"; 2 +import { ONE_TO_ONE_MESSAGE } from "../../../topics";
3 3
4 export default { 4 export default {
5 Mutation: { 5 Mutation: {
6 newMessage: async (_, args, { request, pubsub }) => { 6 newMessage: async (_, args, { request, pubsub }) => {
7 - // isAuthenticated(request); 7 + isAuthenticated(request);
8 const { user } = request; 8 const { user } = request;
9 const { receiverId, message, roomId } = args; 9 const { receiverId, message, roomId } = args;
10 - let room; 10 + let room = await prisma.room.findOne({
11 + where: {
12 + id: roomId,
13 + },
14 + });
15 + room = await prisma.room.update({
16 + where: {
17 + id: roomId,
18 + },
19 + data: {
20 + participants: {
21 + connect: [{ id: user.id }, { id: receiverId }],
22 + },
23 + },
24 + });
11 // 방이 없는 경우 25 // 방이 없는 경우
12 - if (roomId === undefined) { 26 + if (room === undefined || room === null) {
13 // 보내는 사람과 받는 사람이 다른 경우 27 // 보내는 사람과 받는 사람이 다른 경우
14 if (user.id !== receiverId) { 28 if (user.id !== receiverId) {
15 room = await prisma.room.create({ 29 room = await prisma.room.create({
...@@ -29,20 +43,11 @@ export default { ...@@ -29,20 +43,11 @@ export default {
29 }, 43 },
30 }); 44 });
31 } 45 }
32 - } else {
33 - // 방이 있는 경우
34 - room = await prisma.room.findOne({
35 - where: {
36 - id: roomId,
37 - },
38 - });
39 } 46 }
40 if (!room) { 47 if (!room) {
41 throw new Error("There is no room"); 48 throw new Error("There is no room");
42 } 49 }
43 - 50 + const subMessage = await prisma.message.create({
44 - // 메시지 생성부분. (지금은 1명 밖에 안들어간 구조.)
45 - const newMessage = await prisma.message.create({
46 data: { 51 data: {
47 text: message, 52 text: message,
48 to: { 53 to: {
...@@ -62,8 +67,9 @@ export default { ...@@ -62,8 +67,9 @@ export default {
62 }, 67 },
63 }, 68 },
64 }); 69 });
65 - pubsub.publish(NEW_MESSAGE_TOPIC, { newMessage }); 70 + console.log(subMessage);
66 - return newMessage; 71 + pubsub.publish(ONE_TO_ONE_MESSAGE, subMessage);
72 + return subMessage;
67 }, 73 },
68 }, 74 },
69 }; 75 };
......