|
16 | 16 | ) |
17 | 17 |
|
18 | 18 | import aiohttp |
| 19 | +from aiohttp import BodyPartReader, MultipartReader |
19 | 20 | from aiohttp.client_exceptions import ClientResponseError |
20 | 21 | from aiohttp.client_reqrep import Fingerprint |
21 | 22 | from aiohttp.helpers import BasicAuth |
@@ -421,12 +422,186 @@ async def execute_batch( |
421 | 422 | except Exception as e: |
422 | 423 | raise TransportConnectionFailed(str(e)) from e |
423 | 424 |
|
424 | | - def subscribe( |
| 425 | + async def subscribe( |
425 | 426 | self, |
426 | 427 | request: GraphQLRequest, |
427 | 428 | ) -> AsyncGenerator[ExecutionResult, None]: |
428 | | - """Subscribe is not supported on HTTP. |
| 429 | + """Execute a GraphQL subscription and yield results from multipart response. |
429 | 430 |
|
430 | | - :meta private: |
| 431 | + :param request: GraphQL request to execute |
| 432 | + :yields: ExecutionResult objects as they arrive in the multipart stream |
431 | 433 | """ |
432 | | - raise NotImplementedError(" The HTTP transport does not support subscriptions") |
| 434 | + if self.session is None: |
| 435 | + raise TransportClosed("Transport is not connected") |
| 436 | + |
| 437 | + post_args = self._prepare_request(request) |
| 438 | + |
| 439 | + # Add headers for multipart subscription |
| 440 | + headers = post_args.get("headers", {}) |
| 441 | + headers.update( |
| 442 | + { |
| 443 | + "Content-Type": "application/json", |
| 444 | + "Accept": ( |
| 445 | + "multipart/mixed;boundary=graphql;" |
| 446 | + "subscriptionSpec=1.0,application/json" |
| 447 | + ), |
| 448 | + } |
| 449 | + ) |
| 450 | + post_args["headers"] = headers |
| 451 | + |
| 452 | + try: |
| 453 | + async with self.session.post(self.url, ssl=self.ssl, **post_args) as resp: |
| 454 | + # Saving latest response headers in the transport |
| 455 | + self.response_headers = resp.headers |
| 456 | + |
| 457 | + # Check for errors |
| 458 | + if resp.status >= 400: |
| 459 | + # Raise a TransportServerError if status > 400 |
| 460 | + self._raise_transport_server_error_if_status_more_than_400(resp) |
| 461 | + |
| 462 | + initial_content_type = resp.headers.get("Content-Type", "") |
| 463 | + if ( |
| 464 | + "application/json" in initial_content_type |
| 465 | + and "multipart/mixed" not in initial_content_type |
| 466 | + ): |
| 467 | + yield await self._prepare_result(resp) |
| 468 | + return |
| 469 | + |
| 470 | + if ( |
| 471 | + ("multipart/mixed" not in initial_content_type) |
| 472 | + or ("boundary=graphql" not in initial_content_type) |
| 473 | + or ("subscriptionSpec=1.0" not in initial_content_type) |
| 474 | + ): |
| 475 | + raise TransportProtocolError( |
| 476 | + f"Unexpected content-type: {initial_content_type}. " |
| 477 | + "Server may not support the multipart subscription protocol." |
| 478 | + ) |
| 479 | + |
| 480 | + # Parse multipart response |
| 481 | + async for result in self._parse_multipart_response(resp): |
| 482 | + yield result |
| 483 | + |
| 484 | + except TransportError: |
| 485 | + raise |
| 486 | + except Exception as e: |
| 487 | + raise TransportConnectionFailed(str(e)) from e |
| 488 | + |
| 489 | + async def _parse_multipart_response( |
| 490 | + self, |
| 491 | + response: aiohttp.ClientResponse, |
| 492 | + ) -> AsyncGenerator[ExecutionResult, None]: |
| 493 | + """ |
| 494 | + Parse a multipart response stream and yield execution results. |
| 495 | +
|
| 496 | + Uses aiohttp's built-in MultipartReader to handle the multipart protocol. |
| 497 | +
|
| 498 | + :param response: The aiohttp response object |
| 499 | + :yields: ExecutionResult objects |
| 500 | + """ |
| 501 | + # Use aiohttp's built-in multipart reader |
| 502 | + reader = MultipartReader.from_response(response) |
| 503 | + |
| 504 | + # Iterate through each part in the multipart response |
| 505 | + while True: |
| 506 | + try: |
| 507 | + part = await reader.next() |
| 508 | + except Exception: |
| 509 | + # reader.next() throws on empty parts at the end of the stream. |
| 510 | + # (some servers may send this.) |
| 511 | + # see: https://github.com/aio-libs/aiohttp/pull/11857 |
| 512 | + # As an ugly workaround for now, we can check if we've reached |
| 513 | + # EOF and assume this was the case. |
| 514 | + if reader.at_eof(): |
| 515 | + break |
| 516 | + |
| 517 | + # Otherwise, re-raise unexpected errors |
| 518 | + raise # pragma: no cover |
| 519 | + |
| 520 | + if part is None: |
| 521 | + # No more parts |
| 522 | + break |
| 523 | + |
| 524 | + assert not isinstance( |
| 525 | + part, MultipartReader |
| 526 | + ), "Nested multipart parts are not supported in GraphQL subscriptions" |
| 527 | + |
| 528 | + result = await self._parse_multipart_part(part) |
| 529 | + if result: |
| 530 | + yield result |
| 531 | + |
| 532 | + async def _parse_multipart_part( |
| 533 | + self, part: BodyPartReader |
| 534 | + ) -> Optional[ExecutionResult]: |
| 535 | + """ |
| 536 | + Parse a single part from a multipart response. |
| 537 | +
|
| 538 | + :param part: aiohttp BodyPartReader for the part |
| 539 | + :return: ExecutionResult or None if part is empty/heartbeat |
| 540 | + """ |
| 541 | + # Verify the part has the correct content type |
| 542 | + content_type = part.headers.get(aiohttp.hdrs.CONTENT_TYPE, "") |
| 543 | + if not content_type.startswith("application/json"): |
| 544 | + raise TransportProtocolError( |
| 545 | + f"Unexpected part content-type: {content_type}. " |
| 546 | + "Expected 'application/json'." |
| 547 | + ) |
| 548 | + |
| 549 | + try: |
| 550 | + # Read the part content as text |
| 551 | + body = await part.text() |
| 552 | + body = body.strip() |
| 553 | + |
| 554 | + if log.isEnabledFor(logging.DEBUG): |
| 555 | + log.debug("<<< %s", ascii(body or "(empty body, skipping)")) |
| 556 | + |
| 557 | + if not body: |
| 558 | + return None |
| 559 | + |
| 560 | + # Parse JSON body using custom deserializer |
| 561 | + data = self.json_deserialize(body) |
| 562 | + |
| 563 | + # Handle heartbeats - empty JSON objects |
| 564 | + if not data: |
| 565 | + log.debug("Received heartbeat, ignoring") |
| 566 | + return None |
| 567 | + |
| 568 | + # The multipart subscription protocol wraps data in a "payload" property |
| 569 | + if "payload" not in data: |
| 570 | + log.warning("Invalid response: missing 'payload' field") |
| 571 | + return None |
| 572 | + |
| 573 | + payload = data["payload"] |
| 574 | + |
| 575 | + # Check for transport-level errors (payload is null) |
| 576 | + if payload is None: |
| 577 | + # If there are errors, this is a transport-level error |
| 578 | + errors = data.get("errors") |
| 579 | + if errors: |
| 580 | + error_messages = [ |
| 581 | + error.get("message", "Unknown transport error") |
| 582 | + for error in errors |
| 583 | + ] |
| 584 | + |
| 585 | + for message in error_messages: |
| 586 | + log.error(f"Transport error: {message}") |
| 587 | + |
| 588 | + raise TransportServerError("\n\n".join(error_messages)) |
| 589 | + else: |
| 590 | + # Null payload without errors - just skip this part |
| 591 | + return None |
| 592 | + |
| 593 | + # Extract GraphQL data from payload |
| 594 | + return ExecutionResult( |
| 595 | + data=payload.get("data"), |
| 596 | + errors=payload.get("errors"), |
| 597 | + extensions=payload.get("extensions"), |
| 598 | + ) |
| 599 | + except json.JSONDecodeError as e: |
| 600 | + log.warning( |
| 601 | + f"Failed to parse JSON: {ascii(e)}, " |
| 602 | + f"body: {ascii(body[:100]) if body else ''}" |
| 603 | + ) |
| 604 | + return None |
| 605 | + except UnicodeDecodeError as e: |
| 606 | + log.warning(f"Failed to decode part: {ascii(e)}") |
| 607 | + return None |
0 commit comments