-
GraphQL & Apollo(with Node.js) 간단 정리Programing Study/E.T.C 2021. 3. 24. 00:16728x90반응형
GraphQL | A query language for your API
Evolve your APIwithout versions Add new fields and types to your GraphQL API without impacting existing queries. Aging fields can be deprecated and hidden from tools. By using a single evolving version, GraphQL APIs give apps continuous access to new featu
graphql.org
사용 고려할때 : GraphQL은 rest API 보다 선택적으로 서버에서 보내는 값을 조합해서 클라이언트로 전송 할때 효율적으로 사용할수 있다.
아직 개념정리가 정립된건 아니지만 GraphQL로 데이터베이스를 가져올때 다음과 같이 정리하고자한다.
GraphQL 서버 커리를 사용하기 위해서 여러가지 라이브러리들이 있지만 백앤드 & 프론트앤드 모두지원하는 Apollo를 토대로 공부하였다.
데이터를 가져오는 예로 정리하겠다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters//node.js에서 const { ApolloServer, gql) = require('apollo-server') const data = [ {id : test1, type : G, other : 1}, { id : test2, type: S, other : 2},......] //형태의 데이터 json이 있다고 가정한다. //일단 해당 json값을 받아오기 위한 타입형태(typeDefs)를 정리한다. const typeDefs = sql' type Query { getUsers : [userInfor] // getUser라고 정의하고 리스트를 가져온다. } type userInfor { id : String type : String } ' //json 형태의 데이터를 resolvers의 query의 => 다음 값에 넣는다. //resolvers는 앞으로 클라이언트에게 보내줄 json데이터를 어떠한 형태로 보내줄것인가를 정한다. //data는 리스트 형태의 json에 id와, type을 가지기 때문에 위의 typeDefs안에 type userInfor을 포함한 Query의 getUsers롤 정의했기 때문에 다음과같이 사용하였다. const resolvers = { Query : { getUsers : () => data } } const serever = new ApolloServer({ typeDefs//받을 데이터형태의 정의, resolvers //클라이언트에게 전송할 데이터 값}) server.listen().then(({ url } ) => { console.log('Server ready at ${url}') }) // 결과 값 : Server ready at [ {id : test1, type : G}, { id : test2, type: S},......] //해당정보로 공부하였다. insert문이나 update delete같은 경우에는 해당 영상을 참고하면 좋을것이다.
www.youtube.com/watch?v=9BIXcXHsj0A&t=4860s
728x90반응형'Programing Study > E.T.C' 카테고리의 다른 글
FFmpege) FFmpeg란 무엇인가요? (0) 2021.10.15 Github) Permission denied (publickey) 해결하기 (0) 2021.08.31 개인정보 처리방침 (0) 2021.03.21 Vscode의 remote-ssh를 이용해서 aws EC2 접근하기 (0) 2020.06.09 (프로그래머) 코드 스타일 (0) 2019.10.18