Skip to content

Subscription types incompatible with graphql-subscription's PubSub #7197

@sarink

Description

@sarink

The latest version of graphql-code-generator has changed the subscribe function return type to AsyncIterable<TResult> | Promise<AsyncIterable<TResult>>;

However, when using apollo's graphql-subscriptions lib, the type returned from their pubsub-engine is AsyncIterator https://github.com/apollographql/graphql-subscriptions/blob/master/src/pubsub-engine.ts#L8

AsyncIterable and AsyncIterator types are incompatible.

Example (this code will throw a type error):

const pubsub = new PubSub();

const typeDefs = gql`
  type Subscription {
    root: String
  }
`

const resolvers: Resolvers = {
  Subscription: {
    root: {
      subscribe: () => {
        // Type 'AsyncIterator<unknown, any, undefined>' is not assignable to type 'AsyncIterable<any> | Promise<AsyncIterable<any>>'.
        return pubsub.asyncIterator('root');
      }
    }
  }
};

What's the recommended workaround for this issue?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions