How to pass AWS AppSync custom request header in iOS client?

三世轮回 提交于 2019-12-13 04:58:09

问题


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.



来源:https://stackoverflow.com/questions/51932849/how-to-pass-aws-appsync-custom-request-header-in-ios-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!