diff --git a/strawberry_django/optimizer.py b/strawberry_django/optimizer.py index 956627fa..f34531b0 100644 --- a/strawberry_django/optimizer.py +++ b/strawberry_django/optimizer.py @@ -1193,6 +1193,10 @@ def _get_model_hints_from_paginated( store = None n_type = object_definition.type_var_map.get("NodeType") + + if isinstance(n_type, LazyType): + n_type = n_type.resolve_type() + n_definition = get_object_definition(n_type, strict=True) n_gql_definition = _get_gql_definition( schema, diff --git a/tests/test_paginated_type.py b/tests/test_paginated_type.py index 74537727..0ae01892 100644 --- a/tests/test_paginated_type.py +++ b/tests/test_paginated_type.py @@ -1,10 +1,12 @@ import textwrap +from typing import Annotated import pytest import strawberry from django.db.models import QuerySet import strawberry_django +from strawberry_django.optimizer import DjangoOptimizerExtension from strawberry_django.pagination import OffsetPaginated, OffsetPaginationInput from tests import models @@ -195,6 +197,75 @@ class Query: } +@strawberry_django.type(models.Fruit) +class FruitLazyTest: + id: int + name: str + + +@strawberry_django.type(models.Color) +class ColorLazyTest: + id: int + name: str + fruits: OffsetPaginated[ + Annotated["FruitLazyTest", strawberry.lazy("tests.test_paginated_type")] + ] = strawberry_django.offset_paginated() + + +@pytest.mark.django_db(transaction=True) +def test_pagination_with_lazy_type_and_django_query_optimizer(): + @strawberry.type + class Query: + colors: OffsetPaginated[ColorLazyTest] = strawberry_django.offset_paginated() + + red = models.Color.objects.create(name="Red") + yellow = models.Color.objects.create(name="Yellow") + + models.Fruit.objects.create(name="Apple", color=red) + models.Fruit.objects.create(name="Banana", color=yellow) + models.Fruit.objects.create(name="Strawberry", color=red) + + schema = strawberry.Schema(query=Query, extensions=[DjangoOptimizerExtension]) + + query = """\ + query GetColors ($pagination: OffsetPaginationInput) { + colors { + totalCount + results { + fruits (pagination: $pagination) { + totalCount + results { + name + } + } + } + } + } + """ + + res = schema.execute_sync(query) + assert res.errors is None + assert res.data == { + "colors": { + "totalCount": 2, + "results": [ + { + "fruits": { + "totalCount": 2, + "results": [{"name": "Apple"}, {"name": "Strawberry"}], + } + }, + { + "fruits": { + "totalCount": 1, + "results": [{"name": "Banana"}], + } + }, + ], + } + } + + @pytest.mark.django_db(transaction=True) def test_pagination_nested_query(): @strawberry_django.type(models.Fruit)