"""
GraphQL Request Module
This module defines the GraphQLRequest dataclass that holds the built query/mutation
and provides methods for execution.
"""
from dataclasses import dataclass, field
from typing import Dict, Any, Optional
[docs]
@dataclass
class GraphQLRequest:
"""
Represents a built GraphQL request ready for execution.
Attributes:
query: The GraphQL query/mutation string
variables: The variables to pass with the request
operation_name: The name of the operation (optional)
operation_type: Either 'query' or 'mutation'
"""
query: str
operation_name: Optional[str] = None
operation_type: str = "query"
variables: Dict[str, Any] = field(default_factory=dict)
[docs]
def to_dict(self) -> Dict[str, Any]:
"""Convert request to dictionary format for HTTP payload."""
result = {"query": self.query}
if self.variables:
result["variables"] = self.variables
if self.operation_name:
result["operationName"] = self.operation_name
return result
[docs]
def with_variables(self, variables: Dict[str, Any]) -> "GraphQLRequest":
"""
Create a new request with the given variables.
Args:
variables: Dictionary of variable values
Returns:
New GraphQLRequest with variables set
"""
return GraphQLRequest(
query=self.query,
operation_name=self.operation_name,
operation_type=self.operation_type,
variables=variables
)
def __str__(self) -> str:
return self.query