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.

Subscriptions onConnect return value is not appended to context

See original GitHub issue

According to the docs for subscriptions in apollo-server-express 2.0, the return value of onConnect is appended to the context. However, in practice the return value of onConnect is ignored and only the context returned by the context method of ApolloServer config is available in resolvers.

For example, with this code:

const server = new ApolloServer({
    // These will be defined for both new or existing servers
    typeDefs,
    resolvers,
    context: async ({ req, connection }) => {
        if (connection) {
            // check connection for metadata
            return {
                foo: 'bar'
            };
        } else {
            // check from req
            const token = req.headers.authorization || "";

            return { token };
        }
    },
    subscriptions: {
        onConnect: (connectionParams, webSocket) => {
            console.log('Websocket CONNECTED');
            return {
                hello: 'world'
            }
        },
        onDisconnect: () => console.log('Websocket CONNECTED'),
    }
});

You would expect the context for websockets to be: {foo: 'bar', hello: 'world'} But in practice the context will be: {foo: 'bar'}

I have created a basic reproduction of the issue here.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

30reactions
sabith-thcommented, Sep 21, 2018

I was able to get hello object in the context if I manually add it to the context via connection.context. Like:

context: async ({ req, connection }) => {
        if (connection) {
            // check connection for metadata
            return {
                foo: 'bar',
                hello: connection.context.hello,  
            };
        } else {
            // check from req
            const token = req.headers.authorization || "";

            return { token };
        }
    },
    subscriptions: {
        onConnect: (connectionParams, webSocket) => {
            console.log('Websocket CONNECTED');
            return {
                hello: 'world'
            }
        },
        onDisconnect: () => console.log('Websocket CONNECTED'),
    }

Not sure if this is the correct approach though.

15reactions
Ponjimoncommented, Sep 2, 2019

What’s the current status on this? This issue is open for over a year now and it’s still not working

Read more comments on GitHub >

github_iconTop Results From Across the Web

Subscription onConnect return value not appended to context
In subscription I am returning an object that contains currentUser property. This is how I am creating the server: const server = new ......
Read more >
Subscriptions in Apollo Server - Apollo GraphQL Docs
context function returns an object, contextValue , which is available to your resolvers. Note that the context option is called once per subscription...
Read more >
Subscriptions - Hot Chocolate v10 - ChilliCream
The subscription type is almost implemented like a simple query. ... The setup to get subscriptions running did not change.
Read more >
Use hubs in ASP.NET Core SignalR - Microsoft Learn
Configure SignalR hubs; Create and use hubs; The Context object ... Strongly-typed hubs can also return values from interface methods:.
Read more >
subscriptions-transport-ws | Yarn - Package Manager
The subscriptions-transport-ws package is no longer maintained. We recommend you use graphql-ws instead. For help migrating Apollo software to graphql-ws ...
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