Skip to content

Commit

Permalink
Added missing unpacking of strawberry.LazyType to optimzer.py (#670)
Browse files Browse the repository at this point in the history
  • Loading branch information
NT-Timm authored Dec 8, 2024
1 parent 3e64009 commit 8bce855
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
4 changes: 4 additions & 0 deletions strawberry_django/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
71 changes: 71 additions & 0 deletions tests/test_paginated_type.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 8bce855

Please sign in to comment.