Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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