kok202
GraphQL 강의 (10~11 : Wrapping REST API)

2019. 8. 10. 03:59[공부] 영상/GraphQL

RestAPI 로 이미 사용하고 있는 곳에 GraphQL 으로 wrapping 하여 관리하는 예제

graphql/db.js

import {fetch} from "node-fetch"
const API_URL = "http://yts-am/api/v2/list_movies.json"
export const getMovies = () => {
     return fetch('${API_URI}')
             .then(res => res.json())
             .then(json=>json.data.movies)
}

graphql/resolvers.js

import {getMovies} from "./db";

const resolvers = {
    Query: {
        movies: () => getMovies()
    }
}

export default resolvers

graphql/db.js

type Movie {
    id: Int!
    title: String!
    rating: Float!
    summary: String!
    language: String!
    medium_cover_image: String!
}

type Query {
    movies: [Movie]
}

 

 

 

 

 

query{
    movies{
        id,
        title,
        summary
    }
}

 

 

 

 

 

limit 과 rating 을 추가하기

graphql/db.js

import {fetch} from "node-fetch"
const API_URL = "http://yts-am/api/v2/list_movies.json?"

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('${API_URI}')
             .then(res => res.json())
             .then(json=>json.data.movies)
}

graphql/resolvers.js

import {getMovies} from "./db";

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

export default resolvers

graphql/db.js

type Movie {
    id: Int!
    title: String!
    rating: Float!
    summary: String!
    language: String!
    medium_cover_image: String!
}

type Query {
    movies(limit: Int, rating: Float): [Movie]
}

 

 

 

'[공부] 영상 > GraphQL' 카테고리의 다른 글

GraphQL 강의 (8~9 : Mutation)  (0) 2019.08.10
GraphQL 강의 (6~7 : Schema Array, Find)  (0) 2019.08.10
GraphQL 강의 (4 : Schema String)  (0) 2019.08.10
GraphQL 강의 (1~3 : 세팅)  (0) 2019.08.10
GraphQL  (0) 2019.08.08