awslabs / aws-mobile-appsync-sdk-js

JavaScript library files for Offline, Sync, Sigv4. includes support for React Native
Apache License 2.0
916 stars 265 forks source link

Access request headers in IAM-authorized subscription endpoints #721

Open ghost opened 2 years ago

ghost commented 2 years ago

Do you want to request a feature or report a bug?

Feature

What is the current behavior?

I have an interesting use-case where I need to pass along additional request headers to a subscription endpoint to trigger some logic in the corresponding request resolver. My subscription uses IAM auth, which results in the following payload:

{
  id: "..."
  payload: {…}
  data: "..."
  extensions: {
    authorization: {
      accept: "application/json, text/javascript", 
      content-encoding: "amz-1.0"
      ...all my custom headers end up here...
    }
  }
  type: "start"
}

While my custom headers do get sent, they appear inaccessible once the resolver context is built and supplied to my resolver template. Instead, request headers are only available from the SigV4 headers sent in with the query string ?header="..."

Modifying AppSyncRealTimeSubscriptionHandshakeLink._awsRealTimeIAMHeader to include custom headers would resolve this problem, iff this is a reasonable use-case and I'm not missing some other avenue for accessing my headers.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem.

What is the expected behavior?

Which versions and which environment (browser, react-native, nodejs) / OS are affected by this issue? Did this work in previous versions?

ghost commented 2 years ago

I'd also be willing to open a pull request for this change.

sis-dk commented 1 year ago

Would this help?

import { setContext } from '@apollo/client/link/context'
const link = ApolloLink.from([
    setContext((request, previousContext) => {
      return {
        headers: {
          ...previousContext.headers,
          //Your own headers here
        }
      }
    }),
    //Other links here
])