Skip to content

Commit 137a72b

Browse files
dabanleelinhe0x0
authored andcommitted
Tutorial-ObjectTypes.md (xitu#74)
1 parent 5a6b699 commit 137a72b

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

site/graphql-js/Tutorial-ObjectTypes.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
---
2-
title: Object Types
2+
title: 对象类型
33
layout: ../_core/GraphQLJSLayout
4-
category: GraphQL.js Tutorial
4+
category: GraphQL.js 教程
55
permalink: /graphql-js/object-types/
66
next: /graphql-js/mutations-and-input-types/
77
---
88

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 刚好可以完美地契合你的这个要求。
1010

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/) 一节中,我们有一个掷骰子的方法:
1212

1313
```javascript
1414
type Query {
1515
rollDice(numDice: Int!, numSides: Int): [Int]
1616
}
1717
```
1818

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.
19+
如果随着时间的推移我们想要有越来越多的基于随机骰子的方法,我们可以实现一个 `RandomDie` 的对象类型来替代。
2020

2121
```javascript
2222
type RandomDie {
@@ -28,7 +28,7 @@ type Query {
2828
}
2929
```
3030

31-
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` 对象类型:
3232

3333
```javascript
3434
class RandomDie {
@@ -56,7 +56,7 @@ var root = {
5656
}
5757
```
5858

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:
59+
对于那些不使用任何参数的字段来说,你可以使用对象属性或实例方法来表示。对于上面的示例方法,不论是 `numSides` 还是 `rollOnce` 实际上都可以被用来实现 GraphQL 的字段,所以上面的代码同样可以以 schema 的方式来实现:
6060

6161
```javascript
6262
type RandomDie {
@@ -70,14 +70,14 @@ type Query {
7070
}
7171
```
7272

73-
Putting this all together, here is some sample code that runs a server with this GraphQL API:
73+
最后把这些代码都整理到一起,这里是一些使用该 GraphQL API 运行服务器的示例代码:
7474

7575
```javascript
7676
var express = require('express');
7777
var graphqlHTTP = require('express-graphql');
7878
var { buildSchema } = require('graphql');
7979

80-
// Construct a schema, using GraphQL schema language
80+
// 用 GraphQL schema language 构造一个 schema
8181
var schema = buildSchema(`
8282
type RandomDie {
8383
numSides: Int!
@@ -90,7 +90,7 @@ var schema = buildSchema(`
9090
}
9191
`);
9292

93-
// This class implements the RandomDie GraphQL type
93+
// 该类继承 RandomDie GraphQL 类型
9494
class RandomDie {
9595
constructor(numSides) {
9696
this.numSides = numSides;
@@ -109,7 +109,7 @@ class RandomDie {
109109
}
110110
}
111111

112-
// The root provides the top-level API endpoints
112+
// root 规定了顶层的 API 入口端点
113113
var root = {
114114
getDie: function ({numSides}) {
115115
return new RandomDie(numSides || 6);
@@ -126,7 +126,7 @@ app.listen(4000);
126126
console.log('Running a GraphQL API server at localhost:4000/graphql');
127127
```
128128

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:
129+
当你对一个返回对象类型的 API 发出 GraphQL 查询时,你可以通过嵌套 GraphQL 字段名来一次性调用对象上的多个方法。例如,如果你想在调用 `rollOnce` 方法掷 1 次骰子的同时也调用 `roll` 方法来掷 3 次骰子的话,你可以这么做:
130130

131131
```javascript
132132
{
@@ -137,8 +137,8 @@ When you issue a GraphQL query against an API that returns object types, you can
137137
}
138138
```
139139

140-
If you run this code with `node server.js` and browse to http://localhost:4000/graphql you can try out these APIs with GraphiQL.
140+
如果你用 `node server.js` 命令来运行这些代码并且访问 http://localhost:4000/graphql 的话,你可以用 GraphiQL 试一下这些 API。
141141

142-
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 才能获取到其他对象的信息。这样不仅节省了带宽、让你的应用跑得更快,同时也简化了你客户端应用的逻辑。
143143

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

Comments
 (0)