apollographql / graphql-subscriptions

:newspaper: A small module that implements GraphQL subscriptions for Node.js
MIT License
1.58k stars 133 forks source link

Can't use async/await with withFilter function #244

Closed elisalimli closed 3 years ago

elisalimli commented 3 years ago

Error:

TypeError: asyncIterator.return is not a function

import { withFilter } from "graphql-subscriptions"

I'm using also type-graphql

My code

@Subscription(() => Message, {
    subscribe: withFilter(
      async (_, args: any, { connection }: any) => {
        const userId = connection.context?.req?.session?.userId;

        if (!userId) throw new Error("not auth");
        const user = await User.findOne(userId);
        return pubsub.asyncIterator(NEW_CHANNEL_MESSAGE);
      },
      async (payload: Message, variables: { channelId: number }) => {
        return variables.channelId === payload.channelId;
      }
    ),
  })
  newMessageAdded(
    @Root() root: any,
    @Arg("channelId", () => Int) channelId: number
  ) {
    return root.newMessageAdded;
  }
paymog commented 2 years ago

How did you solve this @alisalim17 - I'm running into the same issue trying to use an async function for the first parameter of withFilter

paymog commented 2 years ago

Found a solution in another issue!

https://github.com/apollographql/graphql-subscriptions/issues/161#issuecomment-408150940

and here's another one: https://github.com/apollographql/graphql-subscriptions/issues/90#issuecomment-727697505