Skip to content

Commit 68f90af

Browse files
committed
docs: 优化 graphql 文章
1 parent 82dfe8c commit 68f90af

File tree

1 file changed

+41
-39
lines changed

1 file changed

+41
-39
lines changed

docs/blog/base-graphql.md

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ query {
4141

4242
## GraphQL 核心知识
4343

44-
GraphQL 中最重要的概念就是 Schema,Schema 表示的是接口的类型,也就是前后端接口的一个约定,这个约定是写在文件中的,并且是严格按照这个约定来返回的数据
44+
GraphQL 中最重要的概念就是 Schema,Schema 表示的是接口的类型,也就是前后端接口的一个约定,这个约定通常是写在一个单独文件中,也即是 `.graphql` 的文件
4545

46-
在开发之前,前后端必须先制定这个 Schema,制定之后就可以放心的编写各端的代码了
46+
定义好 Schema 之后,前后端就会严格按照这个 Schema 进行请求和响应
4747

4848
例如:如果我们的数据库中有一个学生表,我们可以定义一个对象类型去表示这个表中的字段,除此之外,我们可以扩展一些方法(根据学生的分数判断是否优秀)。
4949

50-
在 GraphQL 主要有以下几种类型
50+
除此之外,在 GraphQL 中还包含有以下几种类型
5151

5252
```graphql
5353
# 内置标量类型:Int、Float、String、Boolean、ID
@@ -62,54 +62,54 @@ type Student {
6262

6363
# 自定义标量类型
6464
type Photo {
65-
url:String
66-
size:Int
67-
type:PhotoType
68-
postedBy:Student
65+
url:String
66+
size:Int
67+
type:PhotoType
68+
postedBy:Student
6969
}
7070

7171
# 枚举类型
7272
enum PhotoType {
73-
SELFLE
74-
ACTION
73+
SELFLE
74+
ACTION
7575
}
7676

7777
# 查询类型
7878
type Query {
79-
allStudents:[Student]
80-
allPhotos:[Photo]
79+
allStudents:[Student]
80+
allPhotos:[Photo]
8181
}
8282

8383
# 变更类型
8484
type Mutation {
85-
postPhoto(input: PostPhotoInput): Photo
85+
postPhoto(input: PostPhotoInput): Photo
8686
}
8787

8888
# 输入类型
8989
input PostPhotoInput {
90-
url:String
90+
url:String
9191
}
9292

9393
# 订阅类型
9494
type Subscription {
95-
newPhoto:Photo
96-
newStudent:Student
95+
newPhoto:Photo
96+
newStudent:Student
9797
}
9898
```
9999

100100
在上述的类型中,有 3 个比较重要的类型,他们分别是:QueryMutationSubscrition,只有它们才能作为每一个 GraphQL 查询的入口(也就是 get 请求上携带的参数)。
101101

102102
### Query
103103

104-
一个 query 即表示一次查询,如上面的例子为例,我们可以根据定义的类型,合理的编写我们需要的查询返回值
104+
一个 query 即表示一次查询,同时也可以支持嵌套
105105

106-
例如:我们发送这样一段 query 给服务器端,服务器端就可以根据对应的 resolver 去返回对应的数据格式
106+
例如:我们发送 allPhotos 去查询所有的照片,同时嵌套了一层 postedBy 查询,就可以找到照片的上传者
107107

108108
```graphql
109109
query {
110-
allPhotos{
111-
url,
112-
postedBy{
110+
allPhotos {
111+
url
112+
postedBy {
113113
name
114114
}
115115
}
@@ -143,10 +143,10 @@ GraphQL 除了强大的查询系统之外,还提供了 Mutation 变更,其
143143

144144
```js
145145
mutation {
146-
postPhoto(url:'https://baidu.com/defaul.png'){
147-
url,
148-
size
149-
}
146+
postPhoto(url:'https://baidu.com/defaul.png'){
147+
url,
148+
size
149+
}
150150
}
151151
```
152152

@@ -156,31 +156,31 @@ mutation {
156156

157157
```js
158158
mutation postPhoto($input: PostPhotoInput){
159-
postPhoto(url:$input.url){
160-
url,
161-
size
162-
}
159+
postPhoto(url:$input.url){
160+
url,
161+
size
162+
}
163163
}
164164
// 参数单独存放
165165
// http://xx.com/graphql?variables={input:{url:"https://baidu.com/defaul.png"}}
166166
```
167167

168168
### Subscrition
169169

170-
Subscrition 和其他类型类似,主要用来表示订阅,也就是说,如果在 Subscrition 中定义了查询内容,就通过 webSocket 和前后端建立了一个双向数据通道,每当数据有变动,就可以自动向客户端推送。
170+
Subscrition 和其他类型类似,不同点在于它表示订阅,也就是说,如果在 Subscrition 中定义了查询内容,就通过 webSocket 和前后端建立了一个双向数据通道,每当数据有变动,就可以自动向客户端推送。
171171

172172
例如:上面我们已经定义了 Subscrition 类型,接下来我们定义一个 action 类型的照片,当新的 action 类型的照片插入时,则会通知其他查询刷新数据。
173173

174174
```graphql
175175
subscription{
176-
newPhoto(type:'ACTION'){
177-
url
178-
size
179-
}
176+
newPhoto(type:'ACTION'){
177+
url
178+
size
179+
}
180180
}
181181
```
182182

183-
对应的 Resolver 配置如下
183+
对应的 Resolver 配置如下
184184

185185
```js
186186
const resolver = {
@@ -198,18 +198,18 @@ const resolver = {
198198
}
199199
}
200200
}
201-
}
201+
};
202202
```
203203

204204
## GraphQL 小实战
205205

206-
接下来我们通过 Apollo 和 Express 来搭建一个 GraphQL 服务器
206+
接下来我们通过 Apollo 和 Express 来搭建一个 GraphQL 服务器端
207207

208208
步骤如下:
209209

210210
- 定义 GraphQL 类型
211211
- 使用 Apollo 启动一个服务
212-
- 编写 GraphQL 类型对应的处理方法:Resolver
212+
- 编写处理 GraphQL 类型的方法:Resolver
213213

214214
### 首先定义 GraphQL 类型
215215

@@ -265,6 +265,8 @@ type Subscription {
265265

266266
接下来我们启动一个支持 GraphQLweb 服务。
267267

268+
我们使用 apollo-server-express 可以非常方便的创建一个 graphql 环境。
269+
268270
```js
269271
const express = require("express");
270272
const { ApolloServer, PubSub } = require("apollo-server-express");
@@ -278,7 +280,7 @@ httpServer.listen({ port: 4000 }, () =>
278280

279281
### 编写 Resolver 处理 GraphQL 类型
280282

281-
接下来编写 Resolver ,用于处理 GraphQL 类型对应的业务逻辑,也就是在这里去执行后端的业务逻辑,并返回对应类型的数据。
283+
接下来编写 Resolver 处理 GraphQL 类型对应的业务逻辑,并返回对应类型的数据。
282284

283285
```js
284286
const students = [];
@@ -324,7 +326,7 @@ const server = new ApolloServer({
324326
});
325327
```
326328

327-
完整的代码见下方的参考资料
329+
编写好这三步之后,我们的一个 GraphQL Api 就已经完成了,是不是很简单。完整的代码可以在下方链接中找到
328330

329331
## 参考资料
330332

0 commit comments

Comments
 (0)