svelte-apollo icon indicating copy to clipboard operation
svelte-apollo copied to clipboard

How to add wsLink with headers (auth:token) into the client?

Open beebase opened this issue 4 years ago • 1 comments
trafficstars

In this link I've read how to set up httpLink with headers: {authorization: Bearer "+ getToken() } https://github.com/timhall/svelte-apollo/issues/30

I would also like to add a wsLink with: connectionParams: {headers: {authorisation: Bearer + getToken() } but I can't find any example on the internet

beebase avatar Jan 19 '21 18:01 beebase

@beebase That's how I implemented (not sure if this is the best approach, though):

const wsLink = new WebSocketLink({
  uri: WS_GRAPHQL_ENDPOINT,
  options: {
    lazy: true,
    reconnect: true,
    connectionParams: async () => {
      const token = await getToken();

      return {
        headers: {
          authorization: token ? `Bearer ${token}` : '',
        },
      };
    },
  },
});

chbert avatar Mar 02 '21 13:03 chbert