Fix Users backend not sending back pastaporto actions
@@ -6,19 +6,27 @@ from starlette.responses import Response
from starlette.types import Receive, Scope, Send
from starlette.websockets import WebSocket
from strawberry.asgi import GraphQL as BaseGraphQL
+from strawberry.asgi.handlers.http_handler import HTTPHandler
+
from users.api.context import Context
from users.api.schema import schema
from users.settings import PASTAPORTO_ACTION_SECRET
-class GraphQL(BaseGraphQL):
- def __init__(self) -> None:
- super().__init__(schema)
-
- async def handle_http(self, scope: Scope, receive: Receive, send: Send):
+class CustomHTTPHandler(HTTPHandler):
+ async def handle(self, scope: Scope, receive: Receive, send: Send):
request = Request(scope=scope, receive=receive)
root_value = await self.get_root_value(request)
- context = await self.get_context(request)
+
+ sub_response = Response(
+ content=None,
+ status_code=None, # type: ignore
+ headers=None,
+ media_type=None,
+ background=None,
+ )
+
+ context = await self.get_context(request=request, response=sub_response)
response = await self.get_http_response(
request=request,
@@ -29,6 +37,14 @@ class GraphQL(BaseGraphQL):
context=context,
)
+ response.headers.raw.extend(sub_response.headers.raw)
+
+ if sub_response.background:
+ response.background = sub_response.background
+
+ if sub_response.status_code:
+ response.status_code = sub_response.status_code
+
if context.pastaporto_action:
response.headers[
PASTAPORTO_ACTION_X_HEADER
@@ -36,6 +52,13 @@ class GraphQL(BaseGraphQL):
await response(scope, receive, send)
+
+class GraphQL(BaseGraphQL):
+ http_handler_class = CustomHTTPHandler
+
+ def __init__(self, *args, **kwargs) -> None:
+ super().__init__(schema, *args, **kwargs)
+
async def get_context(
self, request: Union[Request, WebSocket], response: Optional[Response] = None
) -> Context:
