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 - context not extended

See original GitHub issue

Hey,

we have upgraded to apollo server 2 and now have trouble with the GraphQL context in the subscription resolvers. We have an ApolloServer and will extend the GraphQL context with some data the client sent during the first connect. In the docs is an example.

We’ve implemeted the onConnect and return some data:

...
subscriptions: {
  onConnect: (connectionParams: ConnectionParams) => {
    // do sth. with the connection params
    return { some: 'data' };
  }
}
...

The documentation says the return value of the onConnect function extends the GraphQL context. The onConnect gets executed when a new client connects.

In our resolver we want to do some filtering based on the extended context:

...
subscribe: withFilter(
  () => this.pubSub.subscribe('xxx'),
  (payload, _, ctx: IGraphQLContext) => {
    // do sth. with `ctx.some`
    return true;
  },
),

But the context in the resolver is always empty.

We are on the latest version of apollo-server-express and subscriptions-transport-ws.

Thank you very much for your help!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

26reactions
marcel-devdudecommented, Oct 12, 2018

The return value of the onConnect callback is available in the context function. So you can return it if you build the context for subscriptions.

...
subscriptions: {
  onConnect: async (connectionParams) => {
    // Do some stuff with connectionParams like auth
    
    return { extended: 'context' };
  },
},
context: async ({ req, connection }) => {
  // If we build the context for subscriptions, return the context generated in the onConnect callback.
  // In this example `connection.context` is `{ extended: 'context' }`
  if (!req || !req.headers) {
    return connection.context;
  }

  // Build context for normal requests
  return { regular: 'context' };
},
...
1reaction
glassercommented, Mar 11, 2022

Apollo Server v3 does not have built-in support for subscriptions (we felt that the superficial integration with the unmaintained subscriptions-transport-ws package was not up to our standards). https://www.apollographql.com/docs/apollo-server/data/subscriptions/ documents how to integrate Apollo Server with the newer graphql-ws package, including a whole section on the context value. There’s a link at the bottom to the previous version of the docs which explains how to integrate AS with the unmaintained subscriptions-transport-ws package too if you need to.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Subscriptions - Apollo GraphQL Docs
Subscriptions are not currently supported in Apollo Federation. ... The GraphQL context can also be extended with the authenticated user data to enable...
Read more >
GraphQL Subscription fields not able to access context of ...
I don't get an error if I use the subscription without the postedBy field. I can only assume that the undefined object is...
Read more >
Subscription Classes - GraphQL Ruby
You can extend GraphQL::Schema::Subscription to create fields that can be subscribed to. These classes support several behaviors:.
Read more >
How GraphQL Subscriptions Work: Tips, Best Practices and ...
This will open up a long-lived connection to the server by sending the subscription query specifying which event it is interested in.
Read more >
Context | XState Docs
In XState, extended state is known as context. ... i.e., the context will not be created until the machine is actually created/used:.
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