create account

GraphQL 학습 - 1 by happyberrysboy

View this thread on: hive.blogpeakd.comecency.com
· @happyberrysboy ·
$29.00
GraphQL 학습 - 1
![](https://cdn.steemitimages.com/DQmQfLngNsvtMszhqdiGXkBjYQquKCRucxTXrNBjVjG1991/image.png)

안녕하세요 햅뽀이입니다.

새해니까!! 올해도 열심히 개발 공부를 하겠다는 목표를 세웠습니다. 일단 Nomad Coders Academy에 있는 무료 강의들 중에 관심 있었던 것들 부터 들어 볼까 합니다.

그 중의 첫번째가 GraphQL입니다.

동영상 강의 자체는 모두 들었고, 실제로 개발하면서 정리하는 시간을 가져보려 합니다.

강의 위치 : https://academy.nomadcoders.co/

![](https://cdn.steemitimages.com/DQmNpg2UGhNibzGLQeTYd922ExvJRja3NBbTGTmWdcNApmP/image.png)

___

### 샘플이자 GraphQL의 거의 모든 것을 대표하는 소스입니다.

```
const GraphQLServer = require("graphql-yoga").GraphQLServer;

const datas = [
    {
        id: 0,
        name: "Name1",
        data: "Data1"
    },
    {
        id: 1,
        name: "Name2",
        data: "Data2"
    }
];

const typeDefs = 
`
type 
       Data {    
           id:Int!
           name:String!
           data:String!  
       }  

type
   Query {
        getData (
           id:Int
        ): [Data]!    
}`;

const resolvers = {
  Query: {
    getData: function(_, _a) {
      var id = _a.id;

      return datas.filter(function(data) {
        return data.id === id;
      });
    }
  }
};

const server = new GraphQLServer({ typeDefs: typeDefs, resolvers: resolvers });
server.start(function() {
  return console.log("Server is running on localhost:4000");
});

```

이 소스에 거의 모든 것이 들어 있습니다.(제가 배운 부분까지는 말이죠!)

- datas : 데이터 리스트(DB로 생각하시면 됩니다.)
- typeDefs : GraphQL에 데이터형태와, 쿼리시 조회 할 수 있는 형태 등을 정의 합니다.(텍스트 방식입니다.)
- resolvers : typeDefs에 정의된 내용을 호출 할 때 resolvers에 정의된 함수를 거쳐서 결과 값을 보내줍니다.
- server : GraphQLServer 인스턴스를 생성하고, 정의된 typeDefs, resolvers를 설정합니다.
- server.start() : GraphQL 서버를 시작합니다.

다 끝!!??

이정도만 해놓고 실행을 해도, 엄청난 결과물을 확인 하실 수 있습니다.

___

##### 서버를 시작하고...

- http://localhost:4000/ 로 접속을 합니다.
- 그러면 아래와 같은 화면(Play Ground)가 나타납니다.
- 그리고 제가 등록한 소스에 정의된 대로 쿼리를 해볼 수 있도록 기능을 제공해줍니다.

![](https://cdn.steemitimages.com/DQmeuNAtsHSi5kQPi4gkVdVL7fiAVY1anKTmzxf951PxSuD/image.png)

##### 조회 문법
```
// 조회 문법
query{
  getData(id:0){
    name
  }
}
```
 
##### 그리고.. 결과물
```
// 결과물
{
  "data": {
    "getData": [
      {
        "name": "namename"
      }
    ]
  }
}
```


일단 여기까지는 따라와 오셔도 됩니다. 그리고 작성된 소스, 쿼리 문법, 결과물을 분석해 보겠습니다.

#### typeDefs
- 먼저 typeDefs에서, Data를 정의합니다. Data는 3개의 속성을 가집니다. `id, name, data` 이고 각각 `Int, String, String` 로 정의한다는 의미입니다.

- 그리고 Query에서는 조회하는 명령어를 입력하고, 입력 할 수 있는 parameter들, 그리고 리턴 결과 형태를 표시합니다.
- 조회하는 명령어는 **getData**입니다. 그리고 입력 할 수 있는 parameter는 `id, name, data` 세 개가 입력 가능하고 리턴 형태는 `Data` 형태를 가집니다.  Data 형태란 바로 위에 typeDefs에 정의한 그 Data를 말하는 것이지요!!

#### resolvers
- typeDefs에서 정의한 Query 였던 getData를 입력 받으면 실제로 내부적으로 작동하는 함수를 가집니다.
- getData와 Parameter을 받으면 _a 변수로 해당 파라미터를 받을 수 있습니다.
- _a를 통해 전달 받은 Parameter id 값으로, DB로 부터 해당 id값을 가지는 Object를 return 하게 되는 간단한 함수입니다.

#### 조회문법
- 정의된 getData를 입력하고() 안에 Parameter을 입력합니다. 여기서는 id값으로 0을 입력하였습니다.
- 그리고 { name } 부분은 결과물을 보여 줄때 Data Object의 어떤 변수 값만 보여줄 지를 설정할 수 있습니다.
- 현재는 name만 입력을 했지만, {name, data}를 입력하면 결과물에 name, data를 모두 보여주게 됩니다.


___

예전에 @anpigon님이 끝내주는 기술이라고 얼른 배우라고 했는데, 이제서야 배우게 됐네요.
그리고나서 들었던 생각이 ... 왜 이제야 이걸 알았을까?? 였습니다.

정말 직관적이고, 간단하고, 서버 설정도 필요 없습니다.
앞으로는 정말 api 서버는 무!조!건! GraphQL 입니다!!!

### 끝~~~~~~내주는 이 GraphQL..!!!! 한번 써보시지 않겠습니까? ^^
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 139 others
properties (23)
authorhappyberrysboy
permlinkgraphql-1
categorysct
json_metadata{"tags":["sct","sct-kr","sct-dev","bs","mini","zzan","kr-dev","dblog","palnet"],"users":["anpigon"],"image":["https://cdn.steemitimages.com/DQmQfLngNsvtMszhqdiGXkBjYQquKCRucxTXrNBjVjG1991/image.png","https://cdn.steemitimages.com/DQmNpg2UGhNibzGLQeTYd922ExvJRja3NBbTGTmWdcNApmP/image.png","https://cdn.steemitimages.com/DQmeuNAtsHSi5kQPi4gkVdVL7fiAVY1anKTmzxf951PxSuD/image.png"],"links":["https://academy.nomadcoders.co/","http://localhost:4000/"],"app":"steemcoinpan/0.1","format":"markdown","canonical_url":"https://www.steemcoinpan.com/@happyberrysboy/graphql-1"}
created2020-01-07 11:52:33
last_update2020-01-07 11:52:33
depth0
children7
last_payout2020-01-14 11:52:33
cashout_time1969-12-31 23:59:59
total_payout_value16.079 HBD
curator_payout_value12.917 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,139
author_reputation314,484,142,656,697
root_title"GraphQL 학습 - 1"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id94,138,570
net_rshares102,382,273,436,024
author_curate_reward""
vote details (203)
@angdatingdaan ·
# According to the Bible, *How should a Christian deal with peer pressure?*
https://youtu.be/Ki08j0bnXTA
https://i.postimg.cc/SxmKZFY2/image.jpg
Comment what you understand of our Youtube Video to receive our full votes. We have 30,000 #SteemPower. It's our little way to **Thank you, our beloved friend.**  
Check our [Discord Chat](https://discord.gg/vzHFNd6) 
Join our Official Community: https://beta.steemit.com/trending/hive-182074
👍  
properties (23)
authorangdatingdaan
permlinkuy8y5o1t3k
categorysct
json_metadata""
created2020-01-07 12:00:48
last_update2020-01-07 12:00:48
depth1
children0
last_payout2020-01-14 12:00:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length439
author_reputation-8,429,686,171,540
root_title"GraphQL 학습 - 1"
beneficiaries[]
max_accepted_payout10,000.000 HBD
percent_hbd100
post_id94,138,953
net_rshares2,935,477,808
author_curate_reward""
vote details (1)
@goodhello ·
와우~
properties (22)
authorgoodhello
permlinkq3rzhd
categorysct
json_metadata{"tags":["sct"],"app":"steemcoinpan/0.1","canonical_url":"https://www.steemcoinpan.com/@goodhello/q3rzhd"}
created2020-01-08 06:32:00
last_update2020-01-08 06:32:00
depth1
children0
last_payout2020-01-15 06:32:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3
author_reputation430,635,914,360,128
root_title"GraphQL 학습 - 1"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id94,170,479
net_rshares0
@jsquare ·
JCAR 구독보팅입니다. 
2020년, 새해 복 많이 받으세요. ^^
properties (22)
authorjsquare
permlinkre-graphql-1-20200107t120258z
categorysct
json_metadata"{"app": "rewarding/0.1.5"}"
created2020-01-07 12:03:03
last_update2020-01-07 12:03:03
depth1
children0
last_payout2020-01-14 12:03:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length39
author_reputation81,350,685,034,901
root_title"GraphQL 학습 - 1"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id94,139,088
net_rshares0
@sct.tom ·
오호~ Swagger보다 사용하기 더 편할듯하군요~~
properties (22)
authorsct.tom
permlinkq3qrc2
categorysct
json_metadata{"tags":["sct"],"app":"steemcoinpan/0.1","canonical_url":"https://www.steemcoinpan.com/@sct.tom/q3qrc2"}
created2020-01-07 14:38:27
last_update2020-01-07 14:38:27
depth1
children0
last_payout2020-01-14 14:38:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length29
author_reputation2,885,985,444,336
root_title"GraphQL 학습 - 1"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id94,146,061
net_rshares0
@steemitboard ·
Congratulations @happyberrysboy! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

<table><tr><td><img src="https://steemitimages.com/60x70/http://steemitboard.com/@happyberrysboy/payout.png?202001061222"></td><td>You received more than 9000 as payout for your posts. Your next target is to reach a total payout of 10000</td></tr>
</table>

<sub>_You can view [your badges on your Steem Board](https://steemitboard.com/@happyberrysboy) and compare to others on the [Steem Ranking](https://steemitboard.com/ranking/index.php?name=happyberrysboy)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>


To support your work, I also upvoted your post!


###### [Vote for @Steemitboard as a witness](https://v2.steemconnect.com/sign/account-witness-vote?witness=steemitboard&approve=1) to get one more award and increased upvotes!
properties (22)
authorsteemitboard
permlinksteemitboard-notify-happyberrysboy-20200107t121036000z
categorysct
json_metadata{"image":["https://steemitboard.com/img/notify.png"]}
created2020-01-07 12:10:36
last_update2020-01-07 12:10:36
depth1
children0
last_payout2020-01-14 12:10:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length946
author_reputation38,975,615,169,260
root_title"GraphQL 학습 - 1"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id94,139,472
net_rshares0
@tradingideas ·
오 새로나온 db인가요. 한번 봐야겠군요

Posted using [Partiko Android](https://partiko.app/referral/tradingideas)
properties (22)
authortradingideas
permlinktradingideas-re-happyberrysboy-graphql-1-20200107t125903974z
categorysct
json_metadata{"app":"partiko","client":"android"}
created2020-01-07 12:59:03
last_update2020-01-07 12:59:03
depth1
children1
last_payout2020-01-14 12:59:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length97
author_reputation473,979,989,726,345
root_title"GraphQL 학습 - 1"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id94,141,527
net_rshares0
@happyberrysboy ·
DB가 아닌, 아주아주 쉽게 API 서버를 만들 수 있는 것이 랄까요?
사실 쉽게 API 서버를 만들 수 있는데다가, Server에서 Client로 값을 넘겨 줄때 Server가 값을 정하는 것이 아닌, Client가 원하는 값을 정해서 요청하는 방식입니다요..!!! 아주 꿀!!인것 같아요! ㅎ
properties (22)
authorhappyberrysboy
permlinkq3qqwz
categorysct
json_metadata{"tags":["sct"],"app":"steemcoinpan/0.1","canonical_url":"https://www.steemcoinpan.com/@happyberrysboy/q3qqwz"}
created2020-01-07 14:29:24
last_update2020-01-07 14:29:24
depth2
children0
last_payout2020-01-14 14:29:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length165
author_reputation314,484,142,656,697
root_title"GraphQL 학습 - 1"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id94,145,664
net_rshares0