createMessage.js
1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { prisma, isAuthenticated } from "../../../utils";
import { NEW_MESSAGE } from "../../../topics";
export default {
Mutation: {
createMessage: async (_, args, { request, pubsub }) => {
isAuthenticated(request);
const { user } = request;
const { message, roomId } = args;
let room = await prisma.room.findOne({
where: {
id: roomId,
},
});
let messageObj = await prisma.message.create({
data: {
text: message,
sender: {
connect: {
id: user.id,
},
},
room: {
connect: {
id: room.id,
},
},
},
});
if (room !== undefined || room !== null) {
// 방이 존재하는 경우
room = await prisma.room.update({
where: {
id: roomId,
},
data: {
messages: {
connect: {
id: messageObj.id,
},
},
},
});
} else {
throw new Error("There is no room");
}
if (messageObj !== undefined) {
pubsub.publish(NEW_MESSAGE, { subMessage: messageObj });
}
return messageObj;
},
},
};