awslabs / aws-mobile-appsync-sdk-ios

iOS SDK for AWS AppSync.
https://awslabs.github.io/aws-mobile-appsync-sdk-ios/
Other
262 stars 128 forks source link

Enabling retry for custom scenarios #539

Closed jellybeansoup closed 9 months ago

jellybeansoup commented 2 years ago

Is your feature request related to a problem? Please describe. Within the codebase at SEEK, we utilise Alamofire for our networking layer. As part of this solution, we also implement a custom instance of the AWSNetworkTransport protocol to pass mutation requests through this networking layer.

We recently discovered, however, that due to the way AWSAppSync determines whether a mutation should be retried—by checking the domain of the error and then using some of the cases to determine what the error is—our mutations were failing an not being retried, because Alamofire produces a custom error type, i.e. AFError.

Describe the solution you'd like A method whereby the calling app is asked what errors should constitute retrying the network call. This could be done using an AWSRetryableMutationError protocol with a simple boolean property that indicates if the request should be retried.

protocol AWSRetryableMutationError: Error {
    var shouldRetryRequest: Bool { get }
}

This could then replace the existing AWSMutationRetryAdviceHelper class with something extensible, with internal conformances for AWSAppSyncClientError and NSError to provide the existing behaviour.

extension NSError: AWSRetryableMutationError {

    var shouldRetryRequest: Bool {
        guard nsError.domain == NSURLErrorDomain else {
            return false
        }
        switch nsError.code {
        case NSURLErrorNotConnectedToInternet,
             NSURLErrorDNSLookupFailed,
             NSURLErrorCannotConnectToHost,
             NSURLErrorCannotFindHost,
             NSURLErrorTimedOut:
            return true
        default:
            break
        }
        return false
    }

}

Describe alternatives you've considered The potentially-obvious (but crude) resolution would be to check for the appropriate AFError cases within AWSMutationRetryAdviceHelper, but this involves adding explicit support for a third-party library, and may have other ramifications throughout the AWSAppSync codebase.

The other resolution would be to unwrap the AFError cases inside the custom AWSNetworkTransport, extracting the system-level errors that they contain. This is an entirely workable solution—and we plan to do something like this regardless—but it doesn't allow for the possibility of reattempting for reasons other than the pre-prescribed options available.

Additional context I'm more than happy to put together a PR for this if desired, but since it involves a (very minor) architecture change, I didn't want to make any assumptions with regard to why the existing implementation is unable to be extended.

atierian commented 9 months ago

Thank you for opening this issue. AWS AppSync SDK for iOS entered maintenance mode in September 2023 and will receive no further updates as of September 2024.

Please use Amplify Swift going forward. For information on upgrading to Amplify Swift, refer to the Upgrade from AppSync SDK documentation.