graphQL 2

김부릉·2023년 2월 13일
0
  1. database에 없는 스키마를 resolver에서 따로 리턴 할 수 있다.
 User: {
    fullName({ firstName, lastName }) {
      return `${firstName} ${lastName}`;
    },
  },
  • type User에는 fullName 정의가 있어야됨
  1. 테이블 조인
    let tweets = [
     {
       id: "1",
       text: "first one!",
       userId: "2",
     },
     {
       id: "2",
       text: "second one!",
       userId: "1 ",
     },
    ];
    type Tweet {
       id: ID!
       text: String!
       author: User
     }
     
       Tweet: {
       author({ userId }) {
         return users.find((user) => user.id === userId);
       },
     },
  • type Tweet 에서 author는 데이터베이스에 없음. 그러면 아폴로 서버는 author resolver를 검색함. author 함수에서 type User를 리턴받음
  1. 스키마 주석 남기기

  """
  Tweet object represents a resource for a Tweet
  """
  type Tweet {
    id: ID!
    text: String!
    author: User
  }
  • 필드 위에 "3개 쓰고 설명 씀
  1. REST API를 graphQL로 감싸기
  • 외부 API도 똑같이 type을 설정하고 자유롭게 사용 할 수 있다
 allMovies() {
      return fetch("https://yts.mx/api/v2/list_movies.json")
        .then((r) => r.json())
        .then((json) => json.data.movies);
    },

0개의 댓글