Add support for batched GraphQL responses

This commit is contained in:
Joakim Hellsén 2026-01-12 03:13:39 +01:00
commit 1c13e25b17
No known key found for this signature in database
2 changed files with 27 additions and 0 deletions

View file

@ -1033,6 +1033,12 @@ class Command(BaseCommand):
) -> list[dict[str, Any]]: ) -> list[dict[str, Any]]:
"""Normalize various parsed JSON shapes into a list of dict responses. """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: Args:
parsed_json: The parsed JSON data from the file. parsed_json: The parsed JSON data from the file.
@ -1040,6 +1046,10 @@ class Command(BaseCommand):
A list of response dictionaries. A list of response dictionaries.
""" """
if isinstance(parsed_json, dict): 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] return [parsed_json]
if isinstance(parsed_json, list): if isinstance(parsed_json, list):
return [item for item in parsed_json if isinstance(item, dict)] return [item for item in parsed_json if isinstance(item, dict)]

View file

@ -449,3 +449,20 @@ class GraphQLResponse(BaseModel):
"strict": True, "strict": True,
"populate_by_name": 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,
}