본문 바로가기

GraphQL BackEnd/Lecture

GraphQL + Prisma (1) 프리즈마 데이터 모델 분석

 

 

# 1. 프리즈마 데이터 모델을 작성해 봅시다.

- 새로운 타입 정의 하기. User라고 정의하면 

type User{이안에 데이터 필드를 채우면 됩니다.}

# 2 . 1개의 데이터 타입은 prisma가 자동으로 쿼리랑 뮤테이션을 만들어 줍니다.

한마디로, 나는 User객체만 만들었는데, 자동으로 CURD 관련 기능을 만들어 줍니다.!

- 쿼리 users, user, usersConnection

- 뮤테이션 createUser, updateUser , deleteUser, upsertUser, 

- 뮤테이션  updateManyUsers,  deleteManyUsers

 

## 2. 데이터 요소 정의하기

 

## 2.0 id 정의하기

type User {
   id: ID! @id

...

}

 

## 2.1  String으로 필드 만들기 + 생성 및 업데이트 기록하기

   id: ID! @id




   name: String! @unique

   name: String! 

   name: String

   name: String! @default(value: "")



   createdAt: DateTime! @createdAt

   updatedAt: DateTime! @updatedAt 



## 2.2 데이터 relation

   posts: [Post!]! @relation(name: "PostsOfUser", onDelete: CASCADE)

   user: User @relation(name: "PostsOfUser")

 

 

## 2.2 데이터 relation 같은 타입내



   following: [User!]! @relation(name: "FollowRelation")

   followers: [User!]! @relation(name: "FollowRelation")

 

# 3. 예제  인스타그램 클론의 데이터모델

type User {
   id: ID! @id
   avatar: String
   name: String! @unique
   email: String! @unique
   firstName: String @default(value: "")
   lastName: String
   bio: String
   following: [User!]! @relation(name: "FollowRelation")
   followers: [User!]! @relation(name: "FollowRelation")
   posts: [Post!]! @relation(name: "PostsOfUser", onDelete: CASCADE)
   likes: [Like!]! @relation(name: "LikesOfUser", onDelete: CASCADE)
   comments: [Comment!]! @relation(name: "CommentsOfUser", onDelete: CASCADE)
   rooms: [Room!]!
   loginSecret: String
   createdAt: DateTime! @createdAt
   updatedAt: DateTime! @updatedAt 
 }

type Post {
  id: ID! @id
  location: String
  caption: String!
  user: User @relation(name: "PostsOfUser")
  files: [File!]! @relation(name: "FilesOfPost", onDelete: CASCADE)
  likes: [Like!]! @relation(name: "LikeOfPost", onDelete: CASCADE)
  comments: [Comment!]! @relation(name: "CommentOfPost", onDelete: CASCADE)
  createdAt: DateTime! @createdAt
  updatedAt: DateTime! @updatedAt   
}

type Like {
  id: ID! @id
  user: User @relation(name: "LikesOfUser")
  post: Post @relation(name: "LikeOfPost")
  createdAt: DateTime! @createdAt
  updatedAt: DateTime! @updatedAt  
}

type Comment {
  id: ID! @id
  text: String!
  user: User @relation(name: "CommentsOfUser")
  post: Post @relation(name: "CommentOfPost")
  createdAt: DateTime! @createdAt
  updatedAt: DateTime! @updatedAt  

}

type File {
  id: ID! @id
  url: String!
  post: Post @relation(name: "FilesOfPost")
  createdAt: DateTime! @createdAt
  updatedAt: DateTime! @updatedAt  
}

type Room {
  id: ID! @id
  participants: [User!]!
  messages: [Message!]!
  createdAt: DateTime! @createdAt
  updatedAt: DateTime! @updatedAt  
}

type Message {
   id: ID! @id
  text: String!
  from: User! @relation(name: "From")
  to: User! @relation(name: "To")
  room: Room!
  createdAt: DateTime! @createdAt
  updatedAt: DateTime! @updatedAt  
}

 

 

DOS IMPACT - WEB Developer

KIM DO YOUNG

WEB : REACT JS | REACT NATIVE | GraphQL PRISMA