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

Patch: Initializing Subscription WebSocket doesn't inherit custom headers #770

Open
vmcruz opened this issue Feb 6, 2025 · 0 comments
Open

Comments

@vmcruz
Copy link

vmcruz commented Feb 6, 2025

Hi!

I've used patch-package to patch [email protected] for the project I'm working on.

I found that the graphql_headers were not being passed down during the realtime handshake initialization. I needed a custom header in the Auth Lambda but I wasn't receiving it in the requestHeaders from the event object. After debugging, I found this line where the _initializeWebSocketConnection does the handshake:

Here is the diff that solved my problem:

diff --git a/node_modules/aws-appsync-subscription-link/lib/realtime-subscription-handshake-link.js b/node_modules/aws-appsync-subscription-link/lib/realtime-subscription-handshake-link.js
index 612efd0..c8c669d 100644
--- a/node_modules/aws-appsync-subscription-link/lib/realtime-subscription-handshake-link.js
+++ b/node_modules/aws-appsync-subscription-link/lib/realtime-subscription-handshake-link.js
@@ -318,7 +318,8 @@ var AppSyncRealTimeSubscriptionHandshakeLink = /** @class */ (function (_super)
                                 authenticationType: authenticationType,
                                 region: region,
                                 credentials: credentials,
-                                token: token
+                                token: token,
+                                graphql_headers: graphql_headers
                             })];
                     case 3:
                         _g.sent();
@@ -362,7 +363,7 @@ var AppSyncRealTimeSubscriptionHandshakeLink = /** @class */ (function (_super)
     };
     AppSyncRealTimeSubscriptionHandshakeLink.prototype._initializeWebSocketConnection = function (_a) {
         var _this = this;
-        var appSyncGraphqlEndpoint = _a.appSyncGraphqlEndpoint, authenticationType = _a.authenticationType, apiKey = _a.apiKey, region = _a.region, credentials = _a.credentials, token = _a.token;
+        var appSyncGraphqlEndpoint = _a.appSyncGraphqlEndpoint, authenticationType = _a.authenticationType, apiKey = _a.apiKey, region = _a.region, credentials = _a.credentials, token = _a.token, graphql_headers = _a.graphql_headers;
         if (this.socketStatus === types_1.SOCKET_STATUS.READY) {
             return;
         }
@@ -388,7 +389,7 @@ var AppSyncRealTimeSubscriptionHandshakeLink = /** @class */ (function (_super)
                                 region: region,
                                 credentials: credentials,
                                 token: token,
-                                graphql_headers: function () { }
+                                graphql_headers: graphql_headers
                             })];
                     case 2:
                         headerString = _b.apply(_a, [_c.sent()]);

Now it works using setContext from Apollo:

  const customHeadersMiddleware = setContext((_, { headers }) => {
    return {
      headers: {
        ...headers,
        'x-custom-header': 'my-value',
      },
    };
  });

  const subscriptionLink = ApolloLink.from([
    customHeadersMiddleware,
    createAuthLink(subscriptionLinkParams),
    createSubscriptionHandshakeLink(subscriptionLinkParams),
  ]);

This should solve #630 #761 and maybe #585

This issue body was partially generated by patch-package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant