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)