diff --git a/tests/b.py b/tests/b.py index f646816b0c..3042bbe73a 100644 --- a/tests/b.py +++ b/tests/b.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Optional +from typing import TYPE_CHECKING, List, Optional from typing_extensions import Annotated import strawberry @@ -19,6 +19,12 @@ async def a(self) -> Annotated[A, strawberry.lazy("tests.a"), object()]: return A(id=self.id) + @strawberry.field + async def a_list(self) -> List[Annotated[A, strawberry.lazy("tests.a")]]: + from tests.a import A + + return [A(id=self.id)] + @strawberry.field async def optional_a( self, diff --git a/tests/c.py b/tests/c.py new file mode 100644 index 0000000000..c365cd792b --- /dev/null +++ b/tests/c.py @@ -0,0 +1,8 @@ +from __future__ import annotations + +import strawberry + + +@strawberry.type +class C: + id: strawberry.ID diff --git a/tests/d.py b/tests/d.py new file mode 100644 index 0000000000..d36b2a84d5 --- /dev/null +++ b/tests/d.py @@ -0,0 +1,20 @@ +from __future__ import annotations + +from typing import TYPE_CHECKING, List +from typing_extensions import Annotated + +import strawberry + +if TYPE_CHECKING: + from tests.c import C + + +@strawberry.type +class D: + id: strawberry.ID + + @strawberry.field + async def c_list(self) -> List[Annotated[C, strawberry.lazy("tests.c")]]: + from tests.c import C + + return [C(id=self.id)] diff --git a/tests/schema/test_lazy/test_lazy.py b/tests/schema/test_lazy/test_lazy.py index ce1c31f831..a89ad8e589 100644 --- a/tests/schema/test_lazy/test_lazy.py +++ b/tests/schema/test_lazy/test_lazy.py @@ -26,6 +26,12 @@ class Query: type TypeB { typeA: TypeA! + typeAList: [TypeA!]! + typeCList: [TypeC!]! + } + + type TypeC { + name: String! } """ diff --git a/tests/schema/test_lazy/test_lazy_generic.py b/tests/schema/test_lazy/test_lazy_generic.py index 3bc8102d2b..6dfbdcc9be 100644 --- a/tests/schema/test_lazy/test_lazy_generic.py +++ b/tests/schema/test_lazy/test_lazy_generic.py @@ -66,11 +66,17 @@ class Query: type TypeB { typeA: TypeA! + typeAList: [TypeA!]! + typeCList: [TypeC!]! } type TypeBEdge { node: TypeB! } + + type TypeC { + name: String! + } """ ).strip() diff --git a/tests/schema/test_lazy/type_b.py b/tests/schema/test_lazy/type_b.py index 7b43591634..927281c501 100644 --- a/tests/schema/test_lazy/type_b.py +++ b/tests/schema/test_lazy/type_b.py @@ -1,12 +1,22 @@ -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, List from typing_extensions import Annotated import strawberry if TYPE_CHECKING: from .type_a import TypeA + from .type_c import TypeC + + ListTypeA = List[TypeA] + ListTypeC = List[TypeC] else: TypeA = Annotated["TypeA", strawberry.lazy("tests.schema.test_lazy.type_a")] + ListTypeA = List[ + Annotated["TypeA", strawberry.lazy("tests.schema.test_lazy.type_a")] + ] + ListTypeC = List[ + Annotated["TypeC", strawberry.lazy("tests.schema.test_lazy.type_c")] + ] @strawberry.type @@ -18,3 +28,19 @@ def type_a( from .type_a import TypeA return TypeA() + + @strawberry.field() + def type_a_list( + self, + ) -> ListTypeA: + from .type_a import TypeA + + return [TypeA()] + + @strawberry.field() + def type_c_list( + self, + ) -> ListTypeC: + from .type_c import TypeC + + return [TypeC()] diff --git a/tests/test_forward_references.py b/tests/test_forward_references.py index af04206fdd..c2a8965df1 100644 --- a/tests/test_forward_references.py +++ b/tests/test_forward_references.py @@ -13,6 +13,7 @@ from strawberry.scalars import JSON from strawberry.type import StrawberryList, StrawberryOptional from tests.a import A +from tests.d import D def test_forward_reference(): @@ -74,6 +75,7 @@ async def a(self) -> A: type B { id: ID! a: A! + aList: [A!]! optionalA: A optionalA2: A } @@ -87,6 +89,36 @@ async def a(self) -> A: assert print_schema(schema) == textwrap.dedent(expected_representation).strip() +@pytest.mark.skipif( + sys.version_info < (3, 9), + reason="Python 3.8 and previous can't properly resolve this.", +) +def test_lazy_forward_reference_schema_with_a_list_only(): + @strawberry.type + class Query: + @strawberry.field + async def d(self) -> D: + return D(id=strawberry.ID("1")) + + expected_representation = """ + type C { + id: ID! + } + + type D { + id: ID! + cList: [C!]! + } + + type Query { + d: D! + } + """ + + schema = strawberry.Schema(query=Query) + assert print_schema(schema) == textwrap.dedent(expected_representation).strip() + + def test_with_resolver(): global User