Skip to content

Commit 586f0a4

Browse files
authored
Update README.md
1 parent c4d5fb8 commit 586f0a4

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

README.md

+64
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,70 @@
22

33
A Swift implementation of the [GraphQL Cursor Connections Specification](https://relay.dev/graphql/connections.htm).
44

5+
## Purpose
6+
7+
This package provides an alternative to the pagination/connection tools provided by [Graphiti](https://github.com/GraphQLSwift/Graphiti/tree/main/Sources/Graphiti/Connection). Benefits include:
8+
9+
* Create your own `connection/edge` types that expose additional data
10+
* An opaque `Cursor` type that supports identity-based or index-based cursors
11+
* Converts GraphQL pagination model to offset pagination to give to your database, elasticsearch, etc.
12+
* Offset pagination performs a "plus two" algorithm to efficiently calculate `hasNextPage` and `hasPreviousPage`
13+
14+
## Example
15+
16+
Add pagination to any arguments.
17+
18+
```swift
19+
/// This supports forward and backward pagination. We could use `GraphForwardPaginatable` to simplify.
20+
struct PeopleArguments: GraphPaginatable {
21+
// ... other inputs
22+
var first: Int?
23+
var after: Cursor?
24+
var last: Int?
25+
var before: Cursor?
26+
}
27+
```
28+
29+
Use pagination.
30+
31+
```swift
32+
func people(context: Context, arguments: PeopleArguments) -> BasicConnection<Person> {
33+
// Extract offset and count from the input, and query the database.
34+
let offsetPagination = arguments.pagination.makeOffsetPagination()
35+
let people = try await Person.all(offset: offsetPagination.offset, count: offsetPagination.count)
36+
// Create a connection data structure converting people into edges with index-based cursors.
37+
return BasicConnection(
38+
nodes: people,
39+
pagination: arguments.pagination,
40+
cursor: .index
41+
)
42+
}
43+
```
44+
45+
GraphQL
46+
47+
```json
48+
{
49+
"people": {
50+
"edges": [
51+
{
52+
"cursor": "MA==",
53+
"node": { }
54+
},
55+
{
56+
"cursor": "MQ==",
57+
"node": { }
58+
}
59+
],
60+
"pageInfo": {
61+
"hasPreviousPagePage": false,
62+
"hasNextPage": true,
63+
"startCursor": "MA==",
64+
"startCursor": "MQ=="
65+
}
66+
}
67+
}
68+
```
569

670
# License
771

0 commit comments

Comments
 (0)