-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
GraphSchema.swift
462 lines (399 loc) · 12.4 KB
/
GraphSchema.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
import Prelude
/// Graph Response
public struct GraphResponse<T: Decodable>: Decodable {
let data: T?
let errors: [GraphResponseError]?
}
/// Base Query Types
extension Never: CustomStringConvertible {
public var description: String {
fatalError()
}
}
public protocol QueryType: CustomStringConvertible, Hashable {}
extension QueryType {
public var hashValue: Int {
return description.hashValue
}
}
public enum Connection<Q: QueryType> {
case pageInfo(NonEmptySet<PageInfo>)
case edges(NonEmptySet<Edges<Q>>)
case nodes(NonEmptySet<Q>)
case totalCount
}
public func == <Q: QueryType>(lhs: Q, rhs: Q) -> Bool {
return lhs.hashValue == rhs.hashValue
}
public func join<S: Sequence>(_ args: S, _ separator: String = " ") -> String
where S.Iterator.Element: QueryType {
return args.map { $0.description }.sorted().joined(separator: separator)
}
public func join<Q: QueryType>(_ nodes: NonEmptySet<Q>, _ separator: String = " ") -> String {
return join(Array(nodes))
}
public func decodeBase64(_ input: String) -> String? {
return Data(base64Encoded: input)
.flatMap { String(data: $0, encoding: .utf8) }
}
public func decompose(id: String) -> Int? {
return decodeBase64(id)
.flatMap { id -> Int? in
let pair = id.split(separator: "-", maxSplits: 1)
return pair.last.flatMap { Int($0) }
}
}
public struct RelayId: Swift.Decodable {
let id: String
}
extension RelayId: ExpressibleByStringLiteral {
public init(unicodeScalarLiteral value: String) {
self.init(id: value)
}
public init(extendedGraphemeClusterLiteral value: String) {
self.init(id: value)
}
public init(stringLiteral value: String) {
self.init(id: value)
}
}
public enum QueryArg<T: CustomStringConvertible> {
case after(String)
case before(String)
case first(Int)
case last(Int)
case arg(T)
}
public enum EdgesContainerBody<Q: QueryType> {
case pageInfo(NonEmptySet<PageInfo>)
case edges(NonEmptySet<Edges<Q>>)
}
public enum PageInfo: String {
case endCursor
case hasNextPage
case hasPreviousPage
case startCursor
}
public enum Edges<Q: QueryType> {
case cursor
case node(NonEmptySet<Q>)
}
public enum Nodes<Q: QueryType> {
case nodes(NonEmptySet<Q>)
}
public struct GraphResponseError: Decodable {
public let message: String
}
public enum GraphError: Error {
case invalidInput
case invalidJson(responseString: String?)
case requestError(Error, URLResponse?)
case emptyResponse(URLResponse?)
case decodeError(GraphResponseError)
case jsonDecodingError(responseString: String?, error: Error?)
}
public enum Query {
case category(id: String, NonEmptySet<Category>)
case project(slug: String, NonEmptySet<Project>)
case rootCategories(NonEmptySet<Category>)
case user(NonEmptySet<User>)
public enum Amount {
case amount
case currency
}
public enum Category {
public enum ProjectsConnection {
public enum Argument {
case state(Project.State)
}
}
case id
case name
case parentId
case parentCategory
case projects(Set<QueryArg<ProjectsConnection.Argument>>, NonEmptySet<Connection<Project>>)
case slug
indirect case subcategories(Set<QueryArg<Never>>, NonEmptySet<Connection<Category>>)
case totalProjectCount
case url
}
public enum Conversation {
case id
}
public enum Location: String {
case country
case displayableName
case id
case name
}
public enum NewsletterSubscriptions: String {
case alumniNewsletter
case artsCultureNewsletter
case filmNewsletter
case gamesNewsletter
case happeningNewsletter
case inventNewsletter
case promoNewsletter
case publishingNewsletter
case weeklyNewsletter
case musicNewsletter
}
public enum Notifications: String {
case email
case mobile
case topic
}
public enum Project {
case id
case slug
case updates(Set<QueryArg<Never>>, NonEmptySet<Connection<Project.Update>>)
public enum State: String {
case failed = "FAILED"
case live = "LIVE"
case successful = "SUCCESSFUL"
}
public enum Update {
indirect case author(NonEmptySet<User>)
case id
case publishedAt
case title
}
}
public enum User {
case biography
case backedProjects(Set<QueryArg<Never>>, NonEmptySet<Connection<Project>>)
case chosenCurrency
case conversations(Set<QueryArg<Never>>, NonEmptySet<Connection<Conversation>>)
case createdProjects(Set<QueryArg<Never>>, NonEmptySet<Connection<Project>>)
case drop
case email
indirect case followers(Set<QueryArg<Never>>, NonEmptySet<Connection<User>>)
indirect case following(Set<QueryArg<Never>>, NonEmptySet<Connection<User>>)
case hasUnreadMessages
case id
case image(alias: String, width: Int)
case imageUrl(alias: String, blur: Bool, width: Int)
case isEmailDeliverable
case isEmailVerified
case isFollowing
case isSocializing
case ksrLiveAuthToken
case location(NonEmptySet<Location>)
case membershipProjects(Set<QueryArg<Never>>, NonEmptySet<Connection<Project>>)
case name
case needsFreshFacebookToken
case newletterSubscriptions(NonEmptySet<NewsletterSubscriptions>)
case notifications(NonEmptySet<Notifications>)
case optedOutOfRecommendations
case showPublicProfile
case savedProjects(Set<QueryArg<Never>>, NonEmptySet<Connection<Project>>)
case slug
case url
case userId
}
}
extension Query {
public static func build(_ queries: NonEmptySet<Query>) -> String {
return "{ \(join(queries)) }"
}
}
extension Query: QueryType {
public var description: String {
switch self {
case let .category(id, fields):
return "node(id: \"\(id)\") { ... on Category { \(join(fields)) } }"
case let .project(slug, fields):
return "project(slug: \"\(slug)\") { \(join(fields)) }"
case let .rootCategories(fields):
return "rootCategories { \(join(fields)) }"
case let .user(fields):
return "me { \(join(fields)) }"
}
}
}
extension QueryArg: QueryType {
public var description: String {
switch self {
case let .after(cursor):
return "after: \"\(cursor)\""
case let .before(cursor):
return "before: \"\(cursor)\""
case let .first(count):
return "first: \(count)"
case let .last(count):
return "last: \(count)"
case let .arg(arg):
return arg.description
}
}
}
extension EdgesContainerBody: QueryType {
public var description: String {
switch self {
case let .edges(fields):
return "edges { \(join(fields)) }"
case let .pageInfo(pageInfo):
return "pageInfo { \(join(pageInfo)) }"
}
}
}
extension Edges: QueryType {
public var description: String {
switch self {
case .cursor: return "cursor"
case let .node(fields): return "node { \(join(fields)) }"
}
}
}
extension Nodes: QueryType {
public var description: String {
switch self {
case let .nodes(fields):
return "nodes { \(join(fields)) }"
}
}
}
extension PageInfo: QueryType {
public var description: String {
return rawValue
}
public static var all: NonEmptySet<PageInfo> {
return .endCursor +| [
.hasNextPage,
.hasPreviousPage,
.startCursor
]
}
}
extension Connection: QueryType {
public var description: String {
switch self {
case let .nodes(fields): return "nodes { \(join(fields)) }"
case let .pageInfo(pageInfo): return "pageInfo { \(join(pageInfo)) }"
case let .edges(fields): return "edges { \(join(fields)) }"
case .totalCount: return "totalCount"
}
}
}
/// Category
extension Query.Category: QueryType {
public var description: String {
switch self {
case .id: return "id"
case .name: return "name"
case .parentCategory: return "parentCategory { id name }"
case .parentId: return "parentId"
case let .projects(args, fields): return "projects" + connection(args, fields)
case .slug: return "slug"
case let .subcategories(args, fields): return "subcategories" + connection(args, fields)
case .totalProjectCount: return "totalProjectCount"
case .url: return "url"
}
}
}
extension Query.Category.ProjectsConnection.Argument: CustomStringConvertible {
public var description: String {
switch self {
case let .state(state): return "state: \(state.rawValue)"
}
}
}
/// Project
extension Query.Project: QueryType {
public var description: String {
switch self {
case .id: return "id"
case .slug: return "slug"
case let .updates(args, fields): return "updates\(connection(args, fields))"
}
}
}
/// Update
extension Query.Project.Update: QueryType {
public var description: String {
switch self {
case let .author(fields): return "author { \(join(fields)) }"
case .id: return "id"
case .publishedAt: return "publishedAt"
case .title: return "title"
}
}
}
// MARK: User
extension Query.User: QueryType {
public var description: String {
switch self {
case .biography: return "biography"
case let .backedProjects(args, fields): return "backedProjects\(connection(args, fields))"
case let .conversations(args, fields): return "conversations\(connection(args, fields))"
case .chosenCurrency: return "chosenCurrency"
case let .createdProjects(args, fields): return "createdProjects\(connection(args, fields))"
case .drop: return "drop"
case .email: return "email"
case let .followers(args, fields): return "followers\(connection(args, fields))"
case let .following(args, fields): return "following\(connection(args, fields))"
case .hasUnreadMessages: return "hasUnreadMessages"
case .id: return "id"
case let .image(alias, width): return "\(alias): imageUrl(width: \(width))"
case let .imageUrl(alias, blur, width): return "\(alias): imageUrl(blur: \(blur), width: \(width))"
case .isEmailDeliverable: return "isDeliverable"
case .isEmailVerified: return "isEmailVerified"
case .isFollowing: return "isFollowing"
case .isSocializing: return "isSocializing"
case .ksrLiveAuthToken: return "ksrLiveAuthToken"
case let .location(fields): return "location { \(join(fields)) }"
case let .membershipProjects(args, fields): return "membershipProjects\(connection(args, fields))"
case .name: return "name"
case .needsFreshFacebookToken: return "needsFreshFacebookToken"
case let .newletterSubscriptions(fields): return "newslettersSubscriptions { \(join(fields)) }"
case let .notifications(fields): return "notifications { \(join(fields)) }"
case .optedOutOfRecommendations: return "optedOutOfRecommendations"
case let .savedProjects(args, fields): return "savedProjects\(connection(args, fields))"
case .showPublicProfile: return "showPublicProfile"
case .slug: return "slug"
case .url: return "url"
case .userId: return "uid"
}
}
}
// MARK: NewsletterSubscriptions
extension Query.NewsletterSubscriptions: QueryType {
public var description: String {
return self.rawValue
}
}
// MARK: Notifications
extension Query.Notifications: QueryType {
public var description: String {
return self.rawValue
}
}
/// Location
extension Query.Location: QueryType {
public var description: String {
return self.rawValue
}
}
extension Query.Amount: QueryType {
public var description: String {
switch self {
case .amount: return "amount"
case .currency: return "currency"
}
}
}
/// Conversation
extension Query.Conversation: QueryType {
public var description: String {
switch self {
case .id: return "id"
}
}
}
/// Helpers
private func connection<T, U>(_ args: Set<QueryArg<T>>, _ fields: NonEmptySet<Connection<U>>) -> String {
return "\(_args(args)) { \(join(fields)) }"
}
private func _args<T>(_ args: Set<QueryArg<T>>) -> String {
return !args.isEmpty ? "(\(join(args)))" : ""
}