Skip to content

Commit ff2f8ad

Browse files
committed
fix: only recursively get fields if element type is custom type
1 parent 22ec204 commit ff2f8ad

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

graphql_query/base_model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def _get_field_template(field_info: PydanticFieldInfo) -> Field:
2121
return Field(name="<NAME>", fields=[], alias=alias, arguments=arguments, directives=directives, typename=typename)
2222

2323

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

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

5656
#
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from typing import List
2+
3+
from graphql_query import Field, GraphQLQueryBaseModel
4+
5+
6+
def test_simple_model():
7+
class Hero(GraphQLQueryBaseModel):
8+
name: str
9+
friends: List[str]
10+
11+
correct = [
12+
Field(name="name", fields=[]),
13+
Field(name="friends", fields=[]),
14+
]
15+
generated = Hero.graphql_fields()
16+
17+
assert generated == correct
18+
assert (
19+
Field(name="hero", fields=generated).render()
20+
== """hero {
21+
name
22+
friends
23+
}"""
24+
)

0 commit comments

Comments
 (0)