Skip to content

Apollo v3 Subscriptions #1875

@a-type

Description

@a-type

Describe the bug

Since incorporating graphql-modules I've not been able to get a subscription socket to connect and stay connected. The socket sends a connection_init message and then dies.

I've not been able to find any examples or documentation on using this library with Apollo's latest subscription recommendations, which is to create a SubscriptionServer directly.

To Reproduce

Here's how I'm setting up the server. I'll try to find some time to create a sandbox for reproduction.

  // applyDirectives visits directives in the schema and implements them
  const schema = applyDirectives(schemaApplication.createSchemaForApollo());
  const subscribe = schemaApplication.createSubscription();
  const execute = schemaApplication.createExecution();

  const app = express();

  const httpServer = createServer(app);

  const server = new ApolloServer({
    schema,
    context: createContext,
    // allow introspection in non-prod environments
    introspection: APP_ENV !== 'production',
    plugins: [
      {
        async serverWillStart() {
          return {
            async drainServer() {
              subscriptionServer.close();
            },
          };
        },
      },
      ApolloServerPluginDrainHttpServer({ httpServer }),
    ],
  });

  const subscriptionServer = SubscriptionServer.create(
    {
      schema,
      execute,
      subscribe,
      onConnect: (arg1: any) => {
        console.log(`Connected`, arg1);
      },
      onDisconnect: (arg1: any) => {
        console.log('Disconnected', arg1);
      },
      onOperation: (arg1: any) => {
        console.log('Operation', arg1);
      },
    },
    {
      server: httpServer,
      path: server.graphqlPath
    },
  );

  await server.start();

  server.applyMiddleware({
    app,
  });

  httpServer.listen(PORT, () => {
    console.log(
      `🚀 Server ready at http://localhost:${PORT}${server.graphqlPath}`,
    );
  });

Expected behavior

I expect the Subscription client to connect via websocket and receive an ack, then maintain the connection.

I also expect the console logs to be invoked from my SubscriptionServer (they're not)

Environment:

  • OS: MacOS 11.5.2
  • @graphql-modules/1.4.4:
  • NodeJS: 16.6.2

Additional context

The Apollo subscriptions example seems to use an API that's no longer part of their documentation in either v2 or v3.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions