Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Learn-Execution.md #55

Merged
merged 15 commits into from
Aug 17, 2017
84 changes: 33 additions & 51 deletions site/learn/Learn-Execution.md
Expand Up @@ -4,11 +4,12 @@ layout: ../_core/DocsLayout
category: Learn
permalink: /learn/execution/
next: /learn/introspection/

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个多余的空行去掉

---

After being validated, a GraphQL query is executed by a GraphQL server which returns a result that mirrors the shape of the requested query, typically as JSON.
一个 GraphQL 查询在被验证后,GraphQL 服务器会将之执行,并返回与请求的结构相对应的结果,该结果通常会是 JSON 的格式。

GraphQL cannot execute a query without a type system, let's use an example type system to illustrate executing a query. This is a part of the same type system used throughout the examples in these articles:
GraphQL 不能脱离类型系统处理查询,让我们用一个类型系统的例子来说明一个查询的执行过程,在这一系列的文章中我们重复使用了这些类型,下文是其中的一部分:

```graphql
type Query {
Expand All @@ -32,7 +33,7 @@ type Starship {
}
```

In order to describe what happens when a query is executed, let's use an example to walk through.
现在让我们用一个例子来描述当一个查询请求被执行的全过程。

```graphql
# { "graphiql": true }
Expand All @@ -47,16 +48,15 @@ In order to describe what happens when a query is executed, let's use an example
}
```

You can think of each field in a GraphQL query as a function or method of the previous type which returns the next type. In fact, this is exactly how GraphQL works. Each field on each type is backed by a function called the *resolver* which is provided by the GraphQL server developer. When a field is executed, the corresponding *resolver* is called to produce the next value.

If a field produces a scalar value like a string or number, then the execution completes. However if a field produces an object value then the query will contain another selection of fields which apply to that object. This continues until scalar values are reached. GraphQL queries always end at scalar values.
您可以将 GraphQL 查询中的每个字段视为返回子类型的父类型函数或方法。事实上,这正是 GraphQL 的工作原理。每个类型的每个字段都由一个 *resolver* 函数支持,该函数由 GraphQL 服务器开发人员提供。当一个字段被执行时,相应的 *resolver* 被调用以产生下一个值。

如果字段产生标量值,例如字符串或数字,则执行完成。但是,如果一个字段产生一个对象,则该查询将继续执行该对象对应字段的解析器,直到生成标量值。GraphQL 查询始终以标量值结束。

## Root fields & resolvers
## 根字段 & 解析器

At the top level of every GraphQL server is a type that represents all of the possible entry points into the GraphQL API, it's often called the *Root* type or the *Query* type.
每一个 GraphQL 服务端应用的顶层,必有一个类型代表着所有进入 GraphQL API 可能的入口点,我们将它称之为 *Root* 类型或 *Query* 类型。

In this example, our Query type provides a field called `human` which accepts the argument `id`. The resolver function for this field likely accesses a database and then constructs and returns a `Human` object.
在这个例子中查询类型提供了一个字段 `human`,并且接受一个参数 `id`。这个字段的解析器可能请求了数据库之后通过构造函数返回一个 `Human` 对象。

```js
Query: {
Expand All @@ -68,16 +68,15 @@ Query: {
}
```

This example is written in JavaScript, however GraphQL servers can be built in [many different languages](/code/). A resolver function receives three arguments:

- `obj` The previous object, which for a field on the root Query type is often not used.
- `args` The arguments provided to the field in the GraphQL query.
- `context` A value which is provided to every resolver and holds important contextual information like the currently logged in user, or access to a database.
这个例子使用了 JavaScript 语言,但 GraphQL 服务端应用可以被 [多种语言实现](/code/)。解析器函数接收 3 个参数:

- `obj` 上一级对象,如果字段属于根节点查询类型通常不会被使用。
- `args` 可以提供在 GraphQL 查询中传入的参数。
- `context` 会被提供给所有解析器,并且持有重要的上下文信息比如当前登入的用户或者数据库访问对象。

## Asynchronous resolvers
## 异步解析器

Let's take a closer look at what's happening in this resolver function.
让我们来分析一下在这个解析器函数中发生了什么。

```js
human(obj, args, context) {
Expand All @@ -87,14 +86,13 @@ human(obj, args, context) {
}
```

The `context` is used to provide access to a database which is used to load the data for a user by the `id` provided as an argument in the GraphQL query. Since loading from a database is an asynchronous operation, this returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise). In JavaScript Promises are used to work with asynchronous values, but the same concept exists in many languages, often called *Futures*, *Tasks* or *Deferred*. When the database returns, we can construct and return a new `Human` object.
`context` 提供了一个数据库访问对象,用来通过查询中传递的参数 `id` 来查询数据,因为从数据库拉取数据的过程是一个异步操作,该方法返回了一个 [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) 对象,在 JavaScript 语言中 Promise 对象用来处理异步操作,但在许多语言中存在相同的概念,通常称作 *Futures**Tasks* 或者 *Defferred*。当数据库返回查询结果,我们就能构造并返回一个新的 `Human` 对象。

Notice that while the resolver function needs to be aware of Promises, the GraphQL query does not. It simply expects the `human` field to return something which it can then ask the `name` of. During execution, GraphQL will wait for Promises, Futures, and Tasks to complete before continuing and will do so with optimal concurrency.
这里要注意的是,只有解析器能感知到 Promise 的进度,GraphQL 查询只关注一个包含着 `name` 属性的 `human` 字段是否返回,在执行期间如果异步操作没有完成,则 GraphQL 会一直等待下去,因此在这个环节需要关注异步处理上的优化。

## 不重要的解析器

## Trivial resolvers

Now that a `Human` object is available, GraphQL execution can continue with the fields requested on it.
现在 `Human` 对象已经生成了,但 GraphQL 还是会继续递归执行下去。

```js
Human: {
Expand All @@ -104,16 +102,15 @@ Human: {
}
```

A GraphQL server is powered by a type system which is used to determine what to do next. Even before the `human` field returns anything, GraphQL knows that the next step will be to resolve fields on the `Human` type since the type system tells it that the `human` field will return a `Human`.

Resolving the name in this case is very straight-forward. The name resolver function is called and the `obj` argument is the `new Human` object returned from the previous field. In this case, we expect that Human object to have a `name` property which we can read and return directly.
GraphQL 服务端应用的业务取决于类型系统的结构。在 `human` 对象返回值之前,由于类型系统确定了 `human` 字段将返回一个 `Human` 对象,GraphQL 会根据类型系统预设好的 `Human` 类型决定如何解析字段。

In fact, many GraphQL libraries will let you omit resolvers this simple and will just assume that if a resolver isn't provided for a field, that a property of the same name should be read and returned.
在这个例子中,对 name 字段的处理非常的清晰,name 字段对应的解析器被调用的时候,解析器回调函数的 obj 参数是由上层回调函数生成的 `new Human` 对象。在这个案例中,我们希望 Human 对象会拥有一个 `name` 属性可以让我们直接读取。

事实上在返回的字段可以直接从对象中获得并且没有额外定义解析器的时候,大部分 GraphQL 库可以让我们省略定义的步骤。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

意思还是有偏差……没有体现出“没有定义解析器再去读取对象”的先后关系
可译为:“事实上,许多 GraphQL 库可以让你省略这些简单的解析器,假定一个字段没有提供解析器时,那么应​​该从上层返回对象中读取和返回和这个字段同名的属性。”


## Scalar coercion
## 标量强制

While the `name` field is being resolved, the `appearsIn` and `starships` fields can be resolved concurrently. The `appearsIn` field could also have a trivial resolver, but let's take a closer look:
`name` 字段被处理后,`appearsIn` `starships` 字段可以被同步执行, `appearsIn` 字段也可以有一个简单的解析器,但是让我们仔细看看。

```js
Human: {
Expand All @@ -123,14 +120,13 @@ Human: {
}
```

Notice that our type system claims `appearsIn` will return Enum values with known values, however this function is returning numbers! Indeed if we look up at the result we'll see that the appropriate Enum values are being returned. What's going on?
请注意,我们的类型系统声明 `appearsIn` 字段将返回具有已知值的枚举值,但是此函数返回数字!实际上,如果我们查看结果,我们将看到正在返回适当的枚举值。这是怎么回事?

This is an example of scalar coercion. The type system knows what to expect and will convert the values returned by a resolver function into something that upholds the API contract. In this case, there may be an Enum defined on our server which uses numbers like `4`, `5`, and `6` internally, but represents them as Enum values in the GraphQL type system.
这是一个强制标量的例子。因为类型系统已经被设定,所以解析器函数的返回值必须符合与类型系统对应的 API 规则的约束。在这个案例中, 我们可能在服务器上定义了一个枚举类型,它在内部使用像是 4、5 和 6 这样的数字,但在 GraphQL 类型系统中将它们表示为枚举值。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“我们可能……”前边的空格去掉


## 列表解析器

## List resolvers

We've already seen a bit of what happens when a field returns a list of things with the `appearsIn` field above. It returned a *list* of enum values, and since that's what the type system expected, each item in the list was coerced to the appropriate enum value. What happens when the `starships` field is resolved?
我们已经看到一个字段返回上面的 `appearsIn` 字段的事物列表时会发生什么。它返回了枚举值的**列表**,因为这是系统期望的类型,列表中的每个项目被强制为适当的枚举值。让我们看下 `startships` 被解析的时候会发生什么?

```js
Human: {
Expand All @@ -144,26 +140,12 @@ Human: {
}
```

The resolver for this field is not just returning a Promise, it's returning a *list* of Promises. The `Human` object had a list of ids of the `Starships` they piloted, but we need to go load all of those ids to get real Starship objects.

GraphQL will wait for all of these Promises concurrently before continuing, and when left with a list of objects, it will concurrently continue yet again to load the `name` field on each of these items.

解析器在这个字段中不仅仅是返回了一个 Promise 对象,它返回一个 Promises **列表**。 `Human` 对象具有他们正在驾驶的 `Starships` 的 ids 列表,但是我们需要通过这些 id 来获得真正的 Starship 对象。

## Producing the result
GraphQL 将并发执行这些 Promise,当执行结束返回一个对象列表后,它将继续并发加载列表中每个对象的 `name` 字段。

As each field is resolved, the resulting value is placed into a key-value map with the field name (or alias) as the key and the resolved value as the value, this continues from the bottom leaf fields of the query all the way back up to the original field on the root Query type. Collectively these produce a structure that mirrors the original query which can then be sent (typically as JSON) to the client which requested it.
## 产生结果

Let's take one last look at the original query to see how all these resolving functions produce a result:
当每个字段被解析时,结果被放置到键值映射中,字段名称(或别名)作为键值映射的键,解析器的值作为键值映射的值,这个过程从查询字段的底部叶子节点开始返回,直到根 Query 类型的起始节点。总而言之,最后合并成为能够镜像到原始查询结构的结果,然后可以将其发送(通常为 JSON 格式)到请求的客户端。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“总而言之,”去掉


```graphql
# { "graphiql": true }
{
human(id: 1002) {
name
appearsIn
starships {
name
}
}
}
```
让我们最后一眼看看原来的查询,看看这些解析函数如何产生一个结果:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最后的一段代码怎么删掉了