Skip to content

Commit

Permalink
fix: 🐛 add custom headers to subscription in apollo client (#11744)
Browse files Browse the repository at this point in the history
Co-authored-by: Tobbe Lundberg <tobbe@tlundberg.com>
  • Loading branch information
dennemark and Tobbe authored Dec 9, 2024
1 parent 455bae8 commit ce6e36c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .changesets/11744.md
Original file line number Diff line number Diff line change
@@ -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.
13 changes: 6 additions & 7 deletions packages/web/src/apollo/sseLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,21 @@ 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({
url,
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),
Expand Down

0 comments on commit ce6e36c

Please sign in to comment.