-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Pass in first parameter of onOperation to the subscriptionServer context function #1505
Comments
I checked the source code and saw that this feature is resolved, so the token does not reach the context??? |
@ysantalla I'm not sure exactly what you're asking but this has definitely been merged. I'm using it in my app like so: // client
const wsLink = new WebSocketLink({
uri: websocketUrl,
options: {
reconnect: true,
},
webSocketImpl: ws,
})
const subscriptionMiddleware = {
applyMiddleware: async (options, next) => {
options.authToken = await getLoginToken()
next()
},
}
// add the middleware to the web socket link via the Subscription Transport client
wsLink.subscriptionClient.use([subscriptionMiddleware])
// server
const server = new ApolloServer({
schema,
context: async ({req, payload}) => {
const token = payload
? payload.authToken
: getTokenFromRequest({request: req})
return await setupContext({token})
},
subscriptions: {
keepAlive: 30000,
path: WEBSOCKET_PATH,
},
}) |
Thanks, I was missing the payload parameter in the function |
@clayne11 Thanks for showing this approach, but I couldn't figure out
is this just an internal function which adds token to the context? if yes, could you please elaborate? I didnt find anything in the documentation, except for setContext. Right now am doing in such a way
is it the proper way? |
|
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! here is my code so far (working correctly with one token): 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 ? |
By this, you can create multiple websocket links, one per one client. const wsLink = new ApolloLink(operation => {
// This is your context!
const context = operation.getContext().graphqlContext
// Create a new websocket link per request
return new WebSocketLink({
uri: "<YOUR_URI>",
options: {
reconnect: true,
connectionParams: { // give custom params to your websocket backend (e.g. to handles auth)
headers: {
userId: context.user.id
foo: 'bar'
}
},
},
webSocketImpl: ws,
}).request(operation)
// Instead of using `forward()` of Apollo link, we directly use websocketLink's request method
}) |
Server is receiving one token from multiple clients. |
There have been some pretty longstanding issues about how to do authentication for subscriptions (apollographql/apollo-link#197 (comment)).
If we pass the first param of
onOperation
in the subscription server then we can dynamically access any parameters we want that are passed by theSubscriptionClient
.The setup we use looks like this:
The
options
from the client are passed as themessage.payload
on the server. As far as I can tell this is the easiest way to perform auth on the subscriptions. If we simply pass thispayload
in to thecontext
function it would allow this pattern to work withapollo-server@2.0.0
.The current code in
apollo-server-core
looks like this:If we change it to something like this:
it should solve a problem for a lot of people.
The text was updated successfully, but these errors were encountered: