aws-amplify / amplify-swift

A declarative library for application development using cloud services.
Apache License 2.0
456 stars 197 forks source link

AppSync realtime endpoint URL is not correct for AWS China regions #3915

Open wulianer opened 2 days ago

wulianer commented 2 days ago

Describe the bug

Working on iOS projects with AWS China service region. The AppSync Query/Mutation methods work well but no subscription method works. Looking into the network traffic detail, I found that the Amplify SDK mistakenly processes the realtime endpoint URL. Below is the block of source code that needs update:

AmplifyPlugins/API/Sources/AWSAPIPlugin/SubscriptionFactory/AppSyncRealTimeClientFactory.swift

All AWS China AppSync endpoints end with suffix amazonaws.com.cn, not amazonaws.com. Due to the code below, this will cause all AWS China AppSync real time endpoint to have one extra /realtime in the end. The following replacing name logic will also not run.

extension AppSyncRealTimeClientFactory {

    /**
     Converting appsync api url to realtime api url
     1. api.example.com/graphql -> api.example.com/graphql/realtime
     2. abc.appsync-api.us-east-1.amazonaws.com/graphql -> abc.appsync-realtime-api.us-east-1.amazonaws.com/graphql
     */
    static func appSyncRealTimeEndpoint(_ url: URL) -> URL {
        guard let host = url.host else {
            return url
        }

        guard host.hasSuffix("amazonaws.com") else {
            return url.appendingPathComponent("realtime")
        }

        guard var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
            return url
        }

        urlComponents.host = host.replacingOccurrences(of: "appsync-api", with: "appsync-realtime-api")
        guard let realTimeUrl = urlComponents.url else {
            return url
        }

        return realTimeUrl
    }

Steps To Reproduce

As described in the description area.

Expected behavior

User can define the realtime endpoint in the amplifyconfig.json. Why not directly load it from the json instead of using such complex processing logic?

Amplify Framework Version

2.44.0

Amplify Categories

API

Dependency manager

Swift PM

Swift version

5.8

CLI version

12.1.1

Xcode version

15.4

Relevant log output

No response

Is this a regression?

No

Regression additional context

No response

Platforms

iOS

OS Version

iOS 18.1

Device

iPad, iPhone

Specific to simulators

No response

Additional context

No response

mattcreaser commented 1 day ago

Thanks for filing this issue @wulianer. We will check in with the AppSync team and get back to you soon.