davidyaha / graphql-redis-subscriptions

A graphql subscriptions implementation using redis and apollo's graphql-subscriptions
MIT License
1.11k stars 125 forks source link

Simple example not working with ioredis 5 due to mismatched types #605

Closed bneigher closed 1 year ago

bneigher commented 1 year ago

Hello.. I'm creating a module in nestjs and I'm seeing that there is a type incompativility with ioredis and what the Redis/Cluster interfaces expect from graphql-redis-subscriptions.

The following code shows issues with publisher and subscriber

import { Global, Module } from '@nestjs/common'
import { RedisPubSub } from 'graphql-redis-subscriptions'
import { Cluster } from 'ioredis'

export const PUB_SUB = 'PUB_SUB'

@Global()
@Module({
  providers: [
    {
      provide: PUB_SUB,
      useFactory: () => {
        const cluster = new Cluster([
          {
            host: process.env.GLOBAL_CACHE,
            port: 6379,
          },
        ])

        return new RedisPubSub({
          publisher: cluster, // PROBLEM
          subscriber: cluster, // PROBLEM
        })
      },
    },
  ],
  exports: [PUB_SUB],
})

export class SubscriptionsModule {}
Type 'Cluster' is not assignable to type 'RedisClient | undefined'.
  Type 'Cluster' is not assignable to type 'Redis | Cluster'.
    Type 'Cluster' is missing the following properties from type 'Redis': stream, connector, reconnectTimeout, condition, and 139 more.

I believe the issue is with this line: import {Cluster, Redis, RedisOptions} from 'ioredis';

which needs to be replaced with import type { Cluster, Redis, RedisOptions } from "@types/ioredis"

as doing so resolves the issue for me.

rvitaliy commented 1 year ago

FYI: https://www.npmjs.com/package/@types/ioredis is deprecated you should use ioredis. So probably your problem is something different. Try to remove @types/ioredis from your dependencies