From 1c13e25b17730f46c1ba881ac88e019e6a503d0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Hells=C3=A9n?= Date: Mon, 12 Jan 2026 03:13:39 +0100 Subject: [PATCH] Add support for batched GraphQL responses --- .../management/commands/better_import_drops.py | 10 ++++++++++ twitch/schemas.py | 17 +++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/twitch/management/commands/better_import_drops.py b/twitch/management/commands/better_import_drops.py index f531d9f..a770984 100644 --- a/twitch/management/commands/better_import_drops.py +++ b/twitch/management/commands/better_import_drops.py @@ -1033,6 +1033,12 @@ class Command(BaseCommand): ) -> list[dict[str, Any]]: """Normalize various parsed JSON shapes into a list of dict responses. + Handles: + - Single dict response: {"data": {...}} + - List of responses: [{"data": {...}}, {"data": {...}}] + - Batched format: {"responses": [{"data": {...}}, {"data": {...}}]} + - Tuple from json_repair: (data, repair_log) + Args: parsed_json: The parsed JSON data from the file. @@ -1040,6 +1046,10 @@ class Command(BaseCommand): A list of response dictionaries. """ if isinstance(parsed_json, dict): + # Check for batched format: {"responses": [...]} + if "responses" in parsed_json and isinstance(parsed_json["responses"], list): + return [item for item in parsed_json["responses"] if isinstance(item, dict)] + # Single response: {"data": {...}} return [parsed_json] if isinstance(parsed_json, list): return [item for item in parsed_json if isinstance(item, dict)] diff --git a/twitch/schemas.py b/twitch/schemas.py index 15080ef..b110432 100644 --- a/twitch/schemas.py +++ b/twitch/schemas.py @@ -449,3 +449,20 @@ class GraphQLResponse(BaseModel): "strict": True, "populate_by_name": True, } + + +class BatchedGraphQLResponse(BaseModel): + """Schema for batched GraphQL responses wrapped in a 'responses' array. + + Handles cases where multiple GraphQL responses are collected and wrapped + in an outer object with a 'responses' field. + """ + + responses: list[GraphQLResponse] + + model_config = { + "extra": "forbid", + "validate_assignment": True, + "strict": True, + "populate_by_name": True, + }