@@ -16,9 +16,10 @@ private extension String {
1616 }
1717}
1818
19- private func bookmarkGraphQLIDs( from json: [ String : Any ] ) -> [ String ] {
20- return json. compactMap {
21- guard let item = $0. value as? [ String : Any ] else { return nil }
19+ private func bookmarkGraphQLIDs( from json: [ String : Any ] , originalKeys: [ String ] ) -> [ String ] {
20+ // fetch by originalKeys to retain original ordering
21+ return originalKeys. compactMap {
22+ guard let item = json [ $0] as? [ String : Any ] else { return nil }
2223 if let issueOrPullRequest = item [ " issueOrPullRequest " ] as? [ String : Any ] {
2324 return issueOrPullRequest [ " id " ] as? String
2425 }
@@ -33,30 +34,33 @@ extension GithubClient: BookmarkViewController.Client {
3334 bookmarks: [ BookmarkCloudMigratorClientBookmarks ] ,
3435 completion: @escaping ( BookmarkCloudMigratorClientResult ) -> Void
3536 ) {
36- let subQueries : [ String ] = bookmarks. map {
37+ let subQueries : [ ( key : String , query : String ) ] = bookmarks. map {
3738 switch $0 {
3839 case . issueOrPullRequest( let owner, let name, let number) :
3940 let key = " \( owner) \( name) \( number) " . graphQLSafeKey
40- return """
41+ return ( key , """
4142 \( key) : repository(owner: " \( owner) " , name: " \( name) " ) {
4243 issueOrPullRequest(number: \( number) ) {
4344 ... on Issue { id }
4445 ... on PullRequest { id }
4546 }
4647 }
47- """
48+ """ )
4849 case . repo( let owner, let name) :
4950 let key = " \( owner) \( name) " . graphQLSafeKey
50- return """
51+ return ( key , """
5152 \( key) : repository(owner: " \( owner) " , name: " \( name) " ) { id }
52- """
53+ """ )
5354 }
5455 }
55- let query = " query{ \( subQueries. joined ( separator: " " ) ) } "
56+ let query = " query{ \( subQueries. map ( { $0 . query } ) . joined ( separator: " " ) ) } "
5657 client. send ( ManualGraphQLRequest ( query: query) ) { result in
5758 switch result {
5859 case . success( let json) :
59- let ids = bookmarkGraphQLIDs ( from: json. data)
60+ let ids = bookmarkGraphQLIDs (
61+ from: json. data,
62+ originalKeys: subQueries. map ( { $0. key } )
63+ )
6064 let delta = bookmarks. count - ids. count
6165 if delta == 0 {
6266 completion ( . success( ids) )
0 commit comments