Skip to content

Commit f218e20

Browse files
authored
Merge pull request #116 from strue36/handle-top-level-lists
Handle top level lists
2 parents d7c764c + fcf020f commit f218e20

File tree

12 files changed

+142
-54
lines changed

12 files changed

+142
-54
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Changed logic how custom scalar imports are generated. Deprecated `import_` key.
66
- Added escaping of GraphQL names which are Python keywords by appending `_` to them.
7+
- Fixed parsing of list variables.
78

89

910
## 0.5.0 (2023-04-05)

ariadne_codegen/client_generators/dependencies/async_base_client.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ def get_data(self, response: httpx.Response) -> dict[str, Any]:
6969

7070
return cast(dict[str, Any], data)
7171

72+
def _convert_value(self, value: Any) -> Any:
73+
if isinstance(value, BaseModel):
74+
return value.dict(by_alias=True)
75+
if isinstance(value, list):
76+
return [self._convert_value(item) for item in value]
77+
return value
78+
7279
def _convert_dict_to_json_serializable(
7380
self, dict_: Dict[str, Any]
7481
) -> Dict[str, Any]:
75-
return {
76-
key: value
77-
if not isinstance(value, BaseModel)
78-
else value.dict(by_alias=True)
79-
for key, value in dict_.items()
80-
}
82+
return {key: self._convert_value(value) for key, value in dict_.items()}

ariadne_codegen/client_generators/dependencies/base_client.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,14 @@ def get_data(self, response: httpx.Response) -> dict[str, Any]:
6767

6868
return cast(dict[str, Any], data)
6969

70+
def _convert_value(self, value: Any) -> Any:
71+
if isinstance(value, BaseModel):
72+
return value.dict(by_alias=True)
73+
if isinstance(value, list):
74+
return [self._convert_value(item) for item in value]
75+
return value
76+
7077
def _convert_dict_to_json_serializable(
7178
self, dict_: Dict[str, Any]
7279
) -> Dict[str, Any]:
73-
return {
74-
key: value
75-
if not isinstance(value, BaseModel)
76-
else value.dict(by_alias=True)
77-
for key, value in dict_.items()
78-
}
80+
return {key: self._convert_value(value) for key, value in dict_.items()}

tests/client_generators/dependencies/test_async_base_client.py

+35
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,41 @@ class TestModel2(BaseModel):
6767
}
6868

6969

70+
@pytest.mark.asyncio
71+
async def test_execute_correctly_parses_top_level_list_variables(mocker):
72+
class TestModel1(BaseModel):
73+
a: int
74+
75+
fake_client = mocker.AsyncMock()
76+
client = AsyncBaseClient(url="base_url", http_client=fake_client)
77+
query_str = """
78+
query Abc($v1: [[TestModel1!]!]!) {
79+
abc(v1: $v1){
80+
field1
81+
}
82+
}
83+
"""
84+
85+
await client.execute(
86+
query_str,
87+
{
88+
"v1": [[TestModel1(a=1), TestModel1(a=2)]],
89+
},
90+
)
91+
92+
assert fake_client.post.called
93+
assert len(fake_client.post.mock_calls) == 1
94+
call_kwargs = fake_client.post.mock_calls[0].kwargs
95+
assert call_kwargs["url"] == "base_url"
96+
assert call_kwargs["json"] == {
97+
"query": query_str,
98+
"variables": {"v1": [[{"a": 1}, {"a": 2}]]},
99+
}
100+
assert not any(
101+
isinstance(x, BaseModel) for x in call_kwargs["json"]["variables"]["v1"][0]
102+
)
103+
104+
70105
@pytest.mark.parametrize(
71106
"status_code, response_content",
72107
[

tests/client_generators/dependencies/test_base_client.py

+34
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,40 @@ class TestModel2(BaseModel):
6363
}
6464

6565

66+
def test_execute_correctly_parses_top_level_list_variables(mocker):
67+
class TestModel1(BaseModel):
68+
a: int
69+
70+
fake_client = mocker.MagicMock()
71+
client = BaseClient(url="base_url", http_client=fake_client)
72+
query_str = """
73+
query Abc($v1: [[TestModel1!]!]!) {
74+
abc(v1: $v1){
75+
field1
76+
}
77+
}
78+
"""
79+
80+
client.execute(
81+
query_str,
82+
{
83+
"v1": [[TestModel1(a=1), TestModel1(a=2)]],
84+
},
85+
)
86+
87+
assert fake_client.post.called
88+
assert len(fake_client.post.mock_calls) == 1
89+
call_kwargs = fake_client.post.mock_calls[0].kwargs
90+
assert call_kwargs["url"] == "base_url"
91+
assert call_kwargs["json"] == {
92+
"query": query_str,
93+
"variables": {"v1": [[{"a": 1}, {"a": 2}]]},
94+
}
95+
assert not any(
96+
isinstance(x, BaseModel) for x in call_kwargs["json"]["variables"]["v1"][0]
97+
)
98+
99+
66100
@pytest.mark.parametrize(
67101
"status_code, response_content",
68102
[

tests/main/clients/custom_config_file/expected_client/async_base_client.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ def get_data(self, response: httpx.Response) -> dict[str, Any]:
6969

7070
return cast(dict[str, Any], data)
7171

72+
def _convert_value(self, value: Any) -> Any:
73+
if isinstance(value, BaseModel):
74+
return value.dict(by_alias=True)
75+
if isinstance(value, list):
76+
return [self._convert_value(item) for item in value]
77+
return value
78+
7279
def _convert_dict_to_json_serializable(
7380
self, dict_: Dict[str, Any]
7481
) -> Dict[str, Any]:
75-
return {
76-
key: value
77-
if not isinstance(value, BaseModel)
78-
else value.dict(by_alias=True)
79-
for key, value in dict_.items()
80-
}
82+
return {key: self._convert_value(value) for key, value in dict_.items()}

tests/main/clients/custom_files_names/expected_client/async_base_client.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ def get_data(self, response: httpx.Response) -> dict[str, Any]:
6969

7070
return cast(dict[str, Any], data)
7171

72+
def _convert_value(self, value: Any) -> Any:
73+
if isinstance(value, BaseModel):
74+
return value.dict(by_alias=True)
75+
if isinstance(value, list):
76+
return [self._convert_value(item) for item in value]
77+
return value
78+
7279
def _convert_dict_to_json_serializable(
7380
self, dict_: Dict[str, Any]
7481
) -> Dict[str, Any]:
75-
return {
76-
key: value
77-
if not isinstance(value, BaseModel)
78-
else value.dict(by_alias=True)
79-
for key, value in dict_.items()
80-
}
82+
return {key: self._convert_value(value) for key, value in dict_.items()}

tests/main/clients/custom_scalars/expected_client/async_base_client.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ def get_data(self, response: httpx.Response) -> dict[str, Any]:
6969

7070
return cast(dict[str, Any], data)
7171

72+
def _convert_value(self, value: Any) -> Any:
73+
if isinstance(value, BaseModel):
74+
return value.dict(by_alias=True)
75+
if isinstance(value, list):
76+
return [self._convert_value(item) for item in value]
77+
return value
78+
7279
def _convert_dict_to_json_serializable(
7380
self, dict_: Dict[str, Any]
7481
) -> Dict[str, Any]:
75-
return {
76-
key: value
77-
if not isinstance(value, BaseModel)
78-
else value.dict(by_alias=True)
79-
for key, value in dict_.items()
80-
}
82+
return {key: self._convert_value(value) for key, value in dict_.items()}

tests/main/clients/example/expected_client/async_base_client.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ def get_data(self, response: httpx.Response) -> dict[str, Any]:
6969

7070
return cast(dict[str, Any], data)
7171

72+
def _convert_value(self, value: Any) -> Any:
73+
if isinstance(value, BaseModel):
74+
return value.dict(by_alias=True)
75+
if isinstance(value, list):
76+
return [self._convert_value(item) for item in value]
77+
return value
78+
7279
def _convert_dict_to_json_serializable(
7380
self, dict_: Dict[str, Any]
7481
) -> Dict[str, Any]:
75-
return {
76-
key: value
77-
if not isinstance(value, BaseModel)
78-
else value.dict(by_alias=True)
79-
for key, value in dict_.items()
80-
}
82+
return {key: self._convert_value(value) for key, value in dict_.items()}

tests/main/clients/extended_models/expected_client/async_base_client.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ def get_data(self, response: httpx.Response) -> dict[str, Any]:
6969

7070
return cast(dict[str, Any], data)
7171

72+
def _convert_value(self, value: Any) -> Any:
73+
if isinstance(value, BaseModel):
74+
return value.dict(by_alias=True)
75+
if isinstance(value, list):
76+
return [self._convert_value(item) for item in value]
77+
return value
78+
7279
def _convert_dict_to_json_serializable(
7380
self, dict_: Dict[str, Any]
7481
) -> Dict[str, Any]:
75-
return {
76-
key: value
77-
if not isinstance(value, BaseModel)
78-
else value.dict(by_alias=True)
79-
for key, value in dict_.items()
80-
}
82+
return {key: self._convert_value(value) for key, value in dict_.items()}

tests/main/clients/inline_fragments/expected_client/async_base_client.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ def get_data(self, response: httpx.Response) -> dict[str, Any]:
6969

7070
return cast(dict[str, Any], data)
7171

72+
def _convert_value(self, value: Any) -> Any:
73+
if isinstance(value, BaseModel):
74+
return value.dict(by_alias=True)
75+
if isinstance(value, list):
76+
return [self._convert_value(item) for item in value]
77+
return value
78+
7279
def _convert_dict_to_json_serializable(
7380
self, dict_: Dict[str, Any]
7481
) -> Dict[str, Any]:
75-
return {
76-
key: value
77-
if not isinstance(value, BaseModel)
78-
else value.dict(by_alias=True)
79-
for key, value in dict_.items()
80-
}
82+
return {key: self._convert_value(value) for key, value in dict_.items()}

tests/main/clients/remote_schema/expected_client/async_base_client.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ def get_data(self, response: httpx.Response) -> dict[str, Any]:
6969

7070
return cast(dict[str, Any], data)
7171

72+
def _convert_value(self, value: Any) -> Any:
73+
if isinstance(value, BaseModel):
74+
return value.dict(by_alias=True)
75+
if isinstance(value, list):
76+
return [self._convert_value(item) for item in value]
77+
return value
78+
7279
def _convert_dict_to_json_serializable(
7380
self, dict_: Dict[str, Any]
7481
) -> Dict[str, Any]:
75-
return {
76-
key: value
77-
if not isinstance(value, BaseModel)
78-
else value.dict(by_alias=True)
79-
for key, value in dict_.items()
80-
}
82+
return {key: self._convert_value(value) for key, value in dict_.items()}

0 commit comments

Comments
 (0)