29. Message( model, typeDefs)

jinyinshu·2022년 4월 29일
0

insta-backend

목록 보기
29/37

insta에서 DM(direct message)보내는 방법.

backend에서 가장 어려운 부분이니 집중 또 집중, 복습 또 복습 하자

1. model (Message, Room) in schema.prisma

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model User {
  id        Int       @id @default(autoincrement())
  username  String    @unique
  email     String    @unique
  password  String
  bio       String?
  avatar    String?
  followers User[]    @relation("FollowRelation", references: [id])
  following User[]    @relation("FollowRelation", references: [id])
  createdAt DateTime  @default(now())
  updatedAt DateTime  @updatedAt
  photos    Photo[]
  likes     Like[]
  comments  Comment[]
  rooms     Room[]

  messages Message[]
}

model Photo {
  id        Int       @id @default(autoincrement())
  user      User      @relation(fields: [userId], references: [id])
  userId    Int
  file      String
  caption   String?
  hashtags  Hashtag[]
  likes     Like[]
  createdAt DateTime  @default(now())
  updatedAt DateTime  @updatedAt
  comments  Comment[]
}

model Hashtag {
  id        Int      @id @default(autoincrement())
  hashtag   String   @unique
  photos    Photo[]
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

model Like {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  photo     Photo    @relation(fields: [photoId], references: [id])
  user      User     @relation(fields: [userId], references: [id])
  photoId   Int
  userId    Int

  @@unique([photoId, userId])
}

model Comment {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  user      User     @relation(fields: [userId], references: [id])
  photo     Photo    @relation(fields: [photoId], references: [id])
  payload   String
  userId    Int
  photoId   Int
}

model Room {
  id        Int       @id @default(autoincrement())
  createdAt DateTime  @default(now())
  updatedAt DateTime  @updatedAt
  users     User[]
  messages  Message[]
}

model Message {
  id        Int      @id @default(autoincrement())
  payload   String
  user      User     @relation(fields: [userId], references: [id])
  userId    Int
  room      Room     @relation(fields: [roomId], references: [id])
  roomId    Int
  read      Boolean  @default(false)
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

이렇게 통으로 올리는 이유는 model들 간의 relation을 확인하라고~

2. message/message.typedDefs.js

import { gql } from 'apollo-server'

export default gql`
  type Message {
    id: Int!
    payload: String!
    user: User!
    room: Room!
    read: Boolean!
    createdAt: String!
    updatedAt: String!
  }
  type Room {
    id: Int!
    users: [User]
    unreadTotal: Int!
    messages: [Message]
    createdAt: String!
    updatedAt: String!
  }
`

schema.prisma와 비교해 가면서 Message와 Room 확인바람.

3. message/message.resolvers.js

computed Filed를 미이 만들어 놓은 것들을 확인하자!!

import prisma from '../client'

export default {
  Room: {
    users: ({ id }) => prisma.room.findUnique({ where: { id } }).users(),
    messages: ({ id }) =>
      prisma.message.findMany({
        where: { roomId: id },
      }),
    unreadTotal: ({ id }, _, { loggedInUser }) => {
      if (!loggedInUser) {
        return 0
      }
      return prisma.message.count({
        where: {
          read: false,
          roomId: id,
          user: {
            id: {
              not: loggedInUser.id,
            },
          },
        },
      })
    },
  },
  Message: {
    user: ({ id }) => prisma.message.findUnique({ where: { id } }).user(),
  },
}

1. Room에 users, messages, unreadTotal 만들어줌
2. Message에 user 만들어줌.
3. computed Field 를 언제, 어떤거를 만들어 줘야 하는지에 대해서 다시한번 더 확실히 이해할 필요가 있음

profile
코딩하는초딩쌤

0개의 댓글