-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from mirumee/custom_config_file
Custom config file
- Loading branch information
Showing
18 changed files
with
318 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[tool.ariadne-codegen] | ||
queries_path = "queries.graphql" | ||
schema_path = "schema.graphql" | ||
target_package_name = "custom_config_client" | ||
include_comments = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from .async_base_client import AsyncBaseClient | ||
from .base_model import BaseModel | ||
from .client import Client | ||
from .exceptions import ( | ||
GraphQLClientError, | ||
GraphQLClientGraphQLError, | ||
GraphQLClientGraphQLMultiError, | ||
GraphQLClientHttpError, | ||
GraphQlClientInvalidResponseError, | ||
) | ||
from .test import Test | ||
|
||
__all__ = [ | ||
"AsyncBaseClient", | ||
"BaseModel", | ||
"Client", | ||
"GraphQLClientError", | ||
"GraphQLClientGraphQLError", | ||
"GraphQLClientGraphQLMultiError", | ||
"GraphQLClientHttpError", | ||
"GraphQlClientInvalidResponseError", | ||
"Test", | ||
] |
80 changes: 80 additions & 0 deletions
80
tests/main/custom_config_file/expected_client/async_base_client.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from typing import Any, Dict, Optional, TypeVar, cast | ||
|
||
import httpx | ||
from pydantic import BaseModel | ||
|
||
from .exceptions import ( | ||
GraphQLClientGraphQLMultiError, | ||
GraphQLClientHttpError, | ||
GraphQlClientInvalidResponseError, | ||
) | ||
|
||
Self = TypeVar("Self", bound="AsyncBaseClient") | ||
|
||
|
||
class AsyncBaseClient: | ||
def __init__( | ||
self, | ||
url: str = "", | ||
headers: Optional[Dict[str, str]] = None, | ||
http_client: Optional[httpx.AsyncClient] = None, | ||
) -> None: | ||
self.url = url | ||
self.headers = headers | ||
|
||
self.http_client = ( | ||
http_client if http_client else httpx.AsyncClient(headers=headers) | ||
) | ||
|
||
async def __aenter__(self: Self) -> Self: | ||
return self | ||
|
||
async def __aexit__( | ||
self, | ||
exc_type: object, | ||
exc_val: object, | ||
exc_tb: object, | ||
) -> None: | ||
await self.http_client.aclose() | ||
|
||
async def execute( | ||
self, query: str, variables: Optional[Dict[str, Any]] = None | ||
) -> httpx.Response: | ||
payload: Dict[str, Any] = {"query": query} | ||
if variables: | ||
payload["variables"] = self._convert_dict_to_json_serializable(variables) | ||
return await self.http_client.post(url=self.url, json=payload) | ||
|
||
def get_data(self, response: httpx.Response) -> dict[str, Any]: | ||
if not response.is_success: | ||
raise GraphQLClientHttpError( | ||
status_code=response.status_code, response=response | ||
) | ||
|
||
try: | ||
response_json = response.json() | ||
except ValueError as exc: | ||
raise GraphQlClientInvalidResponseError(response=response) from exc | ||
|
||
if (not isinstance(response_json, dict)) or ("data" not in response_json): | ||
raise GraphQlClientInvalidResponseError(response=response) | ||
|
||
data = response_json["data"] | ||
errors = response_json.get("errors") | ||
|
||
if errors: | ||
raise GraphQLClientGraphQLMultiError.from_errors_dicts( | ||
errors_dicts=errors, data=data | ||
) | ||
|
||
return cast(dict[str, Any], data) | ||
|
||
def _convert_dict_to_json_serializable( | ||
self, dict_: Dict[str, Any] | ||
) -> Dict[str, Any]: | ||
return { | ||
key: value | ||
if not isinstance(value, BaseModel) | ||
else value.dict(by_alias=True) | ||
for key, value in dict_.items() | ||
} |
30 changes: 30 additions & 0 deletions
30
tests/main/custom_config_file/expected_client/base_model.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from typing import Any, Dict | ||
|
||
from pydantic import BaseModel as PydanticBaseModel | ||
from pydantic.class_validators import validator | ||
from pydantic.fields import ModelField | ||
|
||
from .scalars import SCALARS_PARSE_FUNCTIONS, SCALARS_SERIALIZE_FUNCTIONS | ||
|
||
|
||
class BaseModel(PydanticBaseModel): | ||
class Config: | ||
allow_population_by_field_name = True | ||
validate_assignment = True | ||
arbitrary_types_allowed = True | ||
|
||
# pylint: disable=no-self-argument | ||
@validator("*", pre=True) | ||
def decode_custom_scalars(cls, value: Any, field: ModelField) -> Any: | ||
decode = SCALARS_PARSE_FUNCTIONS.get(field.type_) | ||
if decode and callable(decode): | ||
return decode(value) | ||
return value | ||
|
||
def dict(self, **kwargs: Any) -> Dict[str, Any]: | ||
dict_ = super().dict(**kwargs) | ||
for key, value in dict_.items(): | ||
serialize = SCALARS_SERIALIZE_FUNCTIONS.get(type(value)) | ||
if serialize and callable(serialize): | ||
dict_[key] = serialize(value) | ||
return dict_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from typing import Any, List, Optional | ||
|
||
from .async_base_client import AsyncBaseClient | ||
from .test import Test | ||
|
||
|
||
def gql(q: str) -> str: | ||
return q | ||
|
||
|
||
class Client(AsyncBaseClient): | ||
async def test(self) -> Test: | ||
query = gql( | ||
""" | ||
query test { | ||
testQuery | ||
} | ||
""" | ||
) | ||
variables: dict[str, object] = {} | ||
response = await self.execute(query=query, variables=variables) | ||
data = self.get_data(response) | ||
return Test.parse_obj(data) |
Empty file.
71 changes: 71 additions & 0 deletions
71
tests/main/custom_config_file/expected_client/exceptions.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from typing import Any, Dict, List, Optional | ||
|
||
import httpx | ||
|
||
|
||
class GraphQLClientError(Exception): | ||
"""Base exception.""" | ||
|
||
|
||
class GraphQLClientHttpError(GraphQLClientError): | ||
def __init__(self, status_code: int, response: httpx.Response) -> None: | ||
self.status_code = status_code | ||
self.response = response | ||
|
||
def __str__(self) -> str: | ||
return f"HTTP status code: {self.status_code}" | ||
|
||
|
||
class GraphQlClientInvalidResponseError(GraphQLClientError): | ||
def __init__(self, response: httpx.Response) -> None: | ||
self.response = response | ||
|
||
def __str__(self) -> str: | ||
return "Invalid response format." | ||
|
||
|
||
class GraphQLClientGraphQLError(GraphQLClientError): | ||
def __init__( | ||
self, | ||
message: str, | ||
locations: Optional[List[Dict[str, int]]] = None, | ||
path: Optional[List[str]] = None, | ||
extensions: Optional[Dict[str, object]] = None, | ||
orginal: Optional[Dict[str, object]] = None, | ||
): | ||
self.message = message | ||
self.locations = locations | ||
self.path = path | ||
self.extensions = extensions | ||
self.orginal = orginal | ||
|
||
def __str__(self) -> str: | ||
return self.message | ||
|
||
@classmethod | ||
def from_dict(cls, error: dict[str, Any]) -> "GraphQLClientGraphQLError": | ||
return cls( | ||
message=error["message"], | ||
locations=error.get("locations"), | ||
path=error.get("path"), | ||
extensions=error.get("extensions"), | ||
orginal=error, | ||
) | ||
|
||
|
||
class GraphQLClientGraphQLMultiError(GraphQLClientError): | ||
def __init__(self, errors: List[GraphQLClientGraphQLError], data: dict[str, Any]): | ||
self.errors = errors | ||
self.data = data | ||
|
||
def __str__(self) -> str: | ||
return "; ".join(str(e) for e in self.errors) | ||
|
||
@classmethod | ||
def from_errors_dicts( | ||
cls, errors_dicts: List[dict[str, Any]], data: dict[str, Any] | ||
) -> "GraphQLClientGraphQLMultiError": | ||
return cls( | ||
errors=[GraphQLClientGraphQLError.from_dict(e) for e in errors_dicts], | ||
data=data, | ||
) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from typing import Any, Callable, Dict | ||
|
||
SCALARS_PARSE_FUNCTIONS: Dict[Any, Callable[[str], Any]] = {} | ||
SCALARS_SERIALIZE_FUNCTIONS: Dict[Any, Callable[[Any], str]] = {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from pydantic import Field | ||
|
||
from .base_model import BaseModel | ||
|
||
|
||
class Test(BaseModel): | ||
test_query: str = Field(alias="testQuery") | ||
|
||
|
||
Test.update_forward_refs() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
query test { | ||
testQuery | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
schema { | ||
query: Query | ||
} | ||
|
||
type Query { | ||
testQuery: String! | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.