Skip to content

Gen2 Data>> Add custom headers. docs are a bit wrong #3954

Open
@bogris

Description

@bogris

Describe the bug

in the amplify docs for adding custom headers, the order of operations is wrong:

https://docs.amplify.aws/swift/build-a-backend/data/connect-to-API/

in the current config, the code fails as the defaultGqlAPI is not initialized.

try apiPlugin.add(interceptor: CustomInterceptor(), for: AWSAPIPlugin.defaultGraphQLAPI)

should go after we call the .configure. in that set-up code works as expected.

here is my working version:


import Amplify
import AWSAPIPlugin
import Authenticator
import AWSCognitoAuthPlugin
import AWSS3StoragePlugin
import SwiftUI


struct CustomInterceptor: URLRequestInterceptor {
    func intercept(_ request: URLRequest) throws -> URLRequest {
        var request = request
        request.setValue("my-value", forHTTPHeaderField: "custom-ios")
        return request
    }
}

@main
struct moonshot_iosApp: App {
    
    init() {
        let awsApiPlugin = AWSAPIPlugin(modelRegistration: AmplifyModels())

        do {
            try Amplify.add(plugin: awsApiPlugin)
            try Amplify.add(plugin: AWSCognitoAuthPlugin())
            try Amplify.add(plugin: AWSS3StoragePlugin())
            try Amplify.configure(with: .amplifyOutputs)
            try awsApiPlugin.add(interceptor: CustomInterceptor(), for: AWSAPIPlugin.defaultGraphQLAPI)
            print("Initialized Amplify")
        } catch {
            // simplified error handling for the tutorial
            print("Could not initialize Amplify: \(error)")
        }
    }
    var body: some Scene {

...

Steps To Reproduce

used code in the example. 
code throws: can not unpack undefined (regarding endpoints that are not configured yet)

Expected behavior

not throw

Amplify Framework Version

gen2

Amplify Categories

API

Dependency manager

Swift PM

Swift version

idk

CLI version

11.0.1

Xcode version

16.2

Relevant log output

<details>
<summary>Log Messages</summary>


INSERT LOG MESSAGES HERE

</details>

Is this a regression?

No

Regression additional context

No response

Platforms

No response

OS Version

macOs 15.2

Device

Ipad for macos env

Specific to simulators

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    apiIssues related to the API categorybugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions