You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: site/graphql-js/Tutorial-ObjectTypes.md
+15-15Lines changed: 15 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -1,22 +1,22 @@
1
1
---
2
-
title: Object Types
2
+
title: 对象类型
3
3
layout: ../_core/GraphQLJSLayout
4
-
category: GraphQL.js Tutorial
4
+
category: GraphQL.js 教程
5
5
permalink: /graphql-js/object-types/
6
6
next: /graphql-js/mutations-and-input-types/
7
7
---
8
8
9
-
In many cases, you don't want to return a number or a string from an API. You want to return an object that has its own complex behavior. GraphQL is a perfect fit for this.
9
+
很多情况下,你可能不想让 API 返回一个数字或字符串。你可能会期望它返回一个带有复杂行为的对象。GraphQL 刚好可以完美地契合你的这个要求。
10
10
11
-
In GraphQL schema language, the way you define a new object type is the same way we have been defining the `Query`type in our examples. Each object can have fields that return a particular type, and methods that take arguments. For example, in the [Passing Arguments](/graphql-js/passing-arguments/)documentation, we had a method to roll some random dice:
11
+
在 GraphQL schema language 中,定义一个新的对象类型就和我们在示例中定义的 `Query`类型一样。每个对象可以有返回指定类型的字段,以及带有参数的方法。例如,在 [参数传递](/graphql-js/passing-arguments/)一节中,我们有一个掷骰子的方法:
12
12
13
13
```javascript
14
14
type Query {
15
15
rollDice(numDice: Int!, numSides: Int): [Int]
16
16
}
17
17
```
18
18
19
-
If we wanted to have more and more methods based on a random die over time, we could implement this with a `RandomDie`object type instead.
Instead of a root-level resolver for the `RandomDie`type, we can instead use an ES6 class, where the resolvers are instance methods. This code shows how the `RandomDie`schema above can be implemented:
31
+
对于 `RandomDie`类型的根级别解析器来说,我们可以用 ES6 的 class 语法来替代,这样的话这些解析器就是这个类的实例方法了。下面的代码展示了如何使用 ES6 的 class 语法来实现上面的 `RandomDie`对象类型:
32
32
33
33
```javascript
34
34
classRandomDie {
@@ -56,7 +56,7 @@ var root = {
56
56
}
57
57
```
58
58
59
-
For fields that don't use any arguments, you can use either properties on the object or instance methods. So for the example code above, both `numSides`and`rollOnce`can actually be used to implement GraphQL fields, so that code also implements the schema of:
Putting this all together, here is some sample code that runs a server with this GraphQL API:
73
+
最后把这些代码都整理到一起,这里是一些使用该 GraphQL API 运行服务器的示例代码:
74
74
75
75
```javascript
76
76
var express =require('express');
77
77
var graphqlHTTP =require('express-graphql');
78
78
var { buildSchema } =require('graphql');
79
79
80
-
//Construct a schema, using GraphQL schema language
80
+
//用 GraphQL schema language 构造一个 schema
81
81
var schema =buildSchema(`
82
82
type RandomDie {
83
83
numSides: Int!
@@ -90,7 +90,7 @@ var schema = buildSchema(`
90
90
}
91
91
`);
92
92
93
-
//This class implements the RandomDie GraphQL type
93
+
//该类继承 RandomDie GraphQL 类型
94
94
classRandomDie {
95
95
constructor(numSides) {
96
96
this.numSides= numSides;
@@ -109,7 +109,7 @@ class RandomDie {
109
109
}
110
110
}
111
111
112
-
//The root provides the top-level API endpoints
112
+
// root 规定了顶层的 API 入口端点
113
113
var root = {
114
114
getDie:function ({numSides}) {
115
115
returnnewRandomDie(numSides ||6);
@@ -126,7 +126,7 @@ app.listen(4000);
126
126
console.log('Running a GraphQL API server at localhost:4000/graphql');
127
127
```
128
128
129
-
When you issue a GraphQL query against an API that returns object types, you can call multiple methods on the object at once by nesting the GraphQL field names. For example, if you wanted to call both `rollOnce`to roll a die once, and `roll`to roll a die three times, you could do it with this query:
This way of defining object types often provides advantages over a traditional REST API. Instead of doing one API request to get basic information about an object, and then multiple subsequent API requests to find out more information about that object, you can get all of that information in one API request. That saves bandwidth, makes your app run faster, and simplifies your client-side logic.
142
+
这种定义对象类型的方式通常会比传统的 REST 风格的 API 会带来更多的好处。你可以只用一次请求就能获取到所有信息,而不是一次请求只能获取到一个对象的相关信息,然后还要请求一系列 API 才能获取到其他对象的信息。这样不仅节省了带宽、让你的应用跑得更快,同时也简化了你客户端应用的逻辑。
143
143
144
-
So far, every API we've looked at is designed for returning data. In order to modify stored data or handle complex input, it helps to [learn about mutations and input types](/graphql-js/mutations-and-input-types/).
144
+
到目前为止,我们所看到的每个 API 都是为返回数据而设计的。为了修改存储的数据或处理复杂的输入,需要继续 [学习 mutations 和 input types](/graphql-js/mutations-and-input-types/)。
0 commit comments