Query Handling

Query CQRS PHP

Be sure to read CQRS Introduction before diving in this chapter.

Handling Queries

External Query Handlers are Services available in your dependency container, which are defined to handle Queries.

class TicketService
{
    #[QueryHandler] 
    public function getTicket(GetTicketById $query) : array
    {
        //return ticket
    }
}

Queries are Plain Old PHP Objects:

class readonly GetTicketById
{
    public function __construct(
        public string $ticketId
    ) {}
}

To send an Query we will be using send method on QueryBus. Query will be delivered to corresponding Query Handler.

class TicketController
{
   // Query Bus will be auto registered in Depedency Container.
   public function __construct(private QueryBus $queryBus) {}
   
   public function createTicketAction(Request $request) : Response
   {
      $result = $this->queryBus->send(
         new GetTicketById(
            $request->get("ticketId")            
         )
      );
      
      return new Response(\json_encode($result));
   }
}

Sending with Routing

Just like with Commands, we may use routing in order to execute queries:

class TicketService
{
    #[QueryHandler("ticket.getById")] 
    public function getTicket(string $ticketId) : array
    {
        //return ticket
    }
}

To send an Query we will be using sendWithRouting method on QueryBus. Query will be delivered to corresponding Query Handler.

class TicketController
{
   public function __construct(private QueryBus $queryBus) {}
   
   public function createTicketAction(Request $request) : Response
   {
      $result = $this->queryBus->sendWithRouting(
         "ticket.getById",
         $request->get("ticketId")            
      );
      
      return new Response(\json_encode($result));
   }
}

Converting result from Query Handler

If you have registered Converter for specific Media Type, then you can tell Ecotone to convert result of your Query Bus to specific format. In order to do this, we need to make use of Metadataand replyContentType header.

class TicketController
{
   public function __construct(private QueryBus $queryBus) {}
   
   public function createTicketAction(Request $request) : Response
   {
      $result = $this->queryBus->sendWithRouting(
         "ticket.getById",
         $request->get("ticketId"),
         // Tell Ecotone which format you want in return
         expectedReturnedMediaType: "application/json"            
      );
      
      return new Response($result);
   }
}

C

Last updated