1
1
---
2
2
title : graphql/validation
3
3
layout : ../_core/GraphQLJSLayout
4
- category : API Reference
4
+ category : API 参考
5
5
permalink : /graphql-js/validation/
6
6
sublinks : specifiedRules,validate
7
7
---
8
8
9
- The ` graphql/validation ` module fulfills the Validation phase of fulfilling a
10
- GraphQL result. You can import either from the ` graphql/validation ` module, or from the root ` graphql ` module. For example:
9
+ ` graphql/validation ` 模块负责完成一个 GraphQL 请求的验证阶段。你可以从 ` graphql/validation ` 模块导入,或者从根 ` graphql ` 模块导入,如下:
11
10
12
11
``` js
13
12
import { validate } from ' graphql/validation' ; // ES6
14
13
var { validate } = require (' graphql/validation' ); // CommonJS
15
14
```
16
15
17
- ## Overview
16
+ ## 概览
18
17
19
18
<ul class =" apiIndex " >
20
19
<li >
21
20
<a href="#validate">
22
21
<pre>function validate</pre>
23
- Validates an AST against a provided Schema.
22
+ 根据给定 Schema 验证一个 AST(抽象语法树)。
24
23
</a>
25
24
</li >
26
25
<li >
27
26
<a href="#specifiedrules">
28
27
<pre>var specifiedRules</pre>
29
- A list of standard validation rules described in the GraphQL specification.
28
+ GraphQL 规范中描述的一系列标准验证规则。
30
29
</a>
31
30
</li >
32
31
</ul >
33
32
34
- ## Validation
33
+ ## 验证
35
34
36
35
### validate
37
36
@@ -43,26 +42,20 @@ function validate(
43
42
): Array<GraphQLError>
44
43
```
45
44
46
- Implements the "Validation" section of the spec.
45
+ 实现规范的“验证”章节。
47
46
48
- Validation runs synchronously, returning an array of encountered errors, or
49
- an empty array if no errors were encountered and the document is valid.
47
+ 验证过程会同步执行,如果发生错误则返回错误数组,如果文档有效没有发生错误,则返回空数组。
50
48
51
- A list of specific validation rules may be provided. If not provided, the
52
- default list of rules defined by the GraphQL specification will be used.
49
+ 如果未提供特定的验证规则,则执行 GraphQL 规范的默认验证规则。
53
50
54
- Each validation rules is a function which returns a visitor
55
- (see the language/visitor API). Visitor methods are expected to return
56
- GraphQLErrors, or Arrays of GraphQLErrors when invalid.
51
+ 每个验证规则都是一个函数,返回一个 visitor(查看 language/visitor API)。visitor 方法应当在文档无效时返回 GraphQLErrors,或者 GraphQLError 数组。
57
52
58
- Visitors can also supply `visitSpreadFragments: true` which will alter the
59
- behavior of the visitor to skip over top level defined fragments, and instead
60
- visit those fragments at every point a spread is encountered.
53
+ visitor 应该支持 `visitSpreadFragments: true`,这将改变 visitor 的行为:跳过顶层定义的片段,并访问这些片段的所有展开点。
61
54
62
55
### specifiedRules
63
56
64
57
```js
65
58
var specifiedRules: Array<(context: ValidationContext): any>
66
59
```
67
60
68
- This set includes all validation rules defined by the GraphQL spec
61
+ 这个集合包含了所有 GraphQL 规范定义的验证规则。
0 commit comments