这份快速参考备忘单提供了 GraphQL 的简要概述

入门

概述

  • RESTful API 的另一种方法
  • GraphQL 是一种 API 查询语言
  • 使用清晰的共享术语轻松描述 GraphQL API 的形状。
  • 客户端发出查询/突变以读取和更新数据
  • GraphQL 语法可以表达复杂的实体关系
  • 不同语言 实现 GraphQL 的库

GraphQL

Schema

:- -
schema GraphQL 架构定义
query 读取和遍历数据
mutation 修改数据或触发动作
subscription 发生事件时运行查询

内置标量类型

:- -
Int 有符号 32 位整数
Float 有符号双精度浮点值
String UTF-8 字符序列
Boolean 对或错布尔值类型
ID 唯一标识符

类型定义

:- -
scalar 标量类型
type 对象类型
interface 接口类型
union 联合类型
enum 枚举类型
input 输入对象类型

类型修饰符

:- -
String 可空字符串
String! 非空字符串
[String] 可空字符串列表
[String]! 可空字符串的非空列表
[String!]! 非空字符串的非空列表

输入参数

基本输入

1
2
3
type Query {
users(limit: Int): [User]
}

输入默认值

1
2
3
type Query {
users(limit: Int = 10): [User]
}

具有多个参数的输入

1
2
3
type Query {
users(limit: Int, sort: String): [User]
}

具有多个参数和默认值的输入

1
2
3
4
5
6
7
8
9
type Query {
users(limit: Int = 10, sort: String): [User]
}
type Query {
users(limit: Int, sort: String = "asc"): [User]
}
type Query {
users(limit: Int = 10, sort: String = "asc"): [User]
}

输入类型

1
2
3
4
input ListUsersInput {
limit: Int
since_id: ID
}
1
2
3
type Mutation {
users(params: ListUsersInput): [User]!
}

自定义标量

1
2
3
4
5
scalar Url
type User {
name: String
homepage: Url
}

接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
interface Foo {
is_foo: Boolean
}
interface Goo {
is_goo: Boolean
}
type Bar implements Foo {
is_foo: Boolean
is_bar: Boolean
}
type Baz implements Foo, Goo {
is_foo: Boolean
is_goo: Boolean
is_baz: Boolean
}

实现一个或多个接口的对象

联合

1
2
3
4
5
6
7
8
9
10
11
12
type Foo {
name: String
}
type Bar {
is_bar: String
}
union SingleUnion = Foo
union MultipleUnion = Foo | Bar
type Root {
single: SingleUnion
multiple: MultipleUnion
}

一个或多个对象的联合

枚举

1
2
3
4
5
6
7
8
9
10
enum USER_STATE {
NOT_FOUND
ACTIVE
INACTIVE
SUSPENDED
}
type Root {
stateForUser(userID: ID!): USER_STATE!
users(state: USER_STATE, limit: Int = 10): [User]
}

查询和变更(Mutations)

字段

1
2
3
4
5
{
hero {
name
}
}

结果:

1
2
3
4
5
6
7
{
"data": {
"hero": {
"name": "R2-D2"
}
}
}

查询可以有注释

1
2
3
4
5
6
7
8
9
{
hero {
name
# 查询可以有注释!
friends {
name
}
}
}

结果:

1
2
3
4
5
6
7
8
9
10
11
{
"data": {
"hero": {
"name": "R2-D2",
"friends": [
{ "name": "Luke Skywalker" },
{ "name": "Han Solo" }
]
}
}
}

参数 Arguments

1
2
3
4
5
6
{
human(id: "1000") {
name
height
}
}

结果:

1
2
3
4
5
6
7
8
{
"data": {
"human": {
"name": "Luke Skywalker",
"height": 1.72
}
}
}

不同类型的参数

1
2
3
4
5
6
{
human(id: "1000") {
name
height(unit: FOOT)
}
}

结果:

1
2
3
4
5
6
7
8
{
"data": {
"human": {
"name": "Luke Skywalker",
"height": 5.6430448
}
}
}

别名(Aliases)

1
2
3
4
5
6
7
8
{
empireHero: hero(episode: EMPIRE) {
name
}
jediHero: hero(episode: JEDI) {
name
}
}

结果:

1
2
3
4
5
6
7
8
9
10
{
"data": {
"empireHero": {
"name": "Luke Skywalker"
},
"jediHero": {
"name": "R2-D2"
}
}
}

片段(Fragments)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
leftComparison: hero(episode: EMPIRE) {
...comparisonFields
}
rightComparison: hero(episode: JEDI) {
...comparisonFields
}
}

fragment comparisonFields on Character {
name
appearsIn
friends {
name
}
}

结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{
"data": {
"leftComparison": {
"name": "Luke Skywalker",
"appearsIn": [
"NEWHOPE",
"EMPIRE",
"JEDI"
],
"friends": [
{ "name": "Han Solo" },
{ "name": "Leia Organa" },
{ "name": "C-3PO" },
{ "name": "R2-D2" }
]
},
"rightComparison": {
"name": "R2-D2",
"appearsIn": [
"NEWHOPE",
"EMPIRE",
"JEDI"
],
"friends": [
{ "name": "Luke Skywalker" },
{ "name": "Han Solo" },
{ "name": "Leia Organa" }
]
}
}
}

在片段内使用变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
query HeroComparison($first: Int = 3) {
leftComparison: hero(episode: EMPIRE) {
...comparisonFields
}
rightComparison: hero(episode: JEDI) {
...comparisonFields
}
}

fragment comparisonFields on Character {
name
friendsConnection(first: $first) {
totalCount
edges {
node {
name
}
}
}
}

结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{
"data": {
"leftComparison": {
"name": "Luke Skywalker",
"friendsConnection": {
"totalCount": 4,
"edges": [
{
"node": {
"name": "Han Solo"
}
},
{
"node": {
"name": "Leia Organa"
}
}
]
}
},
"rightComparison": {
"name": "R2-D2",
"friendsConnection": {
"totalCount": 3,
"edges": [
{
"node": {
"name": "Luke Skywalker"
}
},
{
"node": {
"name": "Han Solo"
}
}
]
}
}
}
}

操作名称(Operation name)

1
2
3
4
5
6
7
8
query HeroNameAndFriends {
hero {
name
friends {
name
}
}
}

结果:

1
2
3
4
5
6
7
8
9
10
11
12
{
"data": {
"hero": {
"name": "R2-D2",
"friends": [
{ "name": "Luke Skywalker" },
{ "name": "Han Solo" },
{ "name": "Leia Organa" }
]
}
}
}

变量(Variables)

1
2
3
4
5
6
7
8
9
# { "graphiql": true, "variables": { "episode": JEDI } }
query HeroNameAndFriends($episode: Episode) {
hero(episode: $episode) {
name
friends {
name
}
}
}

变量前缀必须为 $,后跟其类型

默认变量(Default variables)

1
2
3
4
5
6
7
8
query HeroNameAndFriends($episode: Episode = "JEDI") {
hero(episode: $episode) {
name
friends {
name
}
}
}

指令(Directives)

1
2
3
4
5
6
7
8
query Hero($episode: Episode, $withFriends: Boolean!) {
hero(episode: $episode) {
name
friends @include(if: $withFriends) {
name
}
}
}

1
{ "episode": "JEDI", "withFriends": false }

结果:

1
2
3
{
"data": { "hero": { "name": "R2-D2" } }
}
  • @include(if: Boolean) 仅在参数为 true 时,包含此字段
  • @skip(if: Boolean) 如果参数为 true,跳过此字段

变更(Mutations)

1
2
3
4
5
6
mutation CreateReviewForEpisode($ep: Episode!, $review: ReviewInput!) {
createReview(episode: $ep, review: $review) {
stars
commentary
}
}

1
2
3
4
5
6
7
{
"ep": "JEDI",
"review": {
"stars": 5,
"commentary": "This is a great movie!"
}
}

结果:

1
2
3
4
5
6
7
8
{
"data": {
"createReview": {
"stars": 5,
"commentary": "This is a great movie!"
}
}
}

内联片段(Inline Fragments)

1
2
3
4
5
6
7
8
9
10
11
query HeroForEpisode($ep: Episode!) {
hero(episode: $ep) {
name
... on Droid {
primaryFunction
}
... on Human {
height
}
}
}

1
{ "ep": "JEDI" }

结果:

1
2
3
4
5
6
7
8
{
"data": {
"hero": {
"name": "R2-D2",
"primaryFunction": "Astromech"
}
}
}

元字段(Meta fields)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
search(text: "an") {
__typename
... on Human {
name
}
... on Droid {
name
}
... on Starship {
name
}
}
}

结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"data": {
"search": [
{
"__typename": "Human",
"name": "Han Solo"
},
{
"__typename": "Human",
"name": "Leia Organa"
},
{
"__typename": "Starship",
"name": "TIE Advanced x1"
}
]
}
}

另见


评论
avatar
竹山一叶
技术分享 个人心得
Follow Me
公告
欢迎光临小站,这里是我日常工作和学习中收集和整理的总结,希望能对你有所帮助:)

本站的内容经过个人加工总结而来,也参考了网友们分享的资料,如有侵权,请第一时间联系我,我将及时进行修改或删除😊
最新文章
网站资讯
文章数目 :
437
已运行时间 :
本站总字数 :
431.6k
本站访客数 :
本站总访问量 :
最后更新时间 :
文章归档文章分类文章标签复制本文标题复制本文地址
随便逛逛