Skip to main content

Query Handler

Description

Processes a specific type of Query, retrieving data and typically returning a DTO.

Definition Schema

The definition for a query_handler component expects the following structure:

{
"$defs": {
"PermissionDefinitionForCtxDTO": {
"example": {
"action": "ActionEnum.CREATE",
"resource": "ResourceEnum.ITEM",
"scope": "ScopeEnum.GENERAL"
},
"properties": {
"action": {
"description": "Action string, e.g., 'RevisionDraftExpertAction.CREATE' or 'module.action_enum.ACTION'",
"title": "Action",
"type": "string"
},
"resource": {
"description": "Resource string, e.g., 'RevisionDraftResource.REVISION_DRAFT' or 'module.resource_enum.RESOURCE'",
"title": "Resource",
"type": "string"
},
"scope": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null,
"description": "Scope string, e.g., 'RevisionDraftLifecycleScope.ANY_STATUS' or 'module.scope_enum.SCOPE'",
"title": "Scope"
}
},
"required": [
"action",
"resource"
],
"title": "PermissionDefinitionForCtxDTO",
"type": "object"
},
"QueryHandlerDependencyInputDTO": {
"example": {
"assigned_attribute_name": "_sessionmaker",
"param_name": "session_factory",
"param_type_str": "sessionmaker[Session]"
},
"example_with_explicit_import": {
"param_import_from_module": "sqlalchemy.ext.asyncio",
"param_import_names": [
"AsyncSession"
],
"param_name": "async_session_factory",
"param_type_str": "AsyncSession"
},
"properties": {
"param_name": {
"description": "Name of the parameter in __init__ (e.g., 'session_factory')",
"title": "Param Name",
"type": "string"
},
"param_type_str": {
"description": "Type hint string for the parameter (e.g., 'sessionmaker[Session]')",
"title": "Param Type Str",
"type": "string"
},
"assigned_attribute_name": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null,
"description": "Name of the attribute to assign the dependency to on self (e.g., '_sessionmaker'). Defaults to '_' + param_name if None.",
"title": "Assigned Attribute Name"
},
"param_import_from_module": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null,
"description": "Optional: Explicit module to import the dependency type from (e.g., 'sqlalchemy.ext.asyncio').",
"title": "Param Import From Module"
},
"param_import_names": {
"anyOf": [
{
"items": {
"type": "string"
},
"type": "array"
},
{
"type": "null"
}
],
"default": null,
"description": "Optional: Explicit list of names to import from the module (e.g., ['AsyncSession']). Used if param_import_from_module is set.",
"title": "Param Import Names"
}
},
"required": [
"param_name",
"param_type_str"
],
"title": "QueryHandlerDependencyInputDTO",
"type": "object"
}
},
"example": {
"dependencies": [
{
"assigned_attribute_name": "_sessionmaker",
"param_name": "session_factory",
"param_type_str": "sessionmaker[Session]"
}
],
"execute_method_ctx_permissions": [
{
"action": "PostAction.READ",
"resource": "PostResource.POST",
"scope": "PostScope.PUBLISHED"
}
],
"execute_return_type_str": "Optional[Post]",
"include_basic_authorize": true,
"name": "GetPostByIdQueryHandler",
"query_bus_instance_name": "query_bus",
"query_module": "generated_queries",
"query_name": "GetPostByIdQuery"
},
"properties": {
"name": {
"description": "Name of the handler class (e.g., 'GetPostByIdQueryHandler')",
"title": "Name",
"type": "string"
},
"query_name": {
"description": "Name of the query class it handles (e.g., 'GetPostByIdQuery')",
"title": "Query Name",
"type": "string"
},
"query_module": {
"description": "Module where the query class is defined (e.g., 'app_root_name.application.posts.queries')",
"title": "Query Module",
"type": "string"
},
"dependencies": {
"description": "List of dependencies for the __init__ method.",
"items": {
"$ref": "#/$defs/QueryHandlerDependencyInputDTO"
},
"title": "Dependencies",
"type": "array"
},
"execute_return_type_str": {
"default": "None",
"description": "Return type hint string for the execute method (e.g., 'Optional[Post]', 'List[PostSummary]')",
"title": "Execute Return Type Str",
"type": "string"
},
"include_basic_authorize": {
"default": true,
"description": "Whether to include a basic 'authorize' method returning True with logging.",
"title": "Include Basic Authorize",
"type": "boolean"
},
"execute_method_ctx_permissions": {
"anyOf": [
{
"items": {
"$ref": "#/$defs/PermissionDefinitionForCtxDTO"
},
"type": "array"
},
{
"type": "null"
}
],
"default": null,
"description": "Permissions for the @ctx decorator on the execute method. Each string should be a valid Python expression for a Permission object.",
"title": "Execute Method Ctx Permissions"
},
"query_bus_instance_name": {
"default": "query_bus",
"description": "Name of the query bus instance variable used for the @register decorator.",
"title": "Query Bus Instance Name",
"type": "string"
}
},
"required": [
"name",
"query_name",
"query_module"
],
"title": "QueryHandlerInputDTO",
"type": "object"
}

Naming and Location Conventions

Class: PascalCase, typically ends with 'QueryHandler'. File: ${query_handler_name}.py (based on ${query_handler_name}). Location: app/application/${bounded_ctx}/handlers. (Requires context: ['bounded_ctx'])

Example Definition

{
"name": "GetPostByIdQueryHandler",
"query_name": "GetPostByIdQuery",
"query_module": "generated_queries",
"dependencies": [
{
"param_name": "session_factory",
"param_type_str": "sessionmaker[Session]",
"assigned_attribute_name": "_sessionmaker"
}
],
"execute_return_type_str": "Optional[Post]",
"include_basic_authorize": true,
"execute_method_ctx_permissions": [
{
"action": "PostAction.READ",
"resource": "PostResource.POST",
"scope": "PostScope.PUBLISHED"
}
],
"query_bus_instance_name": "query_bus"
}