wholeMessage.js 1019 Bytes
import { prisma, isAuthenticated } from "../../../utils";

export default {
  Mutation: {
    wholeMessage: async (_, args, { request }) => {
      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,
            },
          },
        },
      });

      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");
      }
      return messageObj;
    },
  },
};