RESTful API
使⽤用簡單的規則將資源定位:
抓取⼀一個使⽤用者的資源
GET /users/1
配合 HTTP 動詞來來對資源進⾏行行操作,
例例如下⾯面可以⽤用來來建立⼀一個新的群組資源
POST /groups
抓取⼀一個群組裡⾯面的使⽤用者清單
GET /groups/1/users
Slide 5
Slide 5 text
RESTful API
使⽤用簡單的規則將資源定位:
抓取⼀一個使⽤用者的資源
GET /users/1
配合 HTTP 動詞來來對資源進⾏行行操作,
例例如下⾯面可以⽤用來來建立⼀一個新的群組資源
POST /groups
抓取⼀一個群組裡⾯面的使⽤用者清單
GET /groups/1/users
很夠了了!
Slide 6
Slide 6 text
RESTful API
如果我想抓取某個群組裡⾯面的使⽤用者清單以外,
還想要抓取裡⾯面的使⽤用者的資料呢?
Type system
type User {
id: ID!
name: String!
email: String
url: String
}
type Group {
id: ID!
name: String!
users: [User!]!
}
Slide 21
Slide 21 text
Type system
• 型別定義:
type User {
...
}
• 定義欄欄位(Field)
id: ID!
name: String!
email: String
users: [User!]!
Slide 22
Slide 22 text
Scalar types
• Int
• Signed 32-bit integer
• Float
• Double-precision floating-point
• String
• UTF-8 character sequence
• Boolean
• Just "true" or "false"
• ID
• A unique identifier
• Serialized in the same way as String
Slide 23
Slide 23 text
Type system
• 提供⾃自訂 Scalar type
scalar Date
• 宣告陣列列
[Type]
• 宣告非空
Type!
• 組合技!
[Type]
[Type]!
[Type!]
[Type!]!
Slide 24
Slide 24 text
Type system
• Interface
interface Account {
id: ID!
name: String!
}
type User implements Account {
id: ID!
name: String!
email: String!
}
interface Bot implements Account {
id: ID!
name: String!
author: User!
}
Slide 25
Slide 25 text
Type system
• Enum
enum PostType {
NORMAL
SHARE
}
• Union type
union SearchResult = Post | User
query {
search(text: "an") {
id
... on Post {
title
}
... on User {
name
}
}
}