Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added missing unpacking of strawberry.LazyType to optimzer.py #670

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading