Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better handle list types #347

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions ariadne_codegen/client_generators/custom_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
GraphQLInputObjectType,
GraphQLInterfaceType,
GraphQLNonNull,
GraphQLList,
GraphQLObjectType,
GraphQLScalarType,
GraphQLUnionType,
Expand All @@ -23,6 +24,7 @@
generate_dict,
generate_import_from,
generate_keyword,
generate_list_annotation,
generate_name,
generate_subscript,
generate_tuple,
Expand Down Expand Up @@ -78,11 +80,12 @@ def generate_arguments(
for arg_name, arg_value in operation_args.items():
final_type = get_final_type(arg_value)
is_required = isinstance(arg_value.type, GraphQLNonNull)
is_list = isinstance(arg_value.type, GraphQLList)
name = process_name(
arg_name, convert_to_snake_case=self.convert_to_snake_case
)
annotation, used_custom_scalar = self._parse_graphql_type_name(
final_type, not is_required
final_type, not is_required, is_list,
)

self._accumulate_method_arguments(
Expand All @@ -95,6 +98,7 @@ def generate_arguments(
name,
final_type,
is_required,
is_list,
used_custom_scalar,
)

Expand Down Expand Up @@ -127,10 +131,12 @@ def _accumulate_return_arguments(
name: str,
final_type: Union[GraphQLObjectType, GraphQLInterfaceType, GraphQLUnionType],
is_required: bool,
is_list: bool,
used_custom_scalar: Optional[str],
) -> None:
"""Accumulates return arguments."""
constant_value = f"{final_type.name}!" if is_required else final_type.name
constant_value = f"[{final_type.name}]" if is_list else final_type.name
constant_value = f"{constant_value}!" if is_required else constant_value
return_arg_dict_value = self._generate_return_arg_value(
name, used_custom_scalar
)
Expand Down Expand Up @@ -178,8 +184,15 @@ def _parse_graphql_type_name(
self,
type_: Union[GraphQLScalarType, GraphQLInputObjectType, GraphQLEnumType],
nullable: bool = True,
is_list: bool = False,
) -> Tuple[Union[ast.Name, ast.Subscript], Optional[str]]:
"""Parses the GraphQL type name and determines if it is a custom scalar."""
if is_list:
self._parse_graphql_type_name(type_, nullable=nullable)
self._add_import(
generate_import_from(names=["List"], from_="typing", level=0)
)
return generate_list_annotation(ast.Name(id=type_.name), nullable), None
name = type_.name
used_custom_scalar = None
if isinstance(type_, GraphQLInputObjectType):
Expand Down