Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

Commit

Permalink
retain bookmark order when syncing (#2572)
Browse files Browse the repository at this point in the history
  • Loading branch information
rnystrom authored Dec 30, 2018
1 parent 9525f37 commit f290b68
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions Classes/Bookmark/GitHubClient+Bookmarks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ private extension String {
}
}

private func bookmarkGraphQLIDs(from json: [String: Any]) -> [String] {
return json.compactMap {
guard let item = $0.value as? [String: Any] else { return nil }
private func bookmarkGraphQLIDs(from json: [String: Any], originalKeys: [String]) -> [String] {
// fetch by originalKeys to retain original ordering
return originalKeys.compactMap {
guard let item = json[$0] as? [String: Any] else { return nil }
if let issueOrPullRequest = item["issueOrPullRequest"] as? [String: Any] {
return issueOrPullRequest["id"] as? String
}
Expand All @@ -33,30 +34,33 @@ extension GithubClient: BookmarkViewController.Client {
bookmarks: [BookmarkCloudMigratorClientBookmarks],
completion: @escaping (BookmarkCloudMigratorClientResult) -> Void
) {
let subQueries: [String] = bookmarks.map {
let subQueries: [(key: String, query: String)] = bookmarks.map {
switch $0 {
case .issueOrPullRequest(let owner, let name, let number):
let key = "\(owner)\(name)\(number)".graphQLSafeKey
return """
return (key, """
\(key): repository(owner: "\(owner)", name: "\(name)") {
issueOrPullRequest(number: \(number)) {
... on Issue { id }
... on PullRequest { id }
}
}
"""
""")
case .repo(let owner, let name):
let key = "\(owner)\(name)".graphQLSafeKey
return """
return (key, """
\(key): repository(owner: "\(owner)", name: "\(name)") { id }
"""
""")
}
}
let query = "query{\(subQueries.joined(separator: " "))}"
let query = "query{\(subQueries.map({ $0.query }).joined(separator: " "))}"
client.send(ManualGraphQLRequest(query: query)) { result in
switch result {
case .success(let json):
let ids = bookmarkGraphQLIDs(from: json.data)
let ids = bookmarkGraphQLIDs(
from: json.data,
originalKeys: subQueries.map({ $0.key })
)
let delta = bookmarks.count - ids.count
if delta == 0 {
completion(.success(ids))
Expand Down

0 comments on commit f290b68

Please sign in to comment.