Subscriptions onConnect return value is not appended to context
See original GitHub issueAccording 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:
- Created 5 years ago
- Reactions:4
- Comments:9 (4 by maintainers)
Top 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 >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
I was able to get
hello
object in the context if I manually add it to the context viaconnection.context
. Like:Not sure if this is the correct approach though.
What’s the current status on this? This issue is open for over a year now and it’s still not working