import httpx

from typing import Optional, Any

from ..utils import get_params


class GraphqlService:
  def __init__(self, client: httpx.Client, path_prefix: str = "/") -> None:
    self.client, self.path_prefix = client, path_prefix

  def request(
    self,
    query: Optional[str] = None,
    prefix: Optional[str] = None,
    mutation: Optional[str] = None,
    variables: Optional[Any] = None,
    brotli: Optional[Any] = None,
    **config: Any,
  ) -> Any:
    path_prefix = prefix or self.path_prefix or "/"
    params = get_params(config=config, brotli_cfg=brotli)
    response = self.client.post(f"{path_prefix}graphql", json={query, mutation, variables}, **{**config, "params": params})
    response.raise_for_status()
    return response.json()
