1
1
import ast
2
+ import enum
2
3
from typing import Any , Dict , List , Optional , Tuple , Union , cast
3
4
4
5
from graphql import (
9
10
GraphQLObjectType ,
10
11
GraphQLScalarType ,
11
12
GraphQLUnionType ,
13
+ GraphQLList ,
12
14
)
13
15
14
16
from ..codegen import (
26
28
generate_name ,
27
29
generate_subscript ,
28
30
generate_tuple ,
31
+ generate_list_annotation ,
29
32
)
30
33
from ..exceptions import ParsingError
31
34
from ..plugins .manager import PluginManager
@@ -77,12 +80,13 @@ def generate_arguments(
77
80
78
81
for arg_name , arg_value in operation_args .items ():
79
82
final_type = get_final_type (arg_value )
83
+ is_list = isinstance (arg_value .type , GraphQLList )
80
84
is_required = isinstance (arg_value .type , GraphQLNonNull )
81
85
name = process_name (
82
86
arg_name , convert_to_snake_case = self .convert_to_snake_case
83
87
)
84
88
annotation , used_custom_scalar = self ._parse_graphql_type_name (
85
- final_type , not is_required
89
+ final_type , not is_required , is_list
86
90
)
87
91
88
92
self ._accumulate_method_arguments (
@@ -93,8 +97,7 @@ def generate_arguments(
93
97
return_arguments_values ,
94
98
arg_name ,
95
99
name ,
96
- final_type ,
97
- is_required ,
100
+ arg_value .type ,
98
101
used_custom_scalar ,
99
102
)
100
103
@@ -125,12 +128,12 @@ def _accumulate_return_arguments(
125
128
return_arguments_values : List [ast .expr ],
126
129
arg_name : str ,
127
130
name : str ,
128
- final_type : Union [GraphQLObjectType , GraphQLInterfaceType , GraphQLUnionType ],
129
- is_required : bool ,
131
+ complete_type : Union [GraphQLObjectType , GraphQLInterfaceType , GraphQLUnionType , GraphQLNonNull , GraphQLList ],
130
132
used_custom_scalar : Optional [str ],
131
133
) -> None :
132
134
"""Accumulates return arguments."""
133
- constant_value = f"{ final_type .name } !" if is_required else final_type .name
135
+ constant_value = self ._generate_complete_type_name (complete_type )
136
+
134
137
return_arg_dict_value = self ._generate_return_arg_value (
135
138
name , used_custom_scalar
136
139
)
@@ -143,6 +146,22 @@ def _accumulate_return_arguments(
143
146
)
144
147
)
145
148
149
+ def _generate_complete_type_name (
150
+ self ,
151
+ complete_type : Union [GraphQLObjectType , GraphQLInterfaceType , GraphQLUnionType , GraphQLNonNull , GraphQLList ]
152
+ ) -> str :
153
+ if isinstance (complete_type , GraphQLNonNull ):
154
+ if hasattr (complete_type , "of_type" ):
155
+ return f"{ self ._generate_complete_type_name (complete_type .of_type )} !"
156
+ else :
157
+ return f"{ self ._generate_complete_type_name (complete_type .type )} "
158
+ if isinstance (complete_type , GraphQLList ):
159
+ if hasattr (complete_type , "of_type" ):
160
+ return f"[{ self ._generate_complete_type_name (complete_type .of_type )} ]"
161
+ else :
162
+ return f"[{ self ._generate_complete_type_name (complete_type .type )} ]"
163
+ return complete_type .name
164
+
146
165
def _generate_return_arg_value (
147
166
self , name : str , used_custom_scalar : Optional [str ]
148
167
) -> Union [ast .Call , ast .Name ]:
@@ -178,6 +197,7 @@ def _parse_graphql_type_name(
178
197
self ,
179
198
type_ : Union [GraphQLScalarType , GraphQLInputObjectType , GraphQLEnumType ],
180
199
nullable : bool = True ,
200
+ is_list : bool = False ,
181
201
) -> Tuple [Union [ast .Name , ast .Subscript ], Optional [str ]]:
182
202
"""Parses the GraphQL type name and determines if it is a custom scalar."""
183
203
name = type_ .name
@@ -205,7 +225,8 @@ def _parse_graphql_type_name(
205
225
self ._used_custom_scalars .append (used_custom_scalar )
206
226
else :
207
227
raise ParsingError (f"Incorrect argument type { name } " )
208
- return generate_annotation_name (name , nullable ), used_custom_scalar
228
+ return generate_annotation_name (name , nullable ) if not is_list else generate_list_annotation (
229
+ generate_annotation_name (name , nullable = False ), nullable ), used_custom_scalar
209
230
210
231
def add_custom_scalar_imports (self ) -> None :
211
232
"""Adds imports for custom scalars used in the schema."""
0 commit comments