@@ -7,90 +7,90 @@ sublinks: BREAK,getLocation,Kind,lex,parse,parseValue,printSource,visit
7
7
next : /graphql-js/type/
8
8
---
9
9
10
- The ` graphql/language ` module is responsible for parsing and operating on the GraphQL language. You can import either from the ` graphql/language ` module, or from the root ` graphql ` module. For example:
10
+ ` graphql/language ` 模块负责对 GraphQL 查询语言进行解析和操作。你既可以从 ` graphql/language ` 模块引入,也可以从根模块 ` graphql ` 引入。示例如下:
11
11
12
12
``` js
13
13
import { Source } from ' graphql' ; // ES6
14
14
var { Source } = require (' graphql' ); // CommonJS
15
15
```
16
16
17
- ## Overview
17
+ ## 概述
18
18
19
- * Source*
19
+ ** Source* *
20
20
21
21
<ul class =" apiIndex " >
22
22
<li >
23
23
<a href="#source">
24
24
<pre>class Source</pre>
25
- Represents the input string to the GraphQL server
25
+ 表示传递给 GraphQL 服务的输入字符串。
26
26
</a>
27
27
</li >
28
28
<li >
29
29
<a href="#getlocation">
30
30
<pre>function getLocation</pre>
31
- Converts a character offset to a row and column in the Source
31
+ 将字符偏移量转化为 Source 中的行和列。
32
32
</a>
33
33
</li >
34
34
</ul >
35
35
36
- * Lexer *
36
+ ** 词法分析器 * *
37
37
38
38
<ul class =" apiIndex " >
39
39
<li >
40
40
<a href="#lex">
41
41
<pre>function lex</pre>
42
- Lexes a GraphQL Source according to the GraphQL Grammar
42
+ 根据 GraphQL 语法对 GraphQL 的 Source 类进行词法分析。
43
43
</a>
44
44
</li >
45
45
</ul >
46
46
47
- * Parser *
47
+ ** 解析器 * *
48
48
49
49
<ul class =" apiIndex " >
50
50
<li >
51
51
<a href="#parse">
52
52
<pre>function parse</pre>
53
- Parses a GraphQL Source according to the GraphQL Grammar
53
+ 根据 GraphQL 语法对 GraphQL 的 Source 类进行解析。
54
54
</a>
55
55
</li >
56
56
<li >
57
57
<a href="#parseValue">
58
58
<pre>function parseValue</pre>
59
- Parses a value according to the GraphQL Grammar
59
+ 根据 GraphQL 语法对值进行解析。
60
60
</a>
61
61
</li >
62
62
<li >
63
63
<a href="#kind">
64
64
<pre>var Kind</pre>
65
- Represents the various kinds of parsed AST nodes.
65
+ 表示已解析的抽象语法树(Abstract Syntax Tree, AST)中节点的各种类型。
66
66
</a>
67
67
</li >
68
68
</ul >
69
69
70
- * Visitor *
70
+ ** 访问方法 * *
71
71
72
72
<ul class =" apiIndex " >
73
73
<li >
74
74
<a href="#visit">
75
75
<pre>function visit</pre>
76
- A general-purpose visitor to traverse a parsed GraphQL AST
76
+ 一个通用的访问方法,用于遍历一个已解析的 GraphQL AST。
77
77
</a>
78
78
</li >
79
79
<li >
80
80
<a href="#break">
81
81
<pre>var BREAK</pre>
82
- A token to allow breaking out of the visitor.
82
+ 用于允许中断访问方法的 token。
83
83
</a>
84
84
</li >
85
85
</ul >
86
86
87
- * Printer*
87
+ ** Printer* *
88
88
89
89
<ul class =" apiIndex " >
90
90
<li >
91
91
<a href="#print">
92
92
<pre>function print</pre>
93
- Prints an AST in a standard format.
93
+ 以一个标准的格式打印出一个 AST。
94
94
</a>
95
95
</li >
96
96
</ul >
@@ -105,10 +105,7 @@ export class Source {
105
105
}
106
106
` ` `
107
107
108
- A representation of source input to GraphQL. The name is optional,
109
- but is mostly useful for clients who store GraphQL documents in
110
- source files; for example, if the GraphQL input is in a file Foo.graphql,
111
- it might be useful for name to be "Foo.graphql".
108
+ 对 GraphQL 源输入的表示。name 参数是可选的,但对于将 GraphQL 文档存储在源文件里的客户端来说,这个参数是非常有用的;举个例子,如果 GraphQL 的输入是在一个名为 Foo.graphql 的文件里,那么将 name 设置为 "Foo.graphql" 在之后就会很有用了。
112
109
113
110
### getLocation
114
111
@@ -121,10 +118,9 @@ type SourceLocation = {
121
118
}
122
119
` ` `
123
120
124
- Takes a Source and a UTF-8 character offset, and returns the corresponding
125
- line and column as a SourceLocation.
121
+ 接收一个 Source 对象和一个 UTF-8 编码的字符偏移量作为参数,返回一个 SourceLocation 对象,包含相关的行列位置信息。
126
122
127
- ## Lexer
123
+ ## 词法分析器
128
124
129
125
### lex
130
126
@@ -141,17 +137,11 @@ export type Token = {
141
137
};
142
138
` ` `
143
139
144
- Given a Source object, this returns a Lexer for that source.
145
- A Lexer is a function that acts like a generator in that every time
146
- it is called, it returns the next token in the Source. Assuming the
147
- source lexes, the final Token emitted by the lexer will be of kind
148
- EOF, after which the lexer will repeatedly return EOF tokens whenever
149
- called.
140
+ 给定一个 Source 对象,为这个对象返回一个词法分析器。词法分析器每次被调用的时候会表现得像一个生成器,返回 Source 里的下一个 token。假设对某个 source 进行词法分析,最后返回的 token 就将是某种类型的文件结尾符,而在这之后,词法分析器不管何时被调用都会重复返回文件结尾符的 token。
150
141
151
- The argument to the lexer function is optional, and can be used to
152
- rewind or fast forward the lexer to a new position in the source.
142
+ 词法分析器函数的参数是可选的,而且可被用于在 Source 里将词法分析器回退或者前进到某个新位置。
153
143
154
- ## Parser
144
+ ## 解析器
155
145
156
146
### parse
157
147
@@ -162,9 +152,9 @@ export function parse(
162
152
): Document
163
153
```
164
154
165
- Given a GraphQL source, parses it into a Document.
155
+ 给定一个 GraphQL 源,将其解析并放入文档里。
166
156
167
- Throws GraphQLError if a syntax error is encountered.
157
+ 如果遇到语法错误则抛出 GraphQLError。
168
158
169
159
### parseValue
170
160
@@ -175,65 +165,54 @@ export function parseValue(
175
165
): Value
176
166
```
177
167
178
- Given a string containing a GraphQL value, parse the AST for that value.
168
+ 给定一个包含 GraphQL 值的字符串,将这个值解析为 AST。
179
169
180
- Throws GraphQLError if a syntax error is encountered.
170
+ 如果遇到语法错误则抛出 GraphQLError。
181
171
182
- This is useful within tools that operate upon GraphQL Values directly and
183
- in isolation of complete GraphQL documents.
172
+ 这在某些工具中会很有用,比如直接在 GraphQL 值上进行操作,并且与 GraphQL 文档完全分离开来。
184
173
185
174
### Kind
186
175
187
- An enum that describes the different kinds of AST nodes.
176
+ 这是一个枚举类型,用于描述不同类型的 AST 节点。
188
177
189
- ## Visitor
178
+ ## 访问方法
190
179
191
180
### visit
192
181
193
182
```js
194
183
function visit(root, visitor, keyMap)
195
184
```
196
185
197
- visit() will walk through an AST using a depth first traversal, calling
198
- the visitor's enter function at each node in the traversal, and calling the
199
- leave function after visiting that node and all of it's child nodes.
186
+ `visit()` 将使用深度优先遍历一个 AST,在遍历当中对每个节点调用访问方法的 `enter` 函数,并在访问完当前节点及其子节点后调用 `leave` 函数。
200
187
201
- By returning different values from the enter and leave functions, the
202
- behavior of the visitor can be altered, including skipping over a sub-tree of
203
- the AST (by returning false), editing the AST by returning a value or null
204
- to remove the value, or to stop the whole traversal by returning BREAK.
188
+ 通过从 `enter` 和 `leave` 函数里返回不同的值,访问方法的行为可以进行更改,包括跳过 AST 的一个子树(返回 `false`)、编辑这个 AST(返回一个值或者返回 `null` 来删除这个节点)、或者返回 `BREAK` 停止整个遍历。
205
189
206
- When using visit() to edit an AST, the original AST will not be modified, and
207
- a new version of the AST with the changes applied will be returned from the
208
- visit function.
190
+ 当使用 `visit()` 编辑一个 AST 的时候,原始的 AST 不会被修改,`visit` 函数会返回一个经过修改的新版本 AST。
209
191
210
192
```js
211
193
var editedAST = visit(ast, {
212
194
enter (node , key , parent , path , ancestors ) {
213
195
// @return
214
- // undefined: no action
215
- // false: skip visiting this node
216
- // visitor.BREAK: stop visiting altogether
217
- // null: delete this node
218
- // any value: replace this node with the returned value
196
+ // undefined: 无操作
197
+ // false: 跳过访问该节点
198
+ // visitor.BREAK: 停止访问
199
+ // null: 删除该节点
200
+ // any value: 使用返回的这个值替代原本的节点
219
201
},
220
202
leave (node , key , parent , path , ancestors ) {
221
203
// @return
222
- // undefined: no action
223
- // false: no action
224
- // visitor.BREAK: stop visiting altogether
225
- // null: delete this node
226
- // any value: replace this node with the returned value
204
+ // undefined: 无操作
205
+ // false: 无操作
206
+ // visitor.BREAK: 停止访问
207
+ // null: 删除该节点
208
+ // any value: 使用返回的这个值替代原本的节点
227
209
}
228
210
});
229
211
` ` `
230
212
231
- Alternatively to providing enter() and leave() functions, a visitor can
232
- instead provide functions named the same as the kinds of AST nodes, or
233
- enter/leave visitors at a named key, leading to four permutations of
234
- visitor API:
213
+ 访问方法可以通过提供和节点类型同名的函数来替代 ` enter ()` 和 ` leave ()` 函数,或者通过名称的关键字来使用 ` enter` 或 ` leave` 访问方法,这就造成访问方法的 API 有四种形式:
235
214
236
- 1) Named visitors triggered when entering a node a specific kind.
215
+ 1) 当进入特定类型的节点时,触发同名访问方法。
237
216
238
217
` ` ` js
239
218
visit (ast, {
@@ -243,8 +222,7 @@ visit(ast, {
243
222
})
244
223
` ` `
245
224
246
- 2) Named visitors that trigger upon entering and leaving a node of
247
- a specific kind.
225
+ 2) 在进入或离开特定类型的节点时,触发同名访问方法。
248
226
249
227
` ` ` js
250
228
visit (ast, {
@@ -259,7 +237,7 @@ visit(ast, {
259
237
})
260
238
` ` `
261
239
262
- 3) Generic visitors that trigger upon entering and leaving any node.
240
+ 3) 在进入或离开任意节点时,触发通用的访问方法。
263
241
264
242
` ` ` js
265
243
visit (ast, {
@@ -272,7 +250,7 @@ visit(ast, {
272
250
})
273
251
` ` `
274
252
275
- 4) Parallel visitors for entering and leaving nodes of a specific kind.
253
+ 4) 为进入或离开特定类型的节点创建平行的访问方法。
276
254
277
255
` ` ` js
278
256
visit (ast, {
@@ -291,7 +269,7 @@ visit(ast, {
291
269
292
270
### BREAK
293
271
294
- The sentinel ` BREAK ` value described in the documentation of ` visitor` .
272
+ ` BREAK ` 标记在 ` visitor` 的文档中有描述。
295
273
296
274
## Printer
297
275
@@ -301,5 +279,4 @@ The sentinel `BREAK` value described in the documentation of `visitor`.
301
279
function print (ast ): string
302
280
```
303
281
304
- Converts an AST into a string, using one set of reasonable
305
- formatting rules.
282
+ 使用一组合理的格式化规则,将一个 AST 转化成一个字符串。
0 commit comments