@@ -2,13 +2,24 @@ import Foundation
22import NIO
33import GraphQL
44
5- public struct Connection < T : Encodable > : Encodable {
6- let edges : [ Edge < T > ]
5+ public struct Connection < Node : Encodable > : Encodable {
6+ let edges : [ Edge < Node > ]
77 let pageInfo : PageInfo
88}
99
1010@available ( OSX 10 . 15 , * )
11- public extension EventLoopFuture where Value : Sequence , Value. Element : Codable & Identifiable {
11+ public extension Connection where Node : Identifiable , Node. ID : LosslessStringConvertible {
12+ static func id( _ cursor: String ) -> Node . ID ? {
13+ cursor. base64Decoded ( ) . flatMap ( { Node . ID ( $0) } )
14+ }
15+
16+ static func cursor( _ id: Node . ID ) -> String ? {
17+ id. description. base64Encoded ( )
18+ }
19+ }
20+
21+ @available ( OSX 10 . 15 , * )
22+ public extension EventLoopFuture where Value : Sequence , Value. Element : Encodable & Identifiable , Value. Element. ID : LosslessStringConvertible {
1223 func connection( from arguments: Paginatable ) -> EventLoopFuture < Connection < Value . Element > > {
1324 flatMapThrowing { value in
1425 try value. connection ( from: arguments)
@@ -29,7 +40,7 @@ public extension EventLoopFuture where Value : Sequence, Value.Element : Codable
2940}
3041
3142@available ( OSX 10 . 15 , * )
32- extension Sequence where Element : Codable & Identifiable {
43+ public extension Sequence where Element : Encodable & Identifiable , Element . ID : LosslessStringConvertible {
3344 func connection( from arguments: Paginatable ) throws -> Connection < Element > {
3445 try connect ( to: Array ( self ) , arguments: PaginationArguments ( arguments) )
3546 }
@@ -44,12 +55,12 @@ extension Sequence where Element : Codable & Identifiable {
4455}
4556
4657@available ( OSX 10 . 15 , * )
47- func connect< T : Codable & Identifiable > (
48- to elements: [ T ] ,
58+ func connect< Node > (
59+ to elements: [ Node ] ,
4960 arguments: PaginationArguments
50- ) throws -> Connection < T > {
61+ ) throws -> Connection < Node > where Node : Encodable & Identifiable , Node . ID : LosslessStringConvertible {
5162 let edges = elements. map { element in
52- Edge < T > ( node: element, cursor: " \ ( element. id) " . base64Encoded ( ) !)
63+ Edge < Node > ( node: element, cursor: Connection < Node > . cursor ( element. id) !)
5364 }
5465
5566 let cursorEdges = slicingCursor ( edges: edges, arguments: arguments)
@@ -66,10 +77,10 @@ func connect<T : Codable & Identifiable>(
6677 )
6778}
6879
69- func slicingCursor< T : Codable > (
70- edges: [ Edge < T > ] ,
80+ func slicingCursor< Node : Encodable > (
81+ edges: [ Edge < Node > ] ,
7182 arguments: PaginationArguments
72- ) -> ArraySlice < Edge < T > > {
83+ ) -> ArraySlice < Edge < Node > > {
7384 var edges = ArraySlice ( edges)
7485
7586 if
@@ -92,10 +103,10 @@ func slicingCursor<T : Codable>(
92103 return edges
93104}
94105
95- func slicingCount< T : Codable > (
96- edges: ArraySlice < Edge < T > > ,
106+ func slicingCount< Node : Encodable > (
107+ edges: ArraySlice < Edge < Node > > ,
97108 arguments: PaginationArguments
98- ) throws -> Array < Edge < T > > {
109+ ) throws -> Array < Edge < Node > > {
99110 var edges = edges
100111
101112 if let first = arguments. first {
@@ -121,8 +132,8 @@ func slicingCount<T : Codable>(
121132 return Array ( edges)
122133}
123134
124- func hasPreviousPage< T : Codable > (
125- edges: ArraySlice < Edge < T > > ,
135+ func hasPreviousPage< Node : Encodable > (
136+ edges: ArraySlice < Edge < Node > > ,
126137 arguments: PaginationArguments
127138) -> Bool {
128139 if let last = arguments. last {
@@ -132,8 +143,8 @@ func hasPreviousPage<T : Codable>(
132143 return false
133144}
134145
135- func hasNextPage< T : Codable > (
136- edges: ArraySlice < Edge < T > > ,
146+ func hasNextPage< Node : Encodable > (
147+ edges: ArraySlice < Edge < Node > > ,
137148 arguments: PaginationArguments
138149) -> Bool {
139150 if let first = arguments. first {
0 commit comments