Skip to content

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

@vmcruz

Description

@vmcruz

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions