skip to Main Content

I am testing using NestJS with Microservices using Redis. I have setup the code just like the documentation suggests, but when I send a message pattern from the client, my Microservice is not getting fired.

My client is a GraphQL NestJS API which I want to funnel all the request down to.

Main.ts

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.listen(3000);
}
bootstrap();

app.module.ts

const REDIS_HOST = process.env.REDIS_HOST || 'localhost';
const REDIS_PORT = process.env.REDIS_PORT || 6379;

@Module({
  imports: [
    GraphQLModule.forRoot({
      autoSchemaFile: 'schema.gql',
      installSubscriptionHandlers: true,
      context: ({ req }) => {
        return {
          req
        }
      }
    }),
    TasksModule,
    TypeOrmModule.forRoot(typeOrmConfig),
    AuthModule,
    ClientsModule.register([{ name: 'TASK_SERVICE', transport: Transport.REDIS, options: { url: `redis://${REDIS_HOST}:${REDIS_PORT}` }}]),
  ]
})
export class AppModule {}

task.service.ts

@Injectable()
export class TaskService {
    constructor(
        @Inject('TASK_SERVICE') private readonly client: ClientProxy
    ) { }

    async onModuleInit() {
        // Connect your client to the redis server on startup.
        await this.client.connect();
      }


    public async findAll() {
        const response = await this.client.send(
            { type: 'get-tasks' },
            { someImaginaryParams: 42 }
        ).toPromise();
        return response;
    }
}

Then I have created a Microservice API (which is also uses GraphQL) which contains the following:

main.ts

const REDIS_HOST = process.env.REDIS_HOST || '127.0.0.1';
const REDIS_PORT = process.env.REDIS_PORT || 6379;

const logger = new Logger('Main');

const microserviceOptions = {
  transport: Transport.REDIS,
  options: {
    url: `redis://${REDIS_HOST}:${REDIS_PORT}`
  }
}

async function bootstrap() {
  const app = await NestFactory.createMicroservice(AppModule, microserviceOptions);
  app.listen(() => {
    logger.log(`Microservice listening...`)
  });
}
bootstrap();

tasks.resolver.ts

@Resolver(of => TaskDto)
export class TaskResolver {
    constructor(
        private readonly taskService: TaskService
    ) { }

    @Query(() => [TaskDto])
    @MessagePattern({ type: 'get-tasks' })
    async tasks() {
        console.log('Tasks API Hit - tasks');
        return await this.taskService.findAll();
    }
}

So the console.log that is held in the Microservice is not being hit, and when I test this on GraphIQL it just never returns a response from the request.

2

Answers


  1. You need to apply the Observable return type from rxjs library to the findAll() at your task.service.ts like this:

    public async findAll(): Observable<any> {
        const response = await this.client.send(
            { type: 'get-tasks' },
            { someImaginaryParams: 42 }
        ).pipe(timeout(5000));
        return response;
    }
    
    Login or Signup to reply.
  2. Only controllers can listen @MessagePattern as explained here https://docs.nestjs.com/microservices/basics#request-response

    This decorator should be used only within the controller classes since
    they are the entry points for your application.

    I think is not possible use @MessagePattern into resolvers.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search