Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pubsub.asyncIterator typescript type #4556

Closed
xialvjun opened this issue Sep 10, 2020 · 4 comments
Closed

Fix pubsub.asyncIterator typescript type #4556

xialvjun opened this issue Sep 10, 2020 · 4 comments
Labels
🧬 typings Relates to TypeScript changes or improvements.

Comments

@xialvjun
Copy link

package: graphql-subscriptions
version: 1.1.0

expected behavior: code like down is typescript ok

Subscription: {
  on_new_count: {
    subscribe: async function*(_, args, ctx, info) {
      const iter = pubsub.asyncIterator<number>('on_new_count');
      // this line errors
      for await (const iterator of iter) {
      }
    },
    resolve: (_: number) => _,
  },
};

actual behavior: typescript errors Type 'AsyncIterator<number, any, undefined>' must have a '[Symbol.asyncIterator]()' method that returns an async iterator.

@xialvjun
Copy link
Author

I know I can use withFilter, but then type generated by graphql-codegen will be lost , ex - args should can be its real type, but using withFilter, I got any

@saeidfiy
Copy link

i solve it with subscribe: async (
_: void,
{ Inputs: { chatId } }: { Inputs: SendMessageSubVariable }
): Promise<AsyncIterator<unknown, { message: string }, undefined>> => {
const key = messageKey(chatId);
return pubsub.asyncIterator([key]);
}
}

@abernix abernix added the 🧬 typings Relates to TypeScript changes or improvements. label Dec 31, 2020
@Miljoen
Copy link

Miljoen commented Feb 25, 2022

Hi guys I came across this issue and this worked for me:

    Subscription: {
        userUpdated: {
            subscribe: () => ({
                [Symbol.asyncIterator]: () => pubSub.asyncIterator(['USER_UPDATED']),
            }),
        },
    },

Basically the best way to handle this is to make your Iterator an Iterable

Issue came up as part of strongly typing my Apollo Server Resolvers using Codegen

@glasser
Copy link
Member

glasser commented Feb 25, 2022

Looks like this is more relevant to the (not super actively maintained, unfortunately) graphql-subscriptions project rather than apollo-server so I will close this issue. Looks like some related discussion on apollographql/graphql-subscriptions#143 and apollographql/graphql-subscriptions#192

Actually I think this might be fixed by apollographql/graphql-subscriptions#232 which @hwillson has scheduled to be released in 3.0 apollographql/graphql-subscriptions#250

@glasser glasser closed this as completed Feb 25, 2022
exbotanical added a commit to exbotanical/uxc that referenced this issue Mar 12, 2022
typecast subscription resolvers to address AsyncIterator codegen
mismatch, as described here
apollographql/apollo-server#4556 and to be
imminently resolved per graphql-subscriptions v3.0
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🧬 typings Relates to TypeScript changes or improvements.
Projects
None yet
Development

No branches or pull requests

5 participants