feat: 9.5. Сборка и монтирование

This commit is contained in:
2026-10-12 17:12:27 +03:00
parent 707ba4aaff
commit 9d9e190328
3 changed files with 57 additions and 1 deletions
+44
View File
@@ -0,0 +1,44 @@
from datetime import datetime
from pathlib import Path
from typing import Any
from ariadne import ScalarType, load_schema_from_path, make_executable_schema
from ariadne import snake_case_fallback_resolvers
from ariadne.asgi import GraphQL
from app.graphql.context import get_context_value
from app.graphql.directives import IsAdminDirective, IsAuthenticatedDirective
from app.graphql.resolvers import audit_event, mutation, query
datetime_scalar = ScalarType("DateTime")
@datetime_scalar.serializer
def _serialize_datetime(value: Any) -> str:
if isinstance(value, datetime):
return value.isoformat()
return str(value)
# Загрузить SDL-схему из файла (в Ariadne есть функция load_schema_from_path)
type_defs = load_schema_from_path(str(Path(__file__).parent / "schema.graphql"))
# Собрать исполняемую схему через `make_executable_schema`, передав:
schema = make_executable_schema(
# типы;
type_defs,
# объекты `query` и `mutation` из `resolvers.py`;
query,
mutation,
audit_event,
datetime_scalar,
snake_case_fallback_resolvers,
# словарь директив `{"isAuthenticated": ..., "isAdmin": ...}`.
directives={
"isAuthenticated": IsAuthenticatedDirective,
"isAdmin": IsAdminDirective,
},
)
# Обернуть схему в ASGI-приложение `GraphQL(schema, context_value=..., debug=True)`.
graphql_app = GraphQL(schema, context_value=get_context_value, debug=True)