Unable to get context for subscriptions
See original GitHub issueI have the following code:
const server = new ApolloServer({
........
subscriptions: {
onConnect: (connectionParams, webSocket, context) => {
// check for headers
let username = <username from connectionParams->headers>
return { username } // according to the documentation, returning here adds the parameter to context, can't seem to get this in subscription function
},
},
context: ({ req, res }) => {
if (req, req.headers){
// non-subscription calls
return <username from connectionParams->headers>
}else{
// we get here when we use subscription query
// req and res are empty
return;
}
},
........
)}
And then in the subscription resolver
........
Subscription: {
somethingChanged: {
// Additional event labels can be passed to asyncIterator creation
subscribe: withFilter(() => pubsub.asyncIterator('SOMETHING_CHANGED'), (payload, variables, context, info) => {
console.log(payload, variables, context, info)
return true
}),
},
},
........
I cannot seem to send the username from onConnect to the context within withFilter. I am trying to achieve this because once I have the context (i.e. headers/auth), I want to compare it to the payload.
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
GraphQL Subscription fields not able to access context of ...
I get the error Cannot read property 'collection' of undefined. I don't get an error if I use the subscription without the postedBy...
Read more >Subscriptions in Apollo Server - Apollo GraphQL Docs
Subscriptions are long-lasting GraphQL read operations that can update their result whenever a particular server-side event occurs. Most commonly, updated ...
Read more >No subscriptions found sign in error for Azure portal
Symptom. When you try to sign in to the Azure portal, you receive the following error message: "No subscriptions found". · Cause. This...
Read more >Real-time data - AWS AppSync
GraphQL schema subscription directives. Subscriptions in AWS AppSync are invoked as a response to a mutation. This means that you can make any...
Read more >Subscriptions - Ariadne GraphQL
This can have implications for application performance. It may be preferable to cache data on websocket.scope instead of info.context to avoid repeated database ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Solved by https://github.com/apollographql/apollo-server/issues/1597#issuecomment-423641175
@judithkoh