graphql-handler-fix
All checks were successful
Deploy on push / deploy (push) Successful in 1m12s

This commit is contained in:
Untone 2024-10-14 12:31:55 +03:00
parent 3188a67661
commit 6f6b619c11
2 changed files with 17 additions and 7 deletions

2
cache/precache.py vendored
View File

@ -82,7 +82,7 @@ async def precache_data():
# cache reset # cache reset
value = await redis.get(key) value = await redis.get(key)
await redis.execute("FLUSHDB") await redis.execute("FLUSHDB")
logger.info(f"FLUSHDB {REDIS_URL}") logger.info("redis: FLUSHDB")
if value is not None: if value is not None:
await redis.execute("HSET", key, value) await redis.execute("HSET", key, value)
logger.info(f"Значение ключа '{key}' сохранено") logger.info(f"Значение ключа '{key}' сохранено")

22
main.py
View File

@ -6,12 +6,15 @@ from os.path import exists
from ariadne import load_schema_from_path, make_executable_schema from ariadne import load_schema_from_path, make_executable_schema
from ariadne.asgi import GraphQL from ariadne.asgi import GraphQL
from starlette.applications import Starlette from starlette.applications import Starlette
from starlette.responses import JSONResponse from starlette.requests import Request
from starlette.responses import JSONResponse, Response
from starlette.routing import Route from starlette.routing import Route
from cache.precache import precache_data from cache.precache import precache_data
from cache.revalidator import revalidation_manager from cache.revalidator import revalidation_manager
from orm import ( # , collection, invite from orm import (
# collection,
# invite,
author, author,
community, community,
notification, notification,
@ -89,19 +92,26 @@ graphql_app = GraphQL(schema, debug=True)
# Оборачиваем GraphQL-обработчик для лучшей обработки ошибок # Оборачиваем GraphQL-обработчик для лучшей обработки ошибок
async def graphql_handler(request): async def graphql_handler(request: Request):
if request.method not in ["GET", "POST"]:
return JSONResponse({"error": "Method Not Allowed"}, status_code=405)
try: try:
return await graphql_app.handle_request(request) result = await graphql_app.handle_request(request)
if isinstance(result, Response):
return result
return JSONResponse(result)
except asyncio.CancelledError: except asyncio.CancelledError:
return JSONResponse({"error": "Request cancelled"}, status_code=499) return JSONResponse({"error": "Request cancelled"}, status_code=499)
except Exception as e: except Exception as e:
print(f"GraphQL error: {str(e)}")
return JSONResponse({"error": str(e)}, status_code=500) return JSONResponse({"error": str(e)}, status_code=500)
# main starlette app object with ariadne mounted in root # Обновляем маршрут в Starlette
app = Starlette( app = Starlette(
routes=[ routes=[
Route("/", graphql_handler), Route("/", graphql_handler, methods=["GET", "POST"]),
Route("/new-author", WebhookEndpoint), Route("/new-author", WebhookEndpoint),
], ],
lifespan=lifespan, lifespan=lifespan,