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/learn/BestPractice-Pagination.md
+28-29Lines changed: 28 additions & 29 deletions
Original file line number
Diff line number
Diff line change
@@ -1,18 +1,18 @@
1
1
---
2
-
title: Pagination
2
+
title: 分页
3
3
layout: ../_core/DocsLayout
4
-
category: Best Practices
4
+
category: 最佳实践
5
5
permalink: /learn/pagination/
6
6
next: /learn/caching/
7
7
---
8
8
9
-
> Different pagination models enable different client capabilities
9
+
> 不同的分页模型可以实现不同的客户端功能
10
10
11
-
A common use case in GraphQL is traversing the relationship between sets of objects. There are a number of different ways that these relationships can be exposed in GraphQL, giving a varying set of capabilities to the client developer.
The most simple way to expose a connection between objects is with a field that returns a plural type. For example, if we wanted to get a list of R2-D2's friends, we could just ask for all of them:
@@ -26,9 +26,9 @@ The most simple way to expose a connection between objects is with a field that
26
26
}
27
27
```
28
28
29
-
## Slicing
29
+
## 切片
30
30
31
-
Quickly, though, we realize that there are additional behaviors a client might want. A client might want to be able to specify how many friends they want to fetch; maybe they only want the first two. So we'd want to expose something like:
@@ -42,19 +42,19 @@ Quickly, though, we realize that there are additional behaviors a client might w
42
42
}
43
43
```
44
44
45
-
But if we just fetched the first two, we might want to paginate through the list as well; once the client fetches the first two friends, they might want to send a second request to ask for the next two friends. How can we enable that behavior?
In general, we've found that **cursor-based pagination** is the most powerful of those designed. Especially if the cursors are opaque, either offset or ID-based pagination can be implemented using cursor-based pagination (by making the cursor the offset or the ID), and using cursors gives additional flexibility if the pagination model changes in the future. As a reminder that the cursors are opaque and that their format should not be relied upon, we suggest base64 encoding them.
55
+
一般来说,我们发现**基于游标的分页**是最强大的分页。特别当游标是不透明的时,则可以使用基于游标的分页(通过为游标设置偏移或 ID)来实现基于偏移或基于 ID 的分页,并且如果分页模型在将来发生变化,则使用游标可以提供额外的灵活性。需要提醒的是,游标是不透明的,并且它们的格式不应该被依赖,我们建议用 base64 编码它们。
56
56
57
-
That leads us to a problem; though; how do we get the cursor from the object? We wouldn't want cursor to live on the `User`type; it's a property of the connection, not of the object. So we might want to introduce a new layer of indirection; our `friends`field should give us a list of edges, and an edge has both a cursor and the underlying node:
@@ -72,13 +72,13 @@ That leads us to a problem; though; how do we get the cursor from the object? We
72
72
}
73
73
```
74
74
75
-
The concept of an edge also proves useful if there is information that is specific to the edge, rather than to one of the objects. For example, if we wanted to expose "friendship time" in the API, having it live on the edge is a natural place to put it.
75
+
如果存在针对于边而不是针对于某一个对象的信息,则边这个概念也被证明是有用的。例如,如果我们想要在 API 中暴露“友谊时间”,将其放置在边里是很自然的。
76
76
77
-
## End-of-list, counts, and Connections
77
+
## 列表的结尾、计数以及连接
78
78
79
-
Now we have the ability to paginate through the connection using cursors, but how do we know when we reach the end of the connection? We have to keep querying until we get an empty list back, but we'd really like for the connection to tell us when we've reached the end so we don't need that additional request. Similarly, what if we want to know additional information about the connection itself; for example, how many total friends does R2-D2 have?
To solve both of these problems, our `friends`field can return a connection object. The connection object will then have field for the edges, as well as other information (like total count and information about whether a next page exists). So our final query might look more like:
@@ -102,18 +102,18 @@ To solve both of these problems, our `friends` field can return a connection obj
102
102
}
103
103
```
104
104
105
-
Note that we also might include `endCursor`and`startCursor` in this `PageInfo` object. This way, if we don't need any of the additional information that the edge contains, we don't need to query for the edges at all, since we got the cursors needed for pagination from `pageInfo`. This leads to a potential usability improvement for connections; instead of just exposing the `edges`list, we could also expose a dedicated list of just the nodes, to avoid a layer of indirection.
Clearly, this is more complex than our original design of just having a plural! But by adopting this design, we've unlocked a number of capabilities for the client:
109
+
显然,这比我们原来只有复数的设计更复杂!但是通过采用这种设计,我们已经为客户解锁了许多功能:
110
110
111
-
-The ability to paginate through the list.
112
-
-The ability to ask for information about the connection itself, like `totalCount`or`pageInfo`.
113
-
-The ability to ask for information about the edge itself, like `cursor`or`friendshipTime`.
114
-
-The ability to change how our backend does pagination, since the user just uses opaque cursors.
111
+
-为列表分页的能力。
112
+
-请求有关连接本身的信息的能力,如 `totalCount`或`pageInfo`。
113
+
-请求有关边本身的信息的能力,如 `cursor`或`friendshipTime`。
114
+
-改变我们后端如何实现分页的能力,因为用户仅使用不透明的游标。
115
115
116
-
To see this in action, there's an additional field in the example schema, called`friendsConnection`, that exposes all of these concepts. You can check it out in the example query. Try removing the `after`parameter to `friendsConnection`to see how the pagination will be affected. Also, try replacing the `edges` field with the helper `friends` field on the connection, which lets you get directly to the list of friends without the additional edge layer of indirection, when that's appropriate for clients.
0 commit comments