Skip to content

Commit be193fd

Browse files
Refactor code to meet 3.10 3.9 python needs
1 parent 5334e00 commit be193fd

File tree

6 files changed

+58
-48
lines changed

6 files changed

+58
-48
lines changed

ariadne_codegen/client_generators/client.py

+8-16
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,9 @@ def create_combine_variables_method(self):
278278
lineno=1,
279279
),
280280
generate_return(
281-
value=generate_tuple(
282-
elts=[
281+
value=generate_dict(
282+
keys=[generate_constant("types"), generate_constant("values")],
283+
values=[
283284
generate_name("variables_types_combined"),
284285
generate_name("processed_variables_combined"),
285286
],
@@ -306,13 +307,10 @@ def create_combine_variables_method(self):
306307
)
307308

308309
returns = generate_subscript(
309-
generate_name(TUPLE),
310+
generate_name(DICT),
310311
generate_tuple(
311312
[
312-
generate_subscript(
313-
generate_name(DICT),
314-
generate_tuple([generate_name("str"), generate_name("Any")]),
315-
),
313+
generate_name("str"),
316314
generate_subscript(
317315
generate_name(DICT),
318316
generate_tuple([generate_name("str"), generate_name("Any")]),
@@ -508,8 +506,6 @@ def create_build_operation_ast_method(self):
508506
)
509507

510508
def create_execute_custom_operation_method(self):
511-
variables_types_combined = generate_name("variables_types_combined")
512-
processed_variables_combined = generate_name("processed_variables_combined")
513509
method_body = [
514510
generate_assign(
515511
targets=["selections"],
@@ -521,11 +517,7 @@ def create_execute_custom_operation_method(self):
521517
),
522518
),
523519
ast.Assign(
524-
targets=[
525-
generate_tuple(
526-
elts=[variables_types_combined, processed_variables_combined],
527-
)
528-
],
520+
targets=[generate_name("combined_variables")],
529521
value=generate_call(
530522
func=generate_attribute(
531523
value=generate_name("self"), attr="_combine_variables"
@@ -540,7 +532,7 @@ def create_execute_custom_operation_method(self):
540532
func=generate_attribute(
541533
value=generate_name("self"), attr="_build_variable_definitions"
542534
),
543-
args=[generate_name("variables_types_combined")],
535+
args=[generate_name('combined_variables["types"]')],
544536
),
545537
),
546538
generate_assign(
@@ -574,7 +566,7 @@ def create_execute_custom_operation_method(self):
574566
keywords=[
575567
generate_keyword(
576568
arg="variables",
577-
value=generate_name("processed_variables_combined"),
569+
value=generate_name('combined_variables["values"]'),
578570
),
579571
generate_keyword(
580572
arg="operation_name",

tests/main/clients/custom_query_builder/expected_client/client.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,23 @@ async def execute_custom_operation(
2626
self, *fields: GraphQLField, operation_type: OperationType, operation_name: str
2727
) -> Dict[str, Any]:
2828
selections = self._build_selection_set(fields)
29-
variables_types_combined, processed_variables_combined = (
30-
self._combine_variables(fields)
31-
)
29+
combined_variables = self._combine_variables(fields)
3230
variable_definitions = self._build_variable_definitions(
33-
variables_types_combined
31+
combined_variables["types"]
3432
)
3533
operation_ast = self._build_operation_ast(
3634
selections, operation_type, operation_name, variable_definitions
3735
)
3836
response = await self.execute(
3937
print_ast(operation_ast),
40-
variables=processed_variables_combined,
38+
variables=combined_variables["values"],
4139
operation_name=operation_name,
4240
)
4341
return self.get_data(response)
4442

4543
def _combine_variables(
4644
self, fields: Tuple[GraphQLField, ...]
47-
) -> Tuple[Dict[str, Any], Dict[str, Any]]:
45+
) -> Dict[str, Dict[str, Any]]:
4846
variables_types_combined = {}
4947
processed_variables_combined = {}
5048
for field in fields:
@@ -55,7 +53,10 @@ def _combine_variables(
5553
processed_variables_combined.update(
5654
{k: v["value"] for k, v in formatted_variables.items()}
5755
)
58-
return (variables_types_combined, processed_variables_combined)
56+
return {
57+
"types": variables_types_combined,
58+
"values": processed_variables_combined,
59+
}
5960

6061
def _build_variable_definitions(
6162
self, variables_types_combined: Dict[str, str]

tests/main/custom_operation_builder/graphql_client/client.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,23 @@ async def execute_custom_operation(
2626
self, *fields: GraphQLField, operation_type: OperationType, operation_name: str
2727
) -> Dict[str, Any]:
2828
selections = self._build_selection_set(fields)
29-
variables_types_combined, processed_variables_combined = (
30-
self._combine_variables(fields)
31-
)
29+
combined_variables = self._combine_variables(fields)
3230
variable_definitions = self._build_variable_definitions(
33-
variables_types_combined
31+
combined_variables["types"]
3432
)
3533
operation_ast = self._build_operation_ast(
3634
selections, operation_type, operation_name, variable_definitions
3735
)
3836
response = await self.execute(
3937
print_ast(operation_ast),
40-
variables=processed_variables_combined,
38+
variables=combined_variables["values"],
4139
operation_name=operation_name,
4240
)
4341
return self.get_data(response)
4442

4543
def _combine_variables(
4644
self, fields: Tuple[GraphQLField, ...]
47-
) -> Tuple[Dict[str, Any], Dict[str, Any]]:
45+
) -> Dict[str, Dict[str, Any]]:
4846
variables_types_combined = {}
4947
processed_variables_combined = {}
5048
for field in fields:
@@ -55,7 +53,10 @@ def _combine_variables(
5553
processed_variables_combined.update(
5654
{k: v["value"] for k, v in formatted_variables.items()}
5755
)
58-
return (variables_types_combined, processed_variables_combined)
56+
return {
57+
"types": variables_types_combined,
58+
"values": processed_variables_combined,
59+
}
5960

6061
def _build_variable_definitions(
6162
self, variables_types_combined: Dict[str, str]
@@ -86,7 +87,9 @@ def _build_operation_ast(
8687
]
8788
)
8889

89-
def _build_selection_set(self, fields: List[GraphQLField]) -> List[SelectionNode]:
90+
def _build_selection_set(
91+
self, fields: Tuple[GraphQLField, ...]
92+
) -> List[SelectionNode]:
9093
return [field.to_ast(idx) for idx, field in enumerate(fields)]
9194

9295
async def query(self, *fields: GraphQLField, operation_name: str) -> Dict[str, Any]:

tests/main/custom_operation_builder/graphql_client/custom_fields.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Union
1+
from typing import Any, Optional, Union
22

33
from . import (
44
AdminGraphQLField,

tests/main/custom_operation_builder/graphql_client/custom_mutations.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional
1+
from typing import Any, Dict, Optional
22

33
from .custom_fields import PostFields, UserFields
44
from .input_types import AddUserInput, UpdateUserInput
@@ -7,15 +7,17 @@
77
class Mutation:
88
@classmethod
99
def add_user(cls, user_input: AddUserInput) -> UserFields:
10-
arguments = {"user_input": {"type": "AddUserInput!", "value": user_input}}
10+
arguments: Dict[str, Dict[str, Any]] = {
11+
"user_input": {"type": "AddUserInput!", "value": user_input}
12+
}
1113
cleared_arguments = {
1214
key: value for key, value in arguments.items() if value["value"] is not None
1315
}
1416
return UserFields(field_name="addUser", arguments=cleared_arguments)
1517

1618
@classmethod
1719
def update_user(cls, user_id: str, user_input: UpdateUserInput) -> UserFields:
18-
arguments = {
20+
arguments: Dict[str, Dict[str, Any]] = {
1921
"user_id": {"type": "ID!", "value": user_id},
2022
"user_input": {"type": "UpdateUserInput!", "value": user_input},
2123
}
@@ -26,7 +28,9 @@ def update_user(cls, user_id: str, user_input: UpdateUserInput) -> UserFields:
2628

2729
@classmethod
2830
def delete_user(cls, user_id: str) -> UserFields:
29-
arguments = {"user_id": {"type": "ID!", "value": user_id}}
31+
arguments: Dict[str, Dict[str, Any]] = {
32+
"user_id": {"type": "ID!", "value": user_id}
33+
}
3034
cleared_arguments = {
3135
key: value for key, value in arguments.items() if value["value"] is not None
3236
}
@@ -36,7 +40,7 @@ def delete_user(cls, user_id: str) -> UserFields:
3640
def add_post(
3741
cls, title: str, content: str, author_id: str, published_at: str
3842
) -> PostFields:
39-
arguments = {
43+
arguments: Dict[str, Dict[str, Any]] = {
4044
"title": {"type": "String!", "value": title},
4145
"content": {"type": "String!", "value": content},
4246
"authorId": {"type": "ID!", "value": author_id},
@@ -56,7 +60,7 @@ def update_post(
5660
content: Optional[str] = None,
5761
published_at: Optional[str] = None
5862
) -> PostFields:
59-
arguments = {
63+
arguments: Dict[str, Dict[str, Any]] = {
6064
"post_id": {"type": "ID!", "value": post_id},
6165
"title": {"type": "String", "value": title},
6266
"content": {"type": "String", "value": content},
@@ -69,7 +73,9 @@ def update_post(
6973

7074
@classmethod
7175
def delete_post(cls, post_id: str) -> PostFields:
72-
arguments = {"post_id": {"type": "ID!", "value": post_id}}
76+
arguments: Dict[str, Dict[str, Any]] = {
77+
"post_id": {"type": "ID!", "value": post_id}
78+
}
7379
cleared_arguments = {
7480
key: value for key, value in arguments.items() if value["value"] is not None
7581
}

tests/main/custom_operation_builder/graphql_client/custom_queries.py

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional
1+
from typing import Any, Dict, Optional
22

33
from .custom_fields import PersonInterfaceInterface, PostFields, UserFields
44
from .custom_typing_fields import GraphQLField, SearchResultUnion
@@ -7,55 +7,63 @@
77
class Query:
88
@classmethod
99
def hello(cls) -> GraphQLField:
10-
arguments = {}
10+
arguments: Dict[str, Dict[str, Any]] = {}
1111
cleared_arguments = {
1212
key: value for key, value in arguments.items() if value["value"] is not None
1313
}
1414
return GraphQLField(field_name="hello", arguments=cleared_arguments)
1515

1616
@classmethod
1717
def greeting(cls, *, name: Optional[str] = None) -> GraphQLField:
18-
arguments = {"name": {"type": "String", "value": name}}
18+
arguments: Dict[str, Dict[str, Any]] = {
19+
"name": {"type": "String", "value": name}
20+
}
1921
cleared_arguments = {
2022
key: value for key, value in arguments.items() if value["value"] is not None
2123
}
2224
return GraphQLField(field_name="greeting", arguments=cleared_arguments)
2325

2426
@classmethod
2527
def user(cls, user_id: str) -> UserFields:
26-
arguments = {"user_id": {"type": "ID!", "value": user_id}}
28+
arguments: Dict[str, Dict[str, Any]] = {
29+
"user_id": {"type": "ID!", "value": user_id}
30+
}
2731
cleared_arguments = {
2832
key: value for key, value in arguments.items() if value["value"] is not None
2933
}
3034
return UserFields(field_name="user", arguments=cleared_arguments)
3135

3236
@classmethod
3337
def users(cls) -> UserFields:
34-
arguments = {}
38+
arguments: Dict[str, Dict[str, Any]] = {}
3539
cleared_arguments = {
3640
key: value for key, value in arguments.items() if value["value"] is not None
3741
}
3842
return UserFields(field_name="users", arguments=cleared_arguments)
3943

4044
@classmethod
4145
def search(cls, text: str) -> SearchResultUnion:
42-
arguments = {"text": {"type": "String!", "value": text}}
46+
arguments: Dict[str, Dict[str, Any]] = {
47+
"text": {"type": "String!", "value": text}
48+
}
4349
cleared_arguments = {
4450
key: value for key, value in arguments.items() if value["value"] is not None
4551
}
4652
return SearchResultUnion(field_name="search", arguments=cleared_arguments)
4753

4854
@classmethod
4955
def posts(cls) -> PostFields:
50-
arguments = {}
56+
arguments: Dict[str, Dict[str, Any]] = {}
5157
cleared_arguments = {
5258
key: value for key, value in arguments.items() if value["value"] is not None
5359
}
5460
return PostFields(field_name="posts", arguments=cleared_arguments)
5561

5662
@classmethod
5763
def person(cls, person_id: str) -> PersonInterfaceInterface:
58-
arguments = {"person_id": {"type": "ID!", "value": person_id}}
64+
arguments: Dict[str, Dict[str, Any]] = {
65+
"person_id": {"type": "ID!", "value": person_id}
66+
}
5967
cleared_arguments = {
6068
key: value for key, value in arguments.items() if value["value"] is not None
6169
}
@@ -65,7 +73,7 @@ def person(cls, person_id: str) -> PersonInterfaceInterface:
6573

6674
@classmethod
6775
def people(cls) -> PersonInterfaceInterface:
68-
arguments = {}
76+
arguments: Dict[str, Dict[str, Any]] = {}
6977
cleared_arguments = {
7078
key: value for key, value in arguments.items() if value["value"] is not None
7179
}

0 commit comments

Comments
 (0)