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
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!).
> Naming things is a hard but important part of building intuitive APIs
14
+
## 共同语言
15
+
> 命名是构建直观接口中一个困难但重要的部分
16
16
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:
*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
+
*没有收件人地址,用户无法发送电子邮件
23
23
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:
Fetch the number of unread emails in my inbox for all my accounts
26
+
获取我所有帐户的收件箱里未读邮件的数量
27
27
```graphql
28
28
{
29
29
accounts {
@@ -34,7 +34,7 @@ Fetch the number of unread emails in my inbox for all my accounts
34
34
}
35
35
```
36
36
37
-
Fetch the "preview info" for the first 20 drafts in the main account
37
+
获取主账户的前二十封草稿邮件的“预览信息”
38
38
```graphql
39
39
{
40
40
mainAccount {
@@ -50,23 +50,23 @@ fragment previewInfo on Email {
50
50
}
51
51
```
52
52
53
-
## Business Logic Layer
54
-
> Your business logic layer should act as the single source of truth for enforcing business domain rules
53
+
## 业务逻辑层
54
+
> 你的业务逻辑层应作为执行业务域规则的唯一正确来源
55
55
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.
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.
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.
Build your GraphQL schema to express "what" rather than "how". Then you can improve your implementation details without breaking the interface with older clients.
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.
0 commit comments