Skip to content

Commit 2b0b995

Browse files
Tina92linhe0x0
authored andcommitted
BestPractice-ThinkingInGraphs.md (xitu#33)
1 parent 8c506f4 commit 2b0b995

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed
Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
---
2-
title: Thinking in Graphs
3-
layout: ../_core/DocsLayout
4-
category: Best Practices
5-
permalink: /learn/thinking-in-graphs/
6-
next: /learn/serving-over-http/
2+
-title: 关于 Graphs 的思考
3+
-layout: ../_core/DocsLayout
4+
-category: 最佳实践
5+
-permalink: /learn/thinking-in-graphs/
6+
-next: /learn/serving-over-http/
77
---
88

9-
## It's Graphs All the Way Down [\*](https://en.wikipedia.org/wiki/Turtles_all_the_way_down)
10-
> With GraphQL, you model your business domain as a graph
9+
## 一切皆是图 [\*](https://en.wikipedia.org/wiki/Turtles_all_the_way_down)
10+
> 使用 GraphQL,你可以将你所有的业务建模为图
1111
12-
Graphs are powerful tools for modeling many real-world phenomena because they resemble our natural mental models and verbal descriptions of the underlying process. With GraphQL, you model your business domain as a graph by defining a schema; within your schema, you define different types of nodes and how they connect/relate to one another. On the client, this creates a pattern similar to Object-Oriented Programming: types that reference other types. On the server, since GraphQL only defines the interface, you have the freedom to use it with any backend (new or legacy!).
12+
图是将很多真实世界现象变成模型的强大工具,因为它们和我们自然的心理模型和基本过程的口头描述很相似。通过 GraphQL,你会把自己的业务领域通过定义 schema 建模成一张图;在你的 schema 里,你定义不同类型的节点以及它们之间是如何连接的。在客户端这边,这创建了一种类似于面向对象编程的模式:引用其他类型的类型。在服务器端,由于 GraphQL 定义了接口,你可以在任何后端自由的使用它(无论新旧!)。
1313

14-
## Shared Language
15-
> Naming things is a hard but important part of building intuitive APIs
14+
## 共同语言
15+
> 命名是构建直观接口中一个困难但重要的部分
1616
17-
Think of your GraphQL schema as an expressive shared language for your team and your users. To build a good schema, examine the everyday language you use to describe your business. For example, let's try to describe an email app in plain english:
17+
考虑下把你的 GraphQL schema 作为一种给你的团队和用户的沟通的共同语言。为了建立一个好的 schema,你必须检查你用来描述业务的日常语言。举个例子,让我们尝试用简单的文字描述一个电子邮件应用程序:
1818

19-
* A user can have multiple email accounts
20-
* Each email account has an address, inbox, drafts, deleted items, and sent items
21-
* Each email has a sender, receive date, subject, and body
22-
* Users cannot send an email without a recipient address
19+
* 一个用户可以有多个邮箱账号
20+
* 每个电子邮件帐户都有地址、收件箱、草稿箱、删除的邮件和发送的邮件
21+
* 每封邮件都有发送人、接收日期、主题和正文
22+
* 没有收件人地址,用户无法发送电子邮件
2323

24-
Naming things is a hard but important part of building intuitive APIs, so take time to carefully think about what makes sense for your problem domain and users. Your team should develop a shared understanding and consensus of these business domain rules because you will need to choose intuitive, durable names for nodes and relationships in the GraphQL schema. Try to imagine some of the queries you will want to execute:
24+
命名是构建直观的接口中一个困难但重要的部分,所以花时间仔细地考虑对于你的问题域和用户什么事有意义的。您的团队应该对这些业务领域的规则形成共同的理解和共识,因为您需要为 GraphQL schema 中的节点和关系选择直观,耐用的名称。试着去想象一些你想执行的查询:
2525

26-
Fetch the number of unread emails in my inbox for all my accounts
26+
获取我所有帐户的收件箱里未读邮件的数量
2727
```graphql
2828
{
2929
accounts {
@@ -34,7 +34,7 @@ Fetch the number of unread emails in my inbox for all my accounts
3434
}
3535
```
3636

37-
Fetch the "preview info" for the first 20 drafts in the main account
37+
获取主账户的前二十封草稿邮件的“预览信息”
3838
```graphql
3939
{
4040
mainAccount {
@@ -50,23 +50,23 @@ fragment previewInfo on Email {
5050
}
5151
```
5252

53-
## Business Logic Layer
54-
> Your business logic layer should act as the single source of truth for enforcing business domain rules
53+
## 业务逻辑层
54+
> 你的业务逻辑层应作为执行业务域规则的唯一正确来源
5555
56-
Where should you define the actual business logic? Where should you perform validation and authorization checks? The answer: inside a dedicated business logic layer. Your business logic layer should act as the single source of truth for enforcing business domain rules.
56+
你应该在哪里定义真正的业务逻辑?你应该在哪里验证,检查用户权限?就是在专门的业务逻辑层里。你的业务逻辑层应作为执行业务域规则的唯一正确来源。
5757

58-
![Business Logic Layer Diagram](/img/diagrams/business_layer.png)
58+
![业务逻辑层图](/img/diagrams/business_layer.png)
5959

60-
In the diagram above, all entry points (REST, GraphQL, and RPC) into the system will be processed with the same validation, authorization, and error handling rules.
60+
在上图中,系统中的所有入口点(REST、GraphQL和RPC)都将使用相同的验证、授权和错误处理规则进行处理。
6161

62-
### Working with Legacy Data
63-
> Prefer building a GraphQL schema that describes how clients use the data, rather than mirroring the legacy database schema.
62+
### 使用旧有的数据
63+
> 希望构建一个描述客户端如何使用数据的 GraphQL schema,而不是镜像旧有的数据库 schema
6464
65-
Sometimes, you will find yourself working with legacy data sources that do not perfectly reflect how clients consume the data. In these cases, prefer building a GraphQL schema that describes how clients use the data, rather than mirroring the legacy database schema.
65+
有时候,你会发现自己正在使用不能完全反映客户端消费数据的旧有的数据源。在这种情况下,更倾向于构建一个描述客户端如何使用数据的 GraphQL schema,而不是镜像旧有的数据库 schema
6666

67-
Build your GraphQL schema to express "what" rather than "how". Then you can improve your implementation details without breaking the interface with older clients.
67+
构建一个表达“是什么”而不是“怎么做”的 GraphQL schema。然后,您可以改进执行的具体细节,而不会破坏与旧客户端的接口。
6868

69-
## One Step at a time
70-
> Get validation and feedback more frequently
69+
## 一次一步
70+
> 更频繁地进行验证和获得反馈
7171
72-
Don't try to model your entire business domain in one sitting. Rather, build only the part of the schema that you need for one scenario at a time. By gradually expanding the schema, you will get validation and feedback more frequently to steer you toward building the right solution.
72+
不要试图一次就做一个模型来构建整个业务域。 而是一次只构建一个场景所需的部分 schema。渐渐地拓展 schema,你要更频繁地进行验证和获得反馈,以便寻找到构建的正确解决方案。

0 commit comments

Comments
 (0)