Skip to content

Commit

Permalink
fix: only recursively get fields if element type is custom type
Browse files Browse the repository at this point in the history
  • Loading branch information
mammothb committed Feb 1, 2025
1 parent 22ec204 commit ff2f8ad
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 2 additions & 2 deletions graphql_query/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def _get_field_template(field_info: PydanticFieldInfo) -> Field:
return Field(name="<NAME>", fields=[], alias=alias, arguments=arguments, directives=directives, typename=typename)


def _get_fields(model: Type['GraphQLQueryBaseModel']) -> List[Union[str, Field, InlineFragment, Fragment]]:
def _get_fields(model: Type["GraphQLQueryBaseModel"]) -> List[Union[str, Field, InlineFragment, Fragment]]:
fields: List[Union[str, Field, InlineFragment, Fragment]] = []

for f_name, f in model.model_fields.items():
Expand Down Expand Up @@ -50,7 +50,7 @@ def _get_fields(model: Type['GraphQLQueryBaseModel']) -> List[Union[str, Field,
InlineFragment(type=union_arg.__name__, fields=_get_fields(union_arg))
for union_arg in union_args
]
else:
elif issubclass(list_args, GraphQLQueryBaseModel):
_field_template.fields = _get_fields(list_args)

#
Expand Down
24 changes: 24 additions & 0 deletions tests/tests_base_model/test_list_of_primitives.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from typing import List

from graphql_query import Field, GraphQLQueryBaseModel


def test_simple_model():
class Hero(GraphQLQueryBaseModel):
name: str
friends: List[str]

correct = [
Field(name="name", fields=[]),
Field(name="friends", fields=[]),
]
generated = Hero.graphql_fields()

assert generated == correct
assert (
Field(name="hero", fields=generated).render()
== """hero {
name
friends
}"""
)

0 comments on commit ff2f8ad

Please sign in to comment.