#CQRS Error: The query handler for the "7ea93be2-324b-4b9e-b4fd-a4470184d988" query was not found!

1 messages · Page 1 of 1 (latest)

random hedge
#

I am having this problem when using the CQRS module. Essentially, it keeps saying it cannot find my query (and command) handlers. Here's my setup.

In my controller, I have this.

@Controller('accounts')
export class AccountsController {
  constructor(
    private readonly commandBus: CommandBus,
    private readonly queryBus: QueryBus,
    private readonly logger: SystemLogger,
  ) {

  }


 // ....
 async onApplicationBootstrap() {
    // ...
    const account: Account | null = await this.queryBus.execute(
        new GetRootAccountQuery(correlation)
    );
   // ...
 }

What is supposed to happen is that on startup, my app first checks if a root account exists (indicating the application has been set up). If there isn't it creates one.

My module is set up as follows.

const commandHandlers = [
  CreateAccountHandler, 
  CreateRootAccountHandler
];
const queryHandlers = [
  GetAccountForIdQuery,
  GetAccountForIdHandler,
  GetRootAccountQuery,
  GetRootAccountHadler,
  GetAccountIdentifierQuery,
  GetAccountIdentifierHandler,
  GetAccountForEmailQuery,
  GetAccountForEmailHandler
];
const sagas = [AccountsSagas];
const eventHandlers = [
  CreateAccountSummaryHandler
]

@Module({
  controllers: [AccountsController],
  providers: [
    FireStoreAccountsEventStore,
    ...commandHandlers,
    ...queryHandlers,
    ...sagas,
    ...eventHandlers
  ],
  imports: [
    CqrsModule, 
    CoreModule,
    ConfigModule
  ],
  exports: [
    GetAccountForIdQuery,
    GetAccountIdentifierQuery,
    GetAccountForEmailQuery
  ]
})
export class AccountsModule {}

I double checked the docs to make sure I was doing everything correctly. However, when I start the application, it is saying it cannot find the query handler (and the event handler when I comment out the above code and force it to run the command to create the root account). My logs are telling me that it isn't even entering the handler class for the query.

What am I doing wrong?

#

For reference, here is the handler for my GetRootAccountQuery. ```@QueryHandler(GetRootAccountQuery)
export class GetRootAccountHadler implements IQueryHandler<GetRootAccountQuery> {

private readonly context: string;

constructor(
    private readonly queryBus: QueryBus,
    private readonly eventStore: FireStoreAccountsEventStore,
    private readonly logger: SystemLogger
) {
    //
    this.context = GetRootAccountHadler.name;
}

public async execute(query: GetRootAccountQuery): Promise<Account | null> {
    this.logger.log('Executing execute()', this.context);

    try {
        this.logger.debug('Getting account id', this.context);
        const rootAccountEvets = await this.eventStore.query({
            event: 'root-account-created'
        });

        if (rootAccountEvets.length > 0) {
            // process
            const id = (rootAccountEvets[0] as RootAccountCreatedEvent).account_id;
            this.logger.debug(`Getting root account for id ${id.toString()}`, this.context);
            const account: Account = await this.queryBus.execute(
                new GetAccountForIdQuery(id, query.correlation)
            );

            this.logger.debug('Successfully retrieved account', this.context);
            this.logger.log('Exiting execute() successfully');
            return account;
        }
        else {
            // no root account.
            this.logger.debug('Root Account not found', this.context);
            this.logger.log('Exiting execute() successfully', this.context);
            return null;
        }
    }
    catch (e) {
        const error = e as Error;
        this.logger.error(error.message, this.context);
        this.logger.log('Exiting execute() with error', this.context);
        throw e;
    }
}

}```

#

The GetAccountForIdQuery has a similar handler class ```@QueryHandler(GetAccountForIdQuery)
export class GetAccountForIdHandler
implements IQueryHandler<GetAccountForIdQuery>
{
constructor(
private readonly repository: FireStoreAccountsEventStore,
private readonly logger: SystemLogger,
) { }

public async execute(query: GetAccountForIdQuery): Promise<Account | null> {

// gets the account for account summary.
try {
  const events = await this.repository.query({
    key: query.id.toString(),
    event: 'account-summary-created',
    order: 'descending',
    count: 1,
  });

  if (events.length > 0) {
    // process stuff
  }
  else {
    return null;
  }
}
catch (e) {
  throw e;
}

// get all events after summary date related to the ID.
try {
  this.logger.debug('Getting relevant events for query.');
  const events = await this.repository.query({
    key: query.id.toString(),
    after: accountSummaryDate,
  });

  if (events.length > 0) {
    // recreate the account object from the events.
    // process stuff
     
  } else {
    // the account does not exist.
    account = null;
  }
} catch (e) {
  // something went wrong with the event store.
  const error = e as Error;
  this.logger.error(error.message);
  throw e;
}
this.logger.log('Exiting execute()');
return account;

}
}

#

On a side note, is it possible to get it to display the name of the query handler instead of its internal ID in the error? Just so I know which one it is referring to. In this case, it’s pretty intuitive. But may become harder when as my app grows.

random hedge
#

No one?