Skip to content

Commit f425d8d

Browse files
linpu.lilinhe0x0
authored andcommitted
APIReference-Language.md (xitu#73)
1 parent 12d9b29 commit f425d8d

File tree

1 file changed

+49
-72
lines changed

1 file changed

+49
-72
lines changed

site/graphql-js/APIReference-Language.md

Lines changed: 49 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -7,90 +7,90 @@ sublinks: BREAK,getLocation,Kind,lex,parse,parseValue,printSource,visit
77
next: /graphql-js/type/
88
---
99

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` 引入。示例如下:
1111

1212
```js
1313
import { Source } from 'graphql'; // ES6
1414
var { Source } = require('graphql'); // CommonJS
1515
```
1616

17-
## Overview
17+
## 概述
1818

19-
*Source*
19+
**Source**
2020

2121
<ul class="apiIndex">
2222
<li>
2323
<a href="#source">
2424
<pre>class Source</pre>
25-
Represents the input string to the GraphQL server
25+
表示传递给 GraphQL 服务的输入字符串。
2626
</a>
2727
</li>
2828
<li>
2929
<a href="#getlocation">
3030
<pre>function getLocation</pre>
31-
Converts a character offset to a row and column in the Source
31+
将字符偏移量转化为 Source 中的行和列。
3232
</a>
3333
</li>
3434
</ul>
3535

36-
*Lexer*
36+
**词法分析器**
3737

3838
<ul class="apiIndex">
3939
<li>
4040
<a href="#lex">
4141
<pre>function lex</pre>
42-
Lexes a GraphQL Source according to the GraphQL Grammar
42+
根据 GraphQL 语法对 GraphQL 的 Source 类进行词法分析。
4343
</a>
4444
</li>
4545
</ul>
4646

47-
*Parser*
47+
**解析器**
4848

4949
<ul class="apiIndex">
5050
<li>
5151
<a href="#parse">
5252
<pre>function parse</pre>
53-
Parses a GraphQL Source according to the GraphQL Grammar
53+
根据 GraphQL 语法对 GraphQL 的 Source 类进行解析。
5454
</a>
5555
</li>
5656
<li>
5757
<a href="#parseValue">
5858
<pre>function parseValue</pre>
59-
Parses a value according to the GraphQL Grammar
59+
根据 GraphQL 语法对值进行解析。
6060
</a>
6161
</li>
6262
<li>
6363
<a href="#kind">
6464
<pre>var Kind</pre>
65-
Represents the various kinds of parsed AST nodes.
65+
表示已解析的抽象语法树(Abstract Syntax Tree, AST)中节点的各种类型。
6666
</a>
6767
</li>
6868
</ul>
6969

70-
*Visitor*
70+
**访问方法**
7171

7272
<ul class="apiIndex">
7373
<li>
7474
<a href="#visit">
7575
<pre>function visit</pre>
76-
A general-purpose visitor to traverse a parsed GraphQL AST
76+
一个通用的访问方法,用于遍历一个已解析的 GraphQL AST
7777
</a>
7878
</li>
7979
<li>
8080
<a href="#break">
8181
<pre>var BREAK</pre>
82-
A token to allow breaking out of the visitor.
82+
用于允许中断访问方法的 token
8383
</a>
8484
</li>
8585
</ul>
8686

87-
*Printer*
87+
**Printer**
8888

8989
<ul class="apiIndex">
9090
<li>
9191
<a href="#print">
9292
<pre>function print</pre>
93-
Prints an AST in a standard format.
93+
以一个标准的格式打印出一个 AST
9494
</a>
9595
</li>
9696
</ul>
@@ -105,10 +105,7 @@ export class Source {
105105
}
106106
```
107107
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" 在之后就会很有用了。
112109
113110
### getLocation
114111
@@ -121,10 +118,9 @@ type SourceLocation = {
121118
}
122119
```
123120
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 对象,包含相关的行列位置信息。
126122
127-
## Lexer
123+
## 词法分析器
128124
129125
### lex
130126
@@ -141,17 +137,11 @@ export type Token = {
141137
};
142138
```
143139
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。
150141
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 里将词法分析器回退或者前进到某个新位置。
153143
154-
## Parser
144+
## 解析器
155145
156146
### parse
157147
@@ -162,9 +152,9 @@ export function parse(
162152
): Document
163153
```
164154

165-
Given a GraphQL source, parses it into a Document.
155+
给定一个 GraphQL 源,将其解析并放入文档里。
166156

167-
Throws GraphQLError if a syntax error is encountered.
157+
如果遇到语法错误则抛出 GraphQLError
168158

169159
### parseValue
170160

@@ -175,65 +165,54 @@ export function parseValue(
175165
): Value
176166
```
177167

178-
Given a string containing a GraphQL value, parse the AST for that value.
168+
给定一个包含 GraphQL 值的字符串,将这个值解析为 AST
179169

180-
Throws GraphQLError if a syntax error is encountered.
170+
如果遇到语法错误则抛出 GraphQLError
181171

182-
This is useful within tools that operate upon GraphQL Values directly and
183-
in isolation of complete GraphQL documents.
172+
这在某些工具中会很有用,比如直接在 GraphQL 值上进行操作,并且与 GraphQL 文档完全分离开来。
184173

185174
### Kind
186175

187-
An enum that describes the different kinds of AST nodes.
176+
这是一个枚举类型,用于描述不同类型的 AST 节点。
188177

189-
## Visitor
178+
## 访问方法
190179

191180
### visit
192181

193182
```js
194183
function visit(root, visitor, keyMap)
195184
```
196185

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` 函数。
200187

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` 停止整个遍历。
205189

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。
209191

210192
```js
211193
var editedAST = visit(ast, {
212194
enter(node, key, parent, path, ancestors) {
213195
// @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: 使用返回的这个值替代原本的节点
219201
},
220202
leave(node, key, parent, path, ancestors) {
221203
// @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: 使用返回的这个值替代原本的节点
227209
}
228210
});
229211
```
230212
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 有四种形式:
235214
236-
1) Named visitors triggered when entering a node a specific kind.
215+
1) 当进入特定类型的节点时,触发同名访问方法。
237216
238217
```js
239218
visit(ast, {
@@ -243,8 +222,7 @@ visit(ast, {
243222
})
244223
```
245224
246-
2) Named visitors that trigger upon entering and leaving a node of
247-
a specific kind.
225+
2) 在进入或离开特定类型的节点时,触发同名访问方法。
248226
249227
```js
250228
visit(ast, {
@@ -259,7 +237,7 @@ visit(ast, {
259237
})
260238
```
261239
262-
3) Generic visitors that trigger upon entering and leaving any node.
240+
3) 在进入或离开任意节点时,触发通用的访问方法。
263241
264242
```js
265243
visit(ast, {
@@ -272,7 +250,7 @@ visit(ast, {
272250
})
273251
```
274252
275-
4) Parallel visitors for entering and leaving nodes of a specific kind.
253+
4) 为进入或离开特定类型的节点创建平行的访问方法。
276254
277255
```js
278256
visit(ast, {
@@ -291,7 +269,7 @@ visit(ast, {
291269
292270
### BREAK
293271
294-
The sentinel `BREAK` value described in the documentation of `visitor`.
272+
`BREAK` 标记在 `visitor` 的文档中有描述。
295273
296274
## Printer
297275
@@ -301,5 +279,4 @@ The sentinel `BREAK` value described in the documentation of `visitor`.
301279
function print(ast): string
302280
```
303281

304-
Converts an AST into a string, using one set of reasonable
305-
formatting rules.
282+
使用一组合理的格式化规则,将一个 AST 转化成一个字符串。

0 commit comments

Comments
 (0)