apollographql / apollographql/graphql-subscriptions

Return type of pubsub.asynctIterator should include AsyncIterable

Open
#261 2 comments 16 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
1.6k
Forks
129
PR merge metrics
No merged PRs in 30d

Description

the `pubsub.asyncIterator` method [currently is typed as returning an AsyncIterator](https://github.com/apollographql/graphql-subscriptions/blob/master/src/pubsub-engine.ts#L7-L9). While this is accurate, it misses an important detail:

The `subscribe` function of a graphql field expects an AsyncIterable, not an AsyncIterator. At runtime this works because the [PubSubAsyncIterator also implements the AsyncIterable interface](https://github.com/apollographql/graphql-subscriptions/blob/master/src/pubsub-async-iterator.ts#L69-L71). Unfortunately this is hidden by the explicit return type in `pubsub.asyncIterator` Which is typed as returning an `AsyncIterator`, not a `PubSubAsyncIterator`, and does not include the `[Symbol.asyncIterator]` property.

This library is very commonly used when implementing graphql subscriptions, and I believe this has resulted in it becoming common to mis-type the expected return type of `subscribe` functions ([for example in nexus](https://github.com/graphql-nexus/nexus/blob/main/src/definitions/subscriptionType.ts#L24))

This should be a relatively easy, and backwards compatible fix

Contributor guide

Open the contributing guide

Research direction

Start with the return type in src/pubsub-engine.ts and compare it with the AsyncIterable implementation in src/pubsub-async-iterator.ts. Update the public typing so pubsub.asyncIterator exposes the async-iterable behavior needed by GraphQL subscription fields, while retaining compatibility with existing AsyncIterator use.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, backend-api-design
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.