Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bogris opened this issue Jan 27, 2025 · 1 comment
Open

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

bogris opened this issue Jan 27, 2025 · 1 comment
Labels
api Issues related to the API category bug Something isn't working

Comments

@bogris
Copy link

bogris commented Jan 27, 2025

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

@github-actions github-actions bot added pending-triage Issue is pending triage pending-maintainer-response Issue is pending response from an Amplify team member labels Jan 27, 2025
@harsh62 harsh62 added api Issues related to the API category and removed pending-triage Issue is pending triage labels Jan 27, 2025
@harsh62
Copy link
Member

harsh62 commented Jan 27, 2025

@bogris Thank you for raising the issue with our team. The team will look into the Amplify implementation first to make sure its not a bug that is causing the issue when adding the interceptor before configure.

Accordingly we will update the documentation.

@github-actions github-actions bot removed the pending-maintainer-response Issue is pending response from an Amplify team member label Jan 27, 2025
@harsh62 harsh62 added the bug Something isn't working label Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Issues related to the API category bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants