Skip to content

Commit d018087

Browse files
committed
Improve connection.
1 parent 1d91127 commit d018087

File tree

2 files changed

+33
-22
lines changed

2 files changed

+33
-22
lines changed

Sources/Graphiti/Connection/Connection.swift

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,24 @@ import Foundation
22
import NIO
33
import 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 {
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
protocol Edgeable {
2-
associatedtype T : Encodable
3-
var node: T { get }
2+
associatedtype Node : Encodable
3+
var node: Node { get }
44
var cursor: String { get }
55
}
66

7-
struct Edge<T : Encodable> : Edgeable, Encodable {
8-
let node: T
7+
struct Edge<Node : Encodable> : Edgeable, Encodable {
8+
let node: Node
99
let cursor: String
1010
}

0 commit comments

Comments
 (0)