Skip to content

Commit 137608c

Browse files
support async_client=False with custom operations
* support async_client with custom operations * Update changelog
1 parent d311712 commit 137608c

18 files changed

+1522
-42
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 0.14.1 (UNRELEASED)
44

55
- Changed code typing to satisfy MyPy 1.11.0 version
6+
- Added support for `async_client=false` to work with `enable_custom_operations=true`
67

78

89
## 0.14.0 (2024-07-17)

ariadne_codegen/client_generators/client.py

+93-39
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,28 @@ def create_build_operation_ast_method(self):
505505
return_type=generate_name("DocumentNode"),
506506
)
507507

508-
def create_execute_custom_operation_method(self):
508+
def create_execute_custom_operation_method(self, async_client: bool):
509+
execute_call = generate_call(
510+
func=generate_attribute(value=generate_name("self"), attr="execute"),
511+
args=[
512+
generate_call(
513+
func=generate_name("print_ast"),
514+
args=[generate_name("operation_ast")],
515+
)
516+
],
517+
keywords=[
518+
generate_keyword(
519+
arg="variables", value=generate_name('combined_variables["values"]')
520+
),
521+
generate_keyword(
522+
arg="operation_name", value=generate_name("operation_name")
523+
),
524+
],
525+
)
526+
response_value = (
527+
generate_await(value=execute_call) if async_client else execute_call
528+
)
529+
509530
method_body = [
510531
generate_assign(
511532
targets=["selections"],
@@ -549,54 +570,31 @@ def create_execute_custom_operation_method(self):
549570
],
550571
),
551572
),
552-
generate_assign(
553-
targets=["response"],
554-
value=generate_await(
555-
value=generate_call(
556-
func=generate_attribute(
557-
value=generate_name("self"),
558-
attr="execute",
559-
),
560-
args=[
561-
generate_call(
562-
func=generate_name("print_ast"),
563-
args=[generate_name("operation_ast")],
564-
)
565-
],
566-
keywords=[
567-
generate_keyword(
568-
arg="variables",
569-
value=generate_name('combined_variables["values"]'),
570-
),
571-
generate_keyword(
572-
arg="operation_name",
573-
value=generate_name("operation_name"),
574-
),
575-
],
576-
)
577-
),
578-
),
573+
generate_assign(targets=["response"], value=response_value),
579574
generate_return(
580575
value=generate_call(
581576
func=generate_attribute(
582-
value=generate_name("self"),
583-
attr="get_data",
577+
value=generate_name("self"), attr="get_data"
584578
),
585579
args=[generate_name("response")],
586580
)
587581
),
588582
]
589-
return generate_async_method_definition(
583+
584+
method_definition = (
585+
generate_async_method_definition
586+
if async_client
587+
else generate_method_definition
588+
)
589+
590+
return method_definition(
590591
name="execute_custom_operation",
591592
arguments=generate_arguments(
592593
args=[
593594
generate_arg("self"),
594595
generate_arg("*fields", annotation=generate_name("GraphQLField")),
595596
generate_arg(
596-
"operation_type",
597-
annotation=generate_name(
598-
"OperationType",
599-
),
597+
"operation_type", annotation=generate_name("OperationType")
600598
),
601599
generate_arg("operation_name", annotation=generate_name("str")),
602600
]
@@ -655,7 +653,7 @@ def create_build_selection_set(self):
655653
),
656654
)
657655

658-
def add_execute_custom_operation_method(self):
656+
def add_execute_custom_operation_method(self, async_client: bool):
659657
self._add_import(
660658
generate_import_from(
661659
[
@@ -679,13 +677,20 @@ def add_execute_custom_operation_method(self):
679677
)
680678
self._add_import(generate_import_from([DICT, TUPLE, LIST, ANY], "typing"))
681679

682-
self._class_def.body.append(self.create_execute_custom_operation_method())
680+
self._class_def.body.append(
681+
self.create_execute_custom_operation_method(async_client)
682+
)
683683
self._class_def.body.append(self.create_combine_variables_method())
684684
self._class_def.body.append(self.create_build_variable_definitions_method())
685685
self._class_def.body.append(self.create_build_operation_ast_method())
686686
self._class_def.body.append(self.create_build_selection_set())
687687

688-
def create_custom_operation_method(self, name, operation_type):
688+
def create_custom_operation_method(
689+
self,
690+
name: str,
691+
operation_type: str,
692+
async_client: bool,
693+
):
689694
self._add_import(
690695
generate_import_from(
691696
[
@@ -694,6 +699,55 @@ def create_custom_operation_method(self, name, operation_type):
694699
GRAPHQL_MODULE,
695700
)
696701
)
702+
if async_client:
703+
def_query = self._create_async_operation_method(name, operation_type)
704+
else:
705+
def_query = self._create_sync_operation_method(name, operation_type)
706+
self._class_def.body.append(def_query)
707+
708+
def _create_sync_operation_method(self, name: str, operation_type: str):
709+
body_return = generate_return(
710+
value=generate_call(
711+
func=generate_attribute(
712+
value=generate_name("self"),
713+
attr="execute_custom_operation",
714+
),
715+
args=[
716+
generate_name("*fields"),
717+
],
718+
keywords=[
719+
generate_keyword(
720+
arg="operation_type",
721+
value=generate_attribute(
722+
value=generate_name("OperationType"),
723+
attr=operation_type,
724+
),
725+
),
726+
generate_keyword(
727+
arg="operation_name", value=generate_name("operation_name")
728+
),
729+
],
730+
)
731+
)
732+
733+
def_query = generate_method_definition(
734+
name=name,
735+
arguments=generate_arguments(
736+
args=[
737+
generate_arg("self"),
738+
generate_arg("*fields", annotation=generate_name("GraphQLField")),
739+
generate_arg("operation_name", annotation=generate_name("str")),
740+
],
741+
),
742+
body=[body_return],
743+
return_type=generate_subscript(
744+
generate_name(DICT),
745+
generate_tuple([generate_name("str"), generate_name("Any")]),
746+
),
747+
)
748+
return def_query
749+
750+
def _create_async_operation_method(self, name: str, operation_type: str):
697751
body_return = generate_return(
698752
value=generate_await(
699753
value=generate_call(
@@ -734,7 +788,7 @@ def create_custom_operation_method(self, name, operation_type):
734788
generate_tuple([generate_name("str"), generate_name("Any")]),
735789
),
736790
)
737-
self._class_def.body.append(async_def_query)
791+
return async_def_query
738792

739793
def get_variable_names(self, arguments: ast.arguments) -> Dict[str, str]:
740794
mapped_variable_names = [

ariadne_codegen/client_generators/package.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,16 @@ def generate(self) -> List[str]:
156156
if self.enable_custom_operations:
157157
self._generate_custom_fields_typing()
158158
self._generate_custom_fields()
159-
self.client_generator.add_execute_custom_operation_method()
159+
self.client_generator.add_execute_custom_operation_method(self.async_client)
160160
if self.custom_query_generator:
161161
self._generate_custom_queries()
162162
self.client_generator.create_custom_operation_method(
163-
"query", OperationType.QUERY.value.upper()
163+
"query", OperationType.QUERY.value.upper(), self.async_client
164164
)
165165
if self.custom_mutation_generator:
166166
self._generate_custom_mutations()
167167
self.client_generator.create_custom_operation_method(
168-
"mutation", OperationType.MUTATION.value.upper()
168+
"mutation", OperationType.MUTATION.value.upper(), self.async_client
169169
)
170170

171171
self._generate_client()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from .base_client import BaseClient
2+
from .base_model import BaseModel, Upload
3+
from .client import Client
4+
from .enums import MetadataErrorCode
5+
from .exceptions import (
6+
GraphQLClientError,
7+
GraphQLClientGraphQLError,
8+
GraphQLClientGraphQLMultiError,
9+
GraphQLClientHttpError,
10+
GraphQLClientInvalidResponseError,
11+
)
12+
13+
__all__ = [
14+
"BaseClient",
15+
"BaseModel",
16+
"Client",
17+
"GraphQLClientError",
18+
"GraphQLClientGraphQLError",
19+
"GraphQLClientGraphQLMultiError",
20+
"GraphQLClientHttpError",
21+
"GraphQLClientInvalidResponseError",
22+
"MetadataErrorCode",
23+
"Upload",
24+
]

0 commit comments

Comments
 (0)