rob3000 / nestjs-kafka

NestJS integration with KafkaJS
The Unlicense
129 stars 44 forks source link

KafkaModule.registerAsync() gives error Nest can't resolve dependencies of the KAFKA_MODULE_OPTIONS (?) #27

Open obaqueiro opened 3 years ago

obaqueiro commented 3 years ago

Hey there, I'm trying to use this but while attempting to use the module with RegisterAsync (version 1.4.0) but it's blowing up. Not sure if I'm doing something wrong or if this is a bug.

This is my app.module.ts:

import { KafkaService, KafkaModule } from '@rob3000/nestjs-kafka';
import { v4 as uuidv4 } from 'uuid';
import { ConfigModule, ConfigService } from '@nestjs/config';

@Module({
  imports: [
    ConfigModule.forRoot(),
    KafkaModule.registerAsync(['my-name'], {
      useFactory: async (configService: ConfigService) => {
        const uuid = uuidv4();
        const broker = configService.get('KAFKA_BROKER');

        Logger.log(`Registering to Kafka service with client uuid: ${uuid}`);
        return [
          {
            name: 'my-name',
            options: {
              client: {
                brokers: [configService.get('KAFKA_BROKER')],
              },
              brokers: [broker],
              consumer: { groupId: 'my-name' },
            },
          },
        ];
      },
      inject: [ConfigService],      
    }),
  ],
  providers: [
    ConfigService,
    KafkaService,
  ],
})
export class AppModule {}

I am injecting the ConfigService by using inject... no idea why am I getting this error.

The error I'm getting:

consumer_1         | [Nest] 90   - 05/24/2021, 10:16:52 PM   [NestFactory] Starting Nest application...
consumer_1         | [Nest] 90   - 05/24/2021, 10:16:53 PM   [ExceptionHandler] Nest can't resolve dependencies of the KAFKA_MODULE_OPTIONS (?). Please make sure that the argument ConfigService at index [0] is available in the KafkaModule context.
rob3000 commented 3 years ago

Hey @obaqueiro The code you have looks right at first glance. I'll attempt to look into this one further today.

rob3000 commented 2 years ago

Hey @obaqueiro did you manage to solve this issue?

Mr777Nick commented 2 years ago

I had this issue for a while and what fixed it for me was by using this ConfigModule.forRoot({isGlobal: true}) instead of the one OP using.

razvgrecu commented 2 years ago

So, I've been at it for some time and this is how I've gotten it fixed:

KafkaModule.registerAsync(['KAFKA_CLIENT'], {
            imports: [ConfigModule],
            useFactory: async (configService: ConfigService) => ([{
                name: 'KAFKA_CLIENT',
                options: {
                    client: {
                        clientId: 'some-client-id',
                        brokers: [configService.get('KAFKA_BROKER')]
                    },
                    consumer: {
                        groupId: 'some-group'
                    }
                },

            }]),
            inject: [ConfigService]
        })

So, because

interface KafkaModuleOptionsAsync extends Pick<ModuleMetadata, 'imports'>

you have access to an imports object within the ModuleOptions and that's the one it will use.

This is also the way it's done in the e2e-test.