Skip to content

Commit

Permalink
fix(datastore): decode optimistically with paginated list response type
Browse files Browse the repository at this point in the history
  • Loading branch information
5d committed Jan 17, 2024
1 parent 3d142c1 commit 666d35d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Amplify/Categories/API/Response/GraphQLError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ extension GraphQLError {
public let column: Int
}
}

extension GraphQLError: Error { }
25 changes: 25 additions & 0 deletions AmplifyPlugins/Core/AWSPluginsCore/Sync/PaginatedList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,29 @@ public struct PaginatedList<ModelType: Model>: Decodable {
public let items: [MutationSync<ModelType>]
public let nextToken: String?
public let startedAt: Int64?

enum CodingKeys: CodingKey {
case items
case nextToken
case startedAt
}

public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
let optimisticDecodedResults = try values.decode([OptimisticDecoded<MutationSync<ModelType>>].self, forKey: .items)
items = optimisticDecodedResults.compactMap { try? $0.result.get() }
nextToken = try values.decode(String?.self, forKey: .nextToken)
startedAt = try values.decode(Int64?.self, forKey: .startedAt)
}
}


fileprivate struct OptimisticDecoded<T: Decodable>: Decodable {
let result: Swift.Result<T, Error>

init(from decoder: Decoder) throws {
result = Result(catching: {
try T(from: decoder)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,17 @@ final class InitialSyncOperation: AsynchronousOperation {

let syncQueryResult: SyncQueryResult
switch graphQLResult {
case .success(let queryResult):
syncQueryResult = queryResult

case .failure(.partial(let queryResult, let errors)):
syncQueryResult = queryResult
errors.map { DataStoreError.api(APIError(errorDescription: $0.message, error: $0)) }
.forEach { dataStoreConfiguration.errorHandler($0) }

case .failure(let graphQLResponseError):
finish(result: .failure(DataStoreError.api(graphQLResponseError)))
return
case .success(let queryResult):
syncQueryResult = queryResult
}

let items = syncQueryResult.items
Expand Down

0 comments on commit 666d35d

Please sign in to comment.