Skip to content

Adding custom wsHeaders to SubscriptionClient #568

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/guide/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ createApolloClient({
ssr: false,
// Only use Websocket for all requests (including queries and mutations)
websocketsOnly: false,
// Additional headers to pass to the websocket connection
wsHeaders = {},
// Custom starting link.
// If you want to replace the default HttpLink, set `defaultHttpLink` to false
link: null,
Expand Down
10 changes: 6 additions & 4 deletions graphql-client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { setContext } from 'apollo-link-context'
import { withClientState } from 'apollo-link-state'

// Create the apollo client
export function createApolloClient ({
export function createApolloClient({
// Client ID if using multiple Clients
clientId = 'defaultClient',
// URL to the HTTP API
Expand All @@ -26,6 +26,8 @@ export function createApolloClient ({
ssr = false,
// Only use Websocket for all requests (including queries and mutations)
websocketsOnly = false,
// Additional headers to pass to the websocket connection
wsHeaders = {},
// Custom starting link.
// If you want to replace the default HttpLink, set `defaultHttpLink` to false
link = null,
Expand Down Expand Up @@ -123,7 +125,7 @@ export function createApolloClient ({
reconnect: true,
connectionParams: () => {
const Authorization = getAuth(tokenName)
return Authorization ? { Authorization, headers: { Authorization } } : {}
return Authorization ? { Authorization, headers: { ...wsHeaders, Authorization } } : wsHeaders
},
})

Expand Down Expand Up @@ -191,7 +193,7 @@ export function createApolloClient ({
}
}

export function restartWebsockets (wsClient) {
export function restartWebsockets(wsClient) {
// Copy current operations
const operations = Object.assign({}, wsClient.operations)

Expand All @@ -211,7 +213,7 @@ export function restartWebsockets (wsClient) {
})
}

function defaultGetAuth (tokenName) {
function defaultGetAuth(tokenName) {
if (typeof window !== 'undefined') {
// get the authentication token from local storage if it exists
const token = window.localStorage.getItem(tokenName)
Expand Down