|
| 1 | +import Foundation |
| 2 | +import NIO |
| 3 | +import GraphQL |
| 4 | + |
| 5 | +public struct Connection<T : Encodable> : Encodable, Keyable { |
| 6 | + public enum Keys : String { |
| 7 | + case edges |
| 8 | + case pageInfo |
| 9 | + } |
| 10 | + |
| 11 | + let edges: [Edge<T>] |
| 12 | + let pageInfo: PageInfo |
| 13 | +} |
| 14 | + |
| 15 | +@available(OSX 10.15, *) |
| 16 | +public extension EventLoopFuture where Value : Sequence, Value.Element : Codable & Identifiable { |
| 17 | + func connection(from arguments: Paginatable) -> EventLoopFuture<Connection<Value.Element>> { |
| 18 | + flatMapThrowing { value in |
| 19 | + try value.connection(from: arguments) |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + func connection(from arguments: ForwardPaginatable) -> EventLoopFuture<Connection<Value.Element>> { |
| 24 | + flatMapThrowing { value in |
| 25 | + try value.connection(from: arguments) |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + func connection(from arguments: BackwardPaginatable) -> EventLoopFuture<Connection<Value.Element>> { |
| 30 | + flatMapThrowing { value in |
| 31 | + try value.connection(from: arguments) |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +@available(OSX 10.15, *) |
| 37 | +extension Sequence where Element : Codable & Identifiable { |
| 38 | + func connection(from arguments: Paginatable) throws -> Connection<Element> { |
| 39 | + try connect(to: Array(self), arguments: PaginationArguments(arguments)) |
| 40 | + } |
| 41 | + |
| 42 | + func connection(from arguments: ForwardPaginatable) throws -> Connection<Element> { |
| 43 | + try connect(to: Array(self), arguments: PaginationArguments(arguments)) |
| 44 | + } |
| 45 | + |
| 46 | + func connection(from arguments: BackwardPaginatable) throws -> Connection<Element> { |
| 47 | + try connect(to: Array(self), arguments: PaginationArguments(arguments)) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +@available(OSX 10.15, *) |
| 52 | +func connect<T : Codable & Identifiable>( |
| 53 | + to elements: [T], |
| 54 | + arguments: PaginationArguments |
| 55 | +) throws -> Connection<T> { |
| 56 | + let edges = elements.map { element in |
| 57 | + Edge<T>(node: element, cursor: "\(element.id)".base64Encoded()!) |
| 58 | + } |
| 59 | + |
| 60 | + let cursorEdges = slicingCursor(edges: edges, arguments: arguments) |
| 61 | + let countEdges = try slicingCount(edges: cursorEdges, arguments: arguments) |
| 62 | + |
| 63 | + return Connection( |
| 64 | + edges: countEdges, |
| 65 | + pageInfo: PageInfo( |
| 66 | + hasPreviousPage: hasPreviousPage(edges: cursorEdges, arguments: arguments), |
| 67 | + hasNextPage: hasNextPage(edges: cursorEdges, arguments: arguments), |
| 68 | + startCursor: countEdges.first.map(\.cursor), |
| 69 | + endCursor: countEdges.last.map(\.cursor) |
| 70 | + ) |
| 71 | + ) |
| 72 | +} |
| 73 | + |
| 74 | +func slicingCursor<T : Codable>( |
| 75 | + edges: [Edge<T>], |
| 76 | + arguments: PaginationArguments |
| 77 | +) -> ArraySlice<Edge<T>> { |
| 78 | + var edges = ArraySlice(edges) |
| 79 | + |
| 80 | + if |
| 81 | + let after = arguments.after, |
| 82 | + let afterIndex = edges |
| 83 | + .firstIndex(where: { $0.cursor == after })? |
| 84 | + .advanced(by: 1) |
| 85 | + { |
| 86 | + edges = edges[afterIndex...] |
| 87 | + } |
| 88 | + |
| 89 | + if |
| 90 | + let before = arguments.before, |
| 91 | + let beforeIndex = edges |
| 92 | + .firstIndex(where: { $0.cursor == before }) |
| 93 | + { |
| 94 | + edges = edges[..<beforeIndex] |
| 95 | + } |
| 96 | + |
| 97 | + return edges |
| 98 | +} |
| 99 | + |
| 100 | +func slicingCount<T : Codable>( |
| 101 | + edges: ArraySlice<Edge<T>>, |
| 102 | + arguments: PaginationArguments |
| 103 | +) throws -> Array<Edge<T>> { |
| 104 | + var edges = edges |
| 105 | + |
| 106 | + if let first = arguments.first { |
| 107 | + if first < 0 { |
| 108 | + throw GraphQLError( |
| 109 | + message: #"Invalid agurment "first". Argument must be a positive integer."# |
| 110 | + ) |
| 111 | + } |
| 112 | + |
| 113 | + edges = edges.prefix(first) |
| 114 | + } |
| 115 | + |
| 116 | + if let last = arguments.last { |
| 117 | + if last < 0 { |
| 118 | + throw GraphQLError( |
| 119 | + message: #"Invalid agurment "last". Argument must be a positive integer."# |
| 120 | + ) |
| 121 | + } |
| 122 | + |
| 123 | + edges = edges.suffix(last) |
| 124 | + } |
| 125 | + |
| 126 | + return Array(edges) |
| 127 | +} |
| 128 | + |
| 129 | +func hasPreviousPage<T : Codable>( |
| 130 | + edges: ArraySlice<Edge<T>>, |
| 131 | + arguments: PaginationArguments |
| 132 | +) -> Bool { |
| 133 | + if let last = arguments.last { |
| 134 | + return edges.count > last |
| 135 | + } |
| 136 | + |
| 137 | + return false |
| 138 | +} |
| 139 | + |
| 140 | +func hasNextPage<T : Codable>( |
| 141 | + edges: ArraySlice<Edge<T>>, |
| 142 | + arguments: PaginationArguments |
| 143 | +) -> Bool { |
| 144 | + if let first = arguments.first { |
| 145 | + return edges.count > first |
| 146 | + } |
| 147 | + |
| 148 | + return false |
| 149 | +} |
| 150 | + |
| 151 | +extension String { |
| 152 | + func base64Encoded() -> String? { |
| 153 | + return data(using: .utf8)?.base64EncodedString() |
| 154 | + } |
| 155 | + |
| 156 | + func base64Decoded() -> String? { |
| 157 | + guard let data = Data(base64Encoded: self) else { |
| 158 | + return nil |
| 159 | + } |
| 160 | + |
| 161 | + return String(data: data, encoding: .utf8) |
| 162 | + } |
| 163 | +} |
0 commit comments