Skip to content
Open
Changes from 1 commit
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
40 changes: 20 additions & 20 deletions strawberry/codegen/query_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,10 +625,11 @@ def _collect_type_from_variable(
def _field_from_selection(
self, selection: FieldNode, parent_type: StrawberryObjectDefinition
) -> GraphQLField:
parent_type_name = self._parent_type_name(parent_type)
if selection.name.value == "__typename":
return GraphQLField("__typename", None, GraphQLScalar("String", None))
field = self.schema.get_field_for_type(selection.name.value, parent_type.name)
assert field, f"{parent_type.name},{selection.name.value}"
field = self.schema.get_field_for_type(selection.name.value, parent_type_name)
assert field, f"{parent_type_name},{selection.name.value}"

field_type = self._get_field_type(field.type)

Expand Down Expand Up @@ -672,24 +673,7 @@ def _field_from_selection_set(
) -> GraphQLField:
assert selection.selection_set is not None

parent_type_name = parent_type.name

# Check if the parent type is generic.
# This seems to be tracked by `strawberry` in the `type_var_map`
# If the type is generic, then the strawberry generated schema
# naming convention is <GenericType,...><ClassName>
# The implementation here assumes that the `type_var_map` is ordered,
# but insertion order is maintained in python3.6+ (for CPython) and
# guaranteed for all python implementations in python3.7+, so that
# should be pretty safe.
if parent_type.type_var_map:
parent_type_name = (
"".join(
c.__name__ # type: ignore[union-attr]
for c in parent_type.type_var_map.values()
)
+ parent_type.name
)
parent_type_name = self._parent_type_name(parent_type)

selected_field = self.schema.get_field_for_type(
selection.name.value, parent_type_name
Expand Down Expand Up @@ -916,6 +900,22 @@ def _collect_enum(self, enum: EnumDefinition) -> GraphQLEnum:
self._collect_type(graphql_enum)
return graphql_enum

def _parent_type_name(self, parent_type: StrawberryObjectDefinition) -> str:
# Check if the parent type is generic.
# This seems to be tracked by `strawberry` in the `type_var_map`
# If the type is generic, then the strawberry generated schema
# naming convention is <GenericType,...><ClassName>
# The implementation here assumes that the `type_var_map` is ordered,
# but insertion order is maintained in python3.6+ (for CPython) and
# guaranteed for all python implementations in python3.7+, so that
# should be pretty safe.
if parent_type.type_var_map:
return "".join(
c.__name__ # type: ignore[union-attr]
for c in parent_type.type_var_map.values()
) + parent_type.name
return parent_type.name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: consider documenting the generic type handling behavior with a docstring, especially the ordering assumptions



__all__ = [
"CodegenFile",
Expand Down
Loading