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 17, 2023
1 parent 7b507f3 commit 9361448
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,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 @@ -27,20 +27,23 @@ class ProcessMutationErrorFromCloudOperation: AsynchronousOperation {
private let completion: (Result<MutationEvent?, Error>) -> Void
private var mutationOperation: AtomicValue<GraphQLOperation<MutationSync<AnyModel>>?>
private weak var api: APICategoryGraphQLBehaviorExtended?

private weak var reconciliationQueue: IncomingEventReconciliationQueue?

init(dataStoreConfiguration: DataStoreConfiguration,
mutationEvent: MutationEvent,
api: APICategoryGraphQLBehaviorExtended,
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 @@ -311,12 +314,27 @@ class ProcessMutationErrorFromCloudOperation: AsynchronousOperation {
if case .failure(let error) = cloudResult {
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 @@ -656,6 +656,7 @@ class ProcessMutationErrorFromCloudOperationTests: XCTestCase {
api: mockAPIPlugin,
storageAdapter: storageAdapter,
graphQLResponseError: graphQLResponseError,
reconciliationQueue: MockReconciliationQueue(),
completion: completion)

queue.addOperation(operation)
Expand Down

0 comments on commit 9361448

Please sign in to comment.