-
Notifications
You must be signed in to change notification settings - Fork 344
Apollo-link-context has no effect on apollo-link-ws #446
Comments
I have traced the execution:
Which is never called in SubscriptionClient. |
Any updates on this? |
Any updates? ------------ EDIT: For googlers, I ended up using the following: const wsLink = new WebSocketLink({
uri: `ws://localhost:3004/graphql`,
options: {
reconnect: true,
connectionParams: () => ({
authorization: Cookie.get('token'),
}),
},
}); Works just like you were setting http headers on graphql playground: |
could someone confirm that once the websocket channel has been opened (with Authorization header = token AAA), each subsequent request using the websocket link will always be identified as AAA token. Or is there a way to send a different Authorization header on each request (other than re-opening another ws channel)? I'd like to understand what's happening on a low level protocol for ws. Thank you for you reply! const wsClient = new SubscriptionClient(
graphqlEndpoint,
{
reconnect: true,
connectionParams: () => ({
headers: {
'Authorization': 'mytokenAAA',
},
}),
},
ws,
);
const link = new WebSocketLink(wsClient);
makePromise(execute(link, options)); // that's using token AAA
// how to make another query (execute) using token BBB without creating another link ? |
Any update on dynamically pulling JWT tokens for wsLink? I'm getting all kinds of errors or non-response when I try and mirror setups for httpLink. |
@hito you should be able to set using middleware of the const subscriptionMiddleware = {
applyMiddleware: function(options, next) {
// Get the current context
const context = options.getContext();
// set it on the `options` which will be passed to the websocket with Apollo
// Server it becomes: `ApolloServer({contetx: ({payload}) => (returns options)
options.authorization = context.authorization;
next()
},
};
const server = new ApolloServer({
context: ({connection, payload, req}) => {
// whatever you return here can be accessed from the middleware of the SubscriptionMiddleware with
// applyMiddleware: (options, next) => options.getContext()
return {authorization: payload.authorization};
},
});
const link = new WebSocketLink({
uri: WS_URL,
webSocketImpl: WebSocket,
options: {
reconnect: true,
}
});
link.subscriptionClient.use([subscriptionMiddleware]); |
I was trying to add context information with headers (like in example https://www.apollographql.com/docs/react/recipes/authentication.html) but in my case I have WS link instead of HTTP.
Context is not delivered to server, I checked it
frames
section in Network tab and inonOperation
:Context is equal to
{}
.Moreover, on client I get the following error when using WS:
Everything works well with HTTP though.
The text was updated successfully, but these errors were encountered: