question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to get context for subscriptions

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found