graphql-modules icon indicating copy to clipboard operation
graphql-modules copied to clipboard

Apollo v3 Subscriptions

Open a-type opened this issue 4 years ago • 1 comments

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.

a-type avatar Sep 27 '21 22:09 a-type

Apologies for my misunderstanding here, we tried rolling back the inclusion of this library and found similar problems existed. I do think some more documentation on usage with subscriptions and modern Apollo would be nice, but the problem itself seems to lie elsewhere. Feel free to close.

a-type avatar Sep 28 '21 14:09 a-type