How to pass AWS AppSync custom request header in i

2019-08-19 02:51发布

问题:

AWS AppSync supports passing custom headers from clients and accessing them in your GraphQL resolvers using $context.request.headers.
I wonder how can I do that in iOS client ?
Thanks :)
https://docs.aws.amazon.com/appsync/latest/devguide/resolver-context-reference.html

回答1:

I've just found a way to pass additional AWS AppSync Request Header in iOS Client :)
Here is a sample class of AppSyncManager

final class AppSyncManager {

    static func instance() -> AWSAppSyncClient {
        let tmpURL = URL(fileURLWithPath: NSTemporaryDirectory())
        let databaseURL = tmpURL.appendingPathComponent(databasName)
        let urlSessionConfiguration = URLSessionConfiguration.default
        // Our request header => In resolve mapping: $context.request.headers.author
        urlSessionConfiguration.httpAdditionalHeaders = ["author": CognitoUserPoolManager.instance.author]
        let appSyncConfig = try! AWSAppSyncClientConfiguration(url: endPointURL,
                                                               serviceRegion: region,
                                                               userPoolsAuthProvider: CognitoAuthProvider(),
                                                               urlSessionConfiguration: urlSessionConfiguration,
                                                               databaseURL: databaseURL)
        let appSyncClient = try! AWSAppSyncClient(appSyncConfig: appSyncConfig)
        appSyncClient.apolloClient?.cacheKeyForObject = { $0["id"] }
        return appSyncClient
    }

}


回答2:

In the latest aws-mobile-appsync-sdk-ios SDK 2.6.22, Issue is being fixed.