1
1
---
2
- title : Validation
2
+ title : 验证
3
3
layout : ../_core/DocsLayout
4
- category : Learn
4
+ category : 学习
5
5
permalink : /learn/validation/
6
6
next : /learn/execution/
7
7
---
8
8
9
- By using the type system, it can be predetermined whether a GraphQL query
10
- is valid or not. This allows servers and clients to effectively inform
11
- developers when an invalid query has been created, without having to rely
12
- on runtime checks.
9
+ 通过使用类型系统,你可以预判一个查询是否有效。这让服务器和客户端可以在无效查询创建时就有效地通知开发者,而不用依赖运行时检查。
13
10
14
- For our Star Wars example, the file
15
- [ starWarsValidation-test.js] ( https://github.com/graphql/graphql-js/blob/master/src/__tests__/starWarsValidation-test.js )
16
- contains a number of queries demonstrating various invalidities, and is a test
17
- file that can be run to exercise the reference implementation's validator.
18
-
19
- To start, let's take a complex valid query. This is a nested query, similar to
20
- an example from the previous section, but with the duplicated fields factored
21
- out into a fragment:
11
+ 对于我们的《星球大战》的案例,[ starWarsValidation-test.js] ( https://github.com/graphql/graphql-js/blob/master/src/__tests__/starWarsValidation-test.js ) 这个文件包含了若干对于各种无效查询的演示,它也是一个测试文件,用于检测参考实现的验证器。
22
12
23
13
``` graphql
24
14
# { "graphiql": true }
@@ -40,11 +30,9 @@ fragment NameAndAppearances on Character {
40
30
}
41
31
```
42
32
43
- And this query is valid. Let's take a look at some invalid queries...
33
+ 上面这个查询是有效的。我们来看看一些无效查询……
44
34
45
- A fragment cannot refer to itself or create a cycle, as this could result in
46
- an unbounded result! Here's the same query above but without the explicit three
47
- levels of nesting:
35
+ 片段不能引用其自身或者创造回环,因为这会导致结果无边界。下面是一个相同的查询,但是没有显式的三层嵌套:
48
36
49
37
``` graphql
50
38
# { "graphiql": true }
@@ -63,40 +51,33 @@ fragment NameAndAppearancesAndFriends on Character {
63
51
}
64
52
```
65
53
66
- When we query for fields, we have to query for a field that exists on the
67
- given type. So as ` hero ` returns a ` Character ` , we have to query for a field
68
- on ` Character ` . That type does not have a ` favoriteSpaceship ` field, so this
69
- query is invalid:
54
+ 查询字段的时候,我们只能查询给定类型上的字段。因此由于 ` hero ` 返回 ` Character ` 类型,我们只能查询 ` Character ` 上的字段。因为这个类型上没有 ` favoriteSpaceship ` 字段,所以这个查询是无效的:
70
55
71
56
``` graphql
72
57
# { "graphiql": true }
73
- # INVALID: favoriteSpaceship does not exist on Character
58
+ # 无效: favoriteSpaceship 不存在于 Character 之上
74
59
{
75
60
hero {
76
61
favoriteSpaceship
77
62
}
78
63
}
79
64
```
80
65
81
- Whenever we query for a field and it returns something other than a scalar
82
- or an enum, we need to specify what data we want to get back from the field.
83
- Hero returns a ` Character ` , and we've been requesting fields like ` name ` and
84
- ` appearsIn ` on it; if we omit that, the query will not be valid:
66
+ 当我们查询一个字段时,如果其返回值不是标量或者枚举型,那我们就需要指明想要从这个字段中获取的数据。` hero ` 返回 ` Character ` 类型,我们也请求了其中像是 ` name ` 和` appearsIn ` 的字段;但如果将其省略,这个查询就变成无效的了:
85
67
86
68
``` graphql
87
69
# { "graphiql": true }
88
- # INVALID: hero is not a scalar, so fields are needed
70
+ # 无效: hero 不是标量,需要指明下级字段
89
71
{
90
72
hero
91
73
}
92
74
```
93
75
94
- Similarly, if a field is a scalar, it doesn't make sense to query for
95
- additional fields on it, and doing so will make the query invalid:
76
+ 类似地,如果一个字段是标量,进一步查询它上面的字段也没有意义,这样做也会导致查询无效:
96
77
97
78
``` graphql
98
79
# { "graphiql": true }
99
- # INVALID: name is a scalar, so fields are not permitted
80
+ # 无效: name 是标量,因此不允许下级字段查询
100
81
{
101
82
hero {
102
83
name {
@@ -106,14 +87,11 @@ additional fields on it, and doing so will make the query invalid:
106
87
}
107
88
```
108
89
109
- Earlier, it was noted that a query can only query for fields on the type
110
- in question; when we query for ` hero ` which returns a ` Character ` , we
111
- can only query for fields that exist on ` Character ` . What happens if we
112
- want to query for R2-D2s primary function, though?
90
+ 我们之前提到过,只有目标类型上的字段才可查询;当我们查询 ` hero ` 时,它会返回 ` Character ` ,因此只有 ` Character ` 上的字段是可查询的。但如果我们要查的是 R2-D2 的 primary function 呢?
113
91
114
92
``` graphql
115
93
# { "graphiql": true }
116
- # INVALID: primaryFunction does not exist on Character
94
+ # 无效: primaryFunction 不存在于 Character 之上
117
95
{
118
96
hero {
119
97
name
@@ -122,12 +100,7 @@ want to query for R2-D2s primary function, though?
122
100
}
123
101
```
124
102
125
- That query is invalid, because ` primaryFunction ` is not a field on ` Character ` .
126
- We want some way of indicating that we wish to fetch ` primaryFunction ` if the
127
- ` Character ` is a ` Droid ` , and to ignore that field otherwise. We can use
128
- the fragments we introduced earlier to do this. By setting up a fragment defined
129
- on ` Droid ` and including it, we ensure that we only query for ` primaryFunction `
130
- where it is defined.
103
+ 这个查询是无效的,因为 ` primaryFunction ` 并不是 ` Character ` 的字段。我们需要某种方法来表示:如果对应的 ` Character ` 是 ` Droid ` ,我们希望获取 ` primaryFunction ` 字段,而在其他情况下,则忽略此字段。我们可以使用之前引入的“片段”来解决这个问题。先在 ` Droid ` 上定义一个片段,然后在查询中引入它,这样我们就能在定义了 ` primaryFunction ` 的地方查询它。
131
104
132
105
``` graphql
133
106
# { "graphiql": true }
@@ -143,11 +116,7 @@ fragment DroidFields on Droid {
143
116
}
144
117
```
145
118
146
- This query is valid, but it's a bit verbose; named fragments were valuable
147
- above when we used them multiple times, but we're only using this one once.
148
- Instead of using a named fragment, we can use an inline fragment; this
149
- still allows us to indicate the type we are querying on, but without naming
150
- a separate fragment:
119
+ 这个查询是有效的,但是有点琐碎;具名片段在我们需要多次使用的时候更有价值,如果只使用一次,那么我们应该使用内联片段而不是具名片段;这同样能表示我们想要查询的类型,而不用单独命名一个片段:
151
120
152
121
``` graphql
153
122
# { "graphiql": true }
@@ -161,10 +130,4 @@ a separate fragment:
161
130
}
162
131
```
163
132
164
- This has just scratched the surface of the validation system; there
165
- are a number of validation rules in place to ensure that a GraphQL query
166
- is semantically meaningful. The specification goes into more detail about this
167
- topic in the "Validation" section, and the
168
- [ validation] ( https://github.com/graphql/graphql-js/blob/master/src/validation )
169
- directory in GraphQL.js contains code implementing a
170
- specification-compliant GraphQL validator.
133
+ 这只是验证系统的冰山一角;事实上需要一大套验证规则才能保证 GraphQL 查询的语义意义。规范中的“验证”章节有关于本话题更详细的内容,GraphQL.js 的 [ validation] ( https://github.com/graphql/graphql-js/blob/master/src/validation ) 目录包含了规范兼容的 GraphQL 验证器实现代码。
0 commit comments