From ce6e36c3e176feeca18679a0af30be1ef3f32899 Mon Sep 17 00:00:00 2001 From: dennemark Date: Mon, 9 Dec 2024 10:46:05 +0100 Subject: [PATCH] fix: :bug: add custom headers to subscription in apollo client (#11744) Co-authored-by: Tobbe Lundberg --- .changesets/11744.md | 3 +++ packages/web/src/apollo/sseLink.ts | 13 ++++++------- 2 files changed, 9 insertions(+), 7 deletions(-) create mode 100644 .changesets/11744.md diff --git a/.changesets/11744.md b/.changesets/11744.md new file mode 100644 index 000000000000..e2c1841ffef9 --- /dev/null +++ b/.changesets/11744.md @@ -0,0 +1,3 @@ +- fix: :bug: add custom headers to subscription in apollo client (#11744) by @dennemark + +Allow passing custom headers to `sseLink`, which is used by the Apollo client for subscriptions. diff --git a/packages/web/src/apollo/sseLink.ts b/packages/web/src/apollo/sseLink.ts index 62e091b928a9..d0162f5a4d94 100644 --- a/packages/web/src/apollo/sseLink.ts +++ b/packages/web/src/apollo/sseLink.ts @@ -95,7 +95,7 @@ class SSELink extends ApolloLink { super() const { url, auth, headers, httpLinkConfig } = options - const { credentials, referrer, referrerPolicy } = + const { credentials, referrer, referrerPolicy, ...customHeaders } = httpLinkConfig?.headers || {} this.client = createClient({ @@ -103,14 +103,13 @@ class SSELink extends ApolloLink { headers: async () => { const token = await auth.tokenFn() - // Only add auth headers when there's a token. `token` is `null` when `!isAuthenticated`. - if (!token) { - return { ...headers } - } + // Only add auth headers when there's a token. `token` is `null` when + // `!isAuthenticated`. return { - Authorization: `Bearer ${token}`, - 'auth-provider': auth.authProviderType, + ...(token && { Authorization: `Bearer ${token}` }), + ...(token && { 'auth-provider': auth.authProviderType }), ...headers, + ...customHeaders, } }, credentials: mapCredentialsHeader(credentials),