Apollo Docs on Subscribing from Meteor Need Update?


#1

The Apollo docs say to use:

import { SubscriptionManager } from 'graphql-subscriptions';

…and provide the following code for getting subscriptions running on the server:

import { SubscriptionManager } from 'graphql-subscriptions';
import { SubscriptionServer } from 'subscriptions-transport-ws';
import { createApolloServer, addCurrentUserToContext } from 'meteor/apollo';
 
// your executable schema
const schema = ...
// any additional context you use for your resolvers, if any
const context = {};
 
// the pubsub mechanism of your choice, for instance:
// - PubSub from graphql-subscriptions (not recommended for production)
// - RedisPubSub from graphql-redis-subscriptions
// - MQTTPubSub from graphql-mqtt-subscriptions
const pubsub = new PubSub();
 
// subscriptions path which fits witht the one you connect to on the client
const subscriptionsPath = '/subscriptions';
 
// start a graphql server with Express handling a possible Meteor current user
createApolloServer({ 
  schema,
  context 
});
 
// create the subscription manager thanks to the schema & the pubsub mechanism
const subscriptionManager = new SubscriptionManager({
  schema,
  pubsub,
});
 
// start up a subscription server
new SubscriptionServer(
  {
    subscriptionManager,
    // on connect subscription lifecycle event
    onConnect: async (connectionParams, webSocket) => {
      // if a meteor login token is passed to the connection params from the client, 
      // add the current user to the subscription context
      const subscriptionContext = connectionParams.meteorLoginToken
        ? await addCurrentUserToContext(context, connectionParams.meteorLoginToken)
        : context;
      return subscriptionContext;
    },
  },
  {
    // bind the subscription server to Meteor WebApp
    server: WebApp.httpServer,
    path: subscriptionsPath,
  }
);

When running this code, the console log shows:

subscriptionManager is deprecated, use execute or subscribe directly from graphql-js!

Is there sample code showing how to use execute or subscribe directly from graphql-js?

A link would be greatly appreciated!


#2

In looking into this, I have found out a couple more items:

  • In order to use SubscriptionManager with subscriptions, it is necessary to fill in the SetupFunctions field.
  • The Apollo docs for Meteor do not mention this.
  • Therefore if you go by the Apollo docs for Meteor, your subscriptions will fail.

Come on MDG – give your Meteor users some Apollo love! Either:

  • Give us sample code that actually works, for setting up Apollo pubsub in Meteor.
  • …or, at least give us links to read up on so we don’t waste 10-20 hours like I just did trying to get the code in the Apollo docs for Meteor to work.

Here’s what not to do:

  • Provide sample code in the official Apollo docs for Meteor that does not work.

#3

+1 on this. The docs are outdated and SubscriptionManager doesn’t even exist anymore. I’m glad everyone is loving React, but don’t forget about Meteor just because it’s the new shiny thing.


#4

Update on this. I recently figured out how to use Apollo 2.0 with complete subscription support. To save other people from pulling their hair out while they wait for the official docs to catch up, I made a quick write-up with an example Meteor app.

https://medium.com/@michaelcbrook/how-to-get-apollo-2-0-working-with-graphql-subscriptions-321388be030c

GitHub example:


#5

Thank you very much for posting this!


#6

Great article! Nice work and thank you.