graphql學習筆記-1

GraphQL筆記-1

簡單介紹

GraphQL是Facebook釋出的一個Web Service Open Source,
將RESTful service的形式改變成單一path,
透過自己訂出的GraphQL格式(塞在request body or query)來call需要的service node(節點)

  • client side可依需求request需要的欄位就好了,避免API來了一大坨資料,而前端只需要部分資料
  • server side開發GraphQL時會定義每個節點的type,所以開發的時候就順便建立好了API DOC,graphql提供一個graphiql的介面可直接測試API,並且可以直接看API docs

query example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// at client using apollo-client to call graphql API
// query
{
me {
id
name
email
createAt
}
}
// will get content of reponse like
{
"data": {
"me": {
"id": "id123",
"name": "max",
"email": "accountemail@abc.com",
"createAt": "2017-09-30T09:24:17.191Z"
}
}
}

GraphQL 與 RESTful service 的對應

GraphQL request types主要分為三種 Query, Mutation, Subscription(web socket)

對應表

GraphQL REST
Query GET
Mutation POST/PUT/DELETE
subscription 還沒碰過

先這樣

之後會陸續更新 server side 實作 及 client side(會用vue-apollo) 如何call graphql service