Skip to content

Query

query

Module defining the base Query class for domain queries.

Query

Bases: Message[QueryPayloadType]

Base class for all domain queries.

Queries represent a request to retrieve data from the domain.
They are handled by query handlers and should not modify state.

Queries are named in interrogative mood (e.g., GetOrder, FindCustomer,
ListProducts).

Example
class GetOrder(Query[dict[str, object]]):
    def __init__(self, order_id: str):
        super().__init__()
        self._order_id = order_id

    @property
    def _payload(self) -> dict[str, object]:
        return {"order_id": self._order_id}
Source code in src/forging_blocks/foundation/messages/query.py
class Query[QueryPayloadType](Message[QueryPayloadType]):
    """Base class for all domain queries.

    Queries represent a request to retrieve data from the domain.
    They are handled by query handlers and should not modify state.

    Queries are named in interrogative mood (e.g., GetOrder, FindCustomer,
    ListProducts).

    Example:
        ```python
        class GetOrder(Query[dict[str, object]]):
            def __init__(self, order_id: str):
                super().__init__()
                self._order_id = order_id

            @property
            def _payload(self) -> dict[str, object]:
                return {"order_id": self._order_id}
        ```
    """

    @property
    @abstractmethod
    def _payload(self) -> dict[str, object]: ...