Skip to content

Commit

Permalink
fix(DataStore): Reconcile mutation responses from conflict handler path
Browse files Browse the repository at this point in the history
  • Loading branch information
lawmicha committed Nov 20, 2023
1 parent e2948d7 commit ed2bda1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ final class OutgoingMutationQueue: OutgoingMutationQueueBehavior {
api: api,
storageAdapter: storageAdapter,
graphQLResponseError: graphQLResponseError,
apiError: apiError
apiError: apiError,
reconciliationQueue: reconciliationQueue
) { [weak self] result in
guard let self = self else {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,23 @@ class ProcessMutationErrorFromCloudOperation: AsynchronousOperation {
private let completion: (Result<MutationEvent?, Error>) -> Void
private var mutationOperation: AtomicValue<GraphQLOperation<MutationSync<AnyModel>>?>
private weak var api: APICategoryGraphQLBehavior?

private weak var reconciliationQueue: IncomingEventReconciliationQueue?

init(dataStoreConfiguration: DataStoreConfiguration,
mutationEvent: MutationEvent,
api: APICategoryGraphQLBehavior,
storageAdapter: StorageEngineAdapter,
graphQLResponseError: GraphQLResponseError<MutationSync<AnyModel>>? = nil,
apiError: APIError? = nil,
reconciliationQueue: IncomingEventReconciliationQueue? = nil,
completion: @escaping (Result<MutationEvent?, Error>) -> Void) {
self.dataStoreConfiguration = dataStoreConfiguration
self.mutationEvent = mutationEvent
self.api = api
self.storageAdapter = storageAdapter
self.graphQLResponseError = graphQLResponseError
self.apiError = apiError
self.reconciliationQueue = reconciliationQueue
self.completion = completion
self.mutationOperation = AtomicValue(initialValue: nil)

Expand Down Expand Up @@ -316,9 +319,24 @@ class ProcessMutationErrorFromCloudOperation: AsynchronousOperation {
dataStoreConfiguration.errorHandler(error)
}

if case .success(let response) = cloudResult,
case .failure(let error) = response {
dataStoreConfiguration.errorHandler(error)
if case let .success(graphQLResponse) = cloudResult {
if case .failure(let error) = graphQLResponse {
dataStoreConfiguration.errorHandler(error)
} else if case let .success(graphQLResult) = graphQLResponse {
guard let reconciliationQueue = reconciliationQueue else {
let dataStoreError = DataStoreError.configuration(
"reconciliationQueue is unexpectedly nil",
"""
The reference to reconciliationQueue has been released while an ongoing mutation was being processed.
\(AmplifyErrorMessages.reportBugToAWS())
"""
)
finish(result: .failure(dataStoreError))
return
}

reconciliationQueue.offer([graphQLResult], modelName: mutationEvent.modelName)
}
}

finish(result: .success(nil))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class ProcessMutationErrorFromCloudOperationTests: XCTestCase {
var storageAdapter: StorageEngineAdapter!
var localPost = Post(title: "localTitle", content: "localContent", createdAt: .now())
let queue = OperationQueue()

let reconciliationQueue = MockReconciliationQueue()

override func setUp() {
tryOrFail {
try setUpWithAPI()
Expand Down Expand Up @@ -585,6 +586,7 @@ class ProcessMutationErrorFromCloudOperationTests: XCTestCase {
api: mockAPIPlugin,
storageAdapter: storageAdapter,
graphQLResponseError: graphQLResponseError,
reconciliationQueue: reconciliationQueue,
completion: completion)

queue.addOperation(operation)
Expand Down Expand Up @@ -662,6 +664,7 @@ class ProcessMutationErrorFromCloudOperationTests: XCTestCase {
api: mockAPIPlugin,
storageAdapter: storageAdapter,
graphQLResponseError: graphQLResponseError,
reconciliationQueue: reconciliationQueue,
completion: completion)

queue.addOperation(operation)
Expand Down Expand Up @@ -956,6 +959,7 @@ class ProcessMutationErrorFromCloudOperationTests: XCTestCase {
api: mockAPIPlugin,
storageAdapter: storageAdapter,
graphQLResponseError: graphQLResponseError,
reconciliationQueue: reconciliationQueue,
completion: completion)

queue.addOperation(operation)
Expand Down Expand Up @@ -1035,6 +1039,7 @@ class ProcessMutationErrorFromCloudOperationTests: XCTestCase {
api: mockAPIPlugin,
storageAdapter: storageAdapter,
graphQLResponseError: graphQLResponseError,
reconciliationQueue: reconciliationQueue,
completion: completion)
queue.addOperation(operation)

Expand Down

0 comments on commit ed2bda1

Please sign in to comment.