type-graphql: Error: Subscription field must return Async Iterable. Received: undefined.

Describe the Bug Error after subscription client.

To Reproduce

/// server
import { ApolloServer } from 'apollo-server-express';
import express from 'express';
import { useServer } from 'graphql-ws/lib/use/ws';
import { createServer } from 'http';
import { WebSocketServer } from 'ws';
/// ...

  const app = express();
  const httpServer = createServer(app);
  const wsServer = new WebSocketServer({
    server: httpServer,
    path: '/',
  })

  const schema = buildFederatedSchema({ orphanedTypes, resolvers, pubSub })
  const serverCleanup = useServer({ schema }, wsServer);
  const server = new ApolloServer({ schema });
  
  await server.start();
  server.applyMiddleware({ app, path: '/' });
  httpServer.listen({ port }, ... );


/// resolver
import { withFilter } from 'graphql-subscriptions';
import {
  Resolver,
  Root,
  Args,
  Subscription,
} from 'type-graphql';
/// ...

@Resolver()
export class SubscriptionResolver {
  @Subscription(() => Notification, {
    subscribe: withFilter(
      () => pubSub.asyncIterator(topics),
      (root, args) => {
        return args.type === root.type;
      },
    ),

    // subscribe() {
    //   return pubSub.asyncIterator(topics);
    // },

    // topics,
    // topics: ({ args }) => args.type,
    
    // filter: ({ args, payload }) => {
    //   return args.type === payload.type;
    // },
  })
  notifications(@Root() payload: NotificationPayload, @Args() args: NotificationArgs) {
    return {
      type: args.typeNotify,
      date: payload.date,
      meta: payload?.meta,
    };
  }

Expected Behavior After execution of subscription operation on Studio ApolloGraphql sandbox, i expect to connect to the socket, and receive messages on the emission of events. But get the error subscribe

Logs

...
🚀 Query endpoint ready at http://localhost:3008/
🚀 Subscriptions ready at ws://localhost:3008/
Internal error occurred during message handling. Please check your implementation. Error: Subscription field must return Async Iterable. Received: undefined.
    at createSourceEventStream (/***/node_modules/graphql/execution/subscribe.js:165:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async subscribe (/***/node_modules/graphql/execution/subscribe.js:67:26)
    at async onMessage (/***/node_modules/graphql-ws/lib/server.js:191:51)
    at async WebSocket.<anonymous> (/***/node_modules/graphql-ws/lib/use/ws.js:83:21)

Environment (please complete the following information):

  • Package version - v1.2.0-rc.1
  • OS: MacOS Monteray
  • Node - 16.17.0
  • TypeScript version - 4.7.4
  • npm - v1.2.0-rc.1

Additional Context Add any other context about the problem here.

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 2
  • Comments: 20 (9 by maintainers)

Most upvoted comments

Yep, there is a reason in version graphql package. Thank you

@carlocorradini tnx bro, i will write about the status.

Found the issue, Federation 2 requires graphql >= 16 while TypeGraphQL requires graphql < 16. You can follow TypeGraphQL v2.0 with support for a newer version of graphql in this pull request. If you really want to use Apollo Federation V2 you must have a separate package with graphql >= 16 and then import it in your main project with graphql < 16. If you don’t do something crazy, it works flawlessly. Take a look at my example here.

Please create a PR with a failing test case. I can’t guess from your snippets what’s wrong with your apollo server, federation or server configuration.