- database에 없는 스키마를 resolver에서 따로 리턴 할 수 있다.
User: {
fullName({ firstName, lastName }) {
return `${firstName} ${lastName}`;
},
},
- type User에는 fullName 정의가 있어야됨
- 테이블 조인
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를 리턴받음
- 스키마 주석 남기기
"""
Tweet object represents a resource for a Tweet
"""
type Tweet {
id: ID!
text: String!
author: User
}
- 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);
},