Skip to content

Commit 076140e

Browse files
authored
Merge pull request #46 from JaapWijnen/master
add custom cursors
2 parents 07f4044 + fa9ce54 commit 076140e

File tree

1 file changed

+43
-15
lines changed

1 file changed

+43
-15
lines changed

Sources/Graphiti/Connection/Connection.swift

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,54 +13,82 @@ public extension Connection where Node : Identifiable, Node.ID : LosslessStringC
1313
cursor.base64Decoded().flatMap({ Node.ID($0) })
1414
}
1515

16-
static func cursor(_ id: Node.ID) -> String? {
17-
id.description.base64Encoded()
16+
static func cursor(_ node: Node) -> String {
17+
node.id.description.base64Encoded()!
1818
}
1919
}
2020

2121
@available(OSX 10.15, *)
2222
public extension EventLoopFuture where Value : Sequence, Value.Element : Encodable & Identifiable, Value.Element.ID : LosslessStringConvertible {
2323
func connection(from arguments: Paginatable) -> EventLoopFuture<Connection<Value.Element>> {
24+
connection(from: arguments, makeCursor: Connection<Value.Element>.cursor)
25+
}
26+
27+
func connection(from arguments: ForwardPaginatable) -> EventLoopFuture<Connection<Value.Element>> {
28+
connection(from: arguments, makeCursor: Connection<Value.Element>.cursor)
29+
}
30+
31+
func connection(from arguments: BackwardPaginatable) -> EventLoopFuture<Connection<Value.Element>> {
32+
connection(from: arguments, makeCursor: Connection<Value.Element>.cursor)
33+
}
34+
}
35+
36+
public extension EventLoopFuture where Value: Sequence, Value.Element: Encodable {
37+
func connection(from arguments: Paginatable, makeCursor: @escaping (Value.Element) throws -> String) -> EventLoopFuture<Connection<Value.Element>> {
2438
flatMapThrowing { value in
25-
try value.connection(from: arguments)
39+
try value.connection(from: arguments, makeCursor: makeCursor)
2640
}
2741
}
2842

29-
func connection(from arguments: ForwardPaginatable) -> EventLoopFuture<Connection<Value.Element>> {
43+
func connection(from arguments: ForwardPaginatable, makeCursor: @escaping (Value.Element) throws -> String) -> EventLoopFuture<Connection<Value.Element>> {
3044
flatMapThrowing { value in
31-
try value.connection(from: arguments)
45+
try value.connection(from: arguments, makeCursor: makeCursor)
3246
}
3347
}
3448

35-
func connection(from arguments: BackwardPaginatable) -> EventLoopFuture<Connection<Value.Element>> {
49+
func connection(from arguments: BackwardPaginatable, makeCursor: @escaping (Value.Element) throws -> String) -> EventLoopFuture<Connection<Value.Element>> {
3650
flatMapThrowing { value in
37-
try value.connection(from: arguments)
51+
try value.connection(from: arguments, makeCursor: makeCursor)
3852
}
3953
}
4054
}
4155

4256
@available(OSX 10.15, *)
4357
public extension Sequence where Element : Encodable & Identifiable, Element.ID : LosslessStringConvertible {
4458
func connection(from arguments: Paginatable) throws -> Connection<Element> {
45-
try connect(to: Array(self), arguments: PaginationArguments(arguments))
59+
try connection(from: arguments, makeCursor: Connection<Element>.cursor)
4660
}
4761

4862
func connection(from arguments: ForwardPaginatable) throws -> Connection<Element> {
49-
try connect(to: Array(self), arguments: PaginationArguments(arguments))
63+
try connection(from: arguments, makeCursor: Connection<Element>.cursor)
5064
}
5165

5266
func connection(from arguments: BackwardPaginatable) throws -> Connection<Element> {
53-
try connect(to: Array(self), arguments: PaginationArguments(arguments))
67+
try connection(from: arguments, makeCursor: Connection<Element>.cursor)
68+
}
69+
}
70+
71+
public extension Sequence where Element : Encodable {
72+
func connection(from arguments: Paginatable, makeCursor: @escaping (Element) throws -> String) throws -> Connection<Element> {
73+
try connect(to: Array(self), arguments: PaginationArguments(arguments), makeCursor: makeCursor)
74+
}
75+
76+
func connection(from arguments: ForwardPaginatable, makeCursor: @escaping (Element) throws -> String) throws -> Connection<Element> {
77+
try connect(to: Array(self), arguments: PaginationArguments(arguments), makeCursor: makeCursor)
78+
}
79+
80+
func connection(from arguments: BackwardPaginatable, makeCursor: @escaping (Element) throws -> String) throws -> Connection<Element> {
81+
try connect(to: Array(self), arguments: PaginationArguments(arguments), makeCursor: makeCursor)
5482
}
5583
}
5684

57-
@available(OSX 10.15, *)
5885
func connect<Node>(
5986
to elements: [Node],
60-
arguments: PaginationArguments
61-
) throws -> Connection<Node> where Node : Encodable & Identifiable, Node.ID : LosslessStringConvertible {
62-
let edges = elements.map { element in
63-
Edge<Node>(node: element, cursor: Connection<Node>.cursor(element.id)!)
87+
arguments: PaginationArguments,
88+
makeCursor: @escaping (Node) throws -> String
89+
) throws -> Connection<Node> where Node : Encodable {
90+
let edges = try elements.map { element in
91+
Edge<Node>(node: element, cursor: try makeCursor(element))
6492
}
6593

6694
let cursorEdges = slicingCursor(edges: edges, arguments: arguments)

0 commit comments

Comments
 (0)