Search code examples
iosswiftamazon-web-servicesgraphqlaws-appsync

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


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


Solution

  • 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
        }
    
    }