From cad99b4e4dee6eea3f5260bbcfa3945a37f44e37 Mon Sep 17 00:00:00 2001 From: franco-gondi Date: Mon, 23 Jun 2025 19:59:26 -0300 Subject: [PATCH 1/2] fix: codegen generic types on selection --- strawberry/codegen/query_codegen.py | 40 ++++++++++++++--------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/strawberry/codegen/query_codegen.py b/strawberry/codegen/query_codegen.py index abd36717dd..31688b4b1c 100644 --- a/strawberry/codegen/query_codegen.py +++ b/strawberry/codegen/query_codegen.py @@ -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) @@ -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 - # 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 @@ -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 + # 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 + __all__ = [ "CodegenFile", From a3aa14edb31bd40c990e25113616b8c2a91b82ee Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Jun 2025 23:04:37 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strawberry/codegen/query_codegen.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/strawberry/codegen/query_codegen.py b/strawberry/codegen/query_codegen.py index 31688b4b1c..7d59489ab3 100644 --- a/strawberry/codegen/query_codegen.py +++ b/strawberry/codegen/query_codegen.py @@ -910,10 +910,13 @@ def _parent_type_name(self, parent_type: StrawberryObjectDefinition) -> str: # 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 ( + "".join( + c.__name__ # type: ignore[union-attr] + for c in parent_type.type_var_map.values() + ) + + parent_type.name + ) return parent_type.name