graphql로 영화리스트 가져오기

Dan.kimhaejun·2019년 11월 12일
0

Be proud of yourself

목록 보기
2/4

with nicolas, thank you!

  • db.js
import fetch from "node-fetch";

const API_URL =
  "https://yts.lt/api/v2/list_movies.json?limit=50&minimum_rating=9";

export const getMovies = (limit, rating) => {
  let REQUEST_URL = API_URL;
  if (limit > 0) {
    REQUEST_URL += `limit=${limit}`;
  }
  if (rating > 0) {
    REQUEST_URL += `&minimum_rating=${rating}`;
  }
  return fetch(REQUEST_URL)
    .then(res => res.json())
    .then(json => json.data.movies);
};
  • resolvers.js
import { getMovies } from "./db";

const resolvers = {
  Query: {
    movies: (_, { limit, rating }) => getMovies(limit, rating)
  }
};

export default resolvers;
  • schema.graphql
type Movie {
  id: Int!
  title: String!
  rating: Float!
  summary: String
  language: String
  medium_cover_image: String
}

type Query {
  movies(limit: Int, rating: Float): [Movie]!
}
profile
제가 겪은 이슈에 대해서 정리합니다. 기억보다는 기록이 더 낫다고 생각합니다.

0개의 댓글