Skip to content

Commit 26a674e

Browse files
fix: name conversion for lazy types (#3944)
* fix: name converter * add RELEASE.md * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix: commented test * Update RELEASE.md --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 10a381d commit 26a674e

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

RELEASE.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Release type: minor
2+
3+
This release fixes NameConverter to properly handle lazy types.

strawberry/schema/name_converter.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,9 @@ def get_name_from_type(self, type_: Union[StrawberryType, type]) -> str:
150150
type_ = eval_type(type_)
151151

152152
if isinstance(type_, LazyType):
153-
name = type_.type_name
154-
elif isinstance(type_, EnumDefinition):
153+
type_ = type_.resolve_type()
154+
155+
if isinstance(type_, EnumDefinition):
155156
name = type_.name
156157
elif isinstance(type_, StrawberryUnion):
157158
name = type_.graphql_name if type_.graphql_name else self.from_union(type_)

tests/objects/generics/test_names.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,17 @@ class TypeB:
4141
([TypeA], "TypeAExample"),
4242
([CustomInt], "CustomIntExample"),
4343
([TypeA, TypeB], "TypeATypeBExample"),
44-
([TypeA, LazyType["TypeB", "test_names"]], "TypeATypeBExample"), # type: ignore
4544
(
46-
[TypeA, Annotated["TypeB", strawberry.lazy("test_names")]],
45+
[TypeA, LazyType["TypeB", "tests.objects.generics.test_names"]],
46+
"TypeATypeBExample",
47+
), # type: ignore
48+
(
49+
[
50+
TypeA,
51+
Annotated[
52+
"TypeB", strawberry.lazy("tests.objects.generics.test_names")
53+
],
54+
],
4755
"TypeATypeBExample",
4856
),
4957
],

tests/schema/test_name_converter.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import textwrap
22
from enum import Enum
3-
from typing import Generic, Optional, TypeVar, Union
3+
from typing import Annotated, Generic, Optional, TypeVar, Union
44

55
import strawberry
66
from strawberry.directive import StrawberryDirective
@@ -78,6 +78,11 @@ class User:
7878
name: str
7979

8080

81+
@strawberry.type(name="MyType")
82+
class TypeWithDifferentNameThanClass:
83+
name: str
84+
85+
8186
@strawberry.type
8287
class Error:
8388
message: str
@@ -115,6 +120,12 @@ def user(self, input: UserInput) -> Union[User, Error]:
115120

116121
enum: MyEnum = MyEnum.A
117122
field: Optional[MyGeneric[str]] = None
123+
field_with_lazy: MyGeneric[
124+
Annotated[
125+
"TypeWithDifferentNameThanClass",
126+
strawberry.lazy("tests.schema.test_name_converter"),
127+
]
128+
] = None
118129

119130
@strawberry.field
120131
def print(self, enum: MyEnum) -> str:
@@ -141,13 +152,22 @@ def test_name_converter():
141152
BX
142153
}
143154
155+
type MyTypeMyGenericXX {
156+
valueX: MyTypeX!
157+
}
158+
159+
type MyTypeX {
160+
nameX: String!
161+
}
162+
144163
interface NodeXX {
145164
idX: ID!
146165
}
147166
148167
type QueryX {
149168
enumX: MyEnumX!
150169
fieldX: StrMyGenericXX
170+
fieldWithLazyX: MyTypeMyGenericXX!
151171
userX(inputX: UserInputX!): UserXErrorXX! @myDirectiveX(name: "my-directive")
152172
printX(enumX: MyEnumX!): String!
153173
}

0 commit comments

Comments
 (0)