blb-ventures / strawberry-django-plus

Enhanced Strawberry GraphQL integration with Django
MIT License
179 stars 47 forks source link

NameError with lazy types in relay connection annotations #234

Closed jacobjove closed 1 year ago

jacobjove commented 1 year ago

Describe the Bug

When I use lazy types as described in https://strawberry.rocks/docs/types/lazy I get a NameError like the following:

  File "/Users/jacob/code/saleor/api/api/product/types/product_types.py", line 82, in ProductType
    def attributes(
  File "/Users/jacob/code/saleor/api/.venv/lib/python3.10/site-packages/strawberry_django_plus/relay.py", line 959, in __call__
    resolved = _eval_type(nodes_type, namespace, None)
  File "/usr/local/Cellar/python@3.10/3.10.7/Frameworks/Python.framework/Versions/3.10/lib/python3.10/typing.py", line 329, in _eval_type
    ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
  File "/usr/local/Cellar/python@3.10/3.10.7/Frameworks/Python.framework/Versions/3.10/lib/python3.10/typing.py", line 329, in <genexpr>
    ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
  File "/usr/local/Cellar/python@3.10/3.10.7/Frameworks/Python.framework/Versions/3.10/lib/python3.10/typing.py", line 329, in _eval_type
    ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
  File "/usr/local/Cellar/python@3.10/3.10.7/Frameworks/Python.framework/Versions/3.10/lib/python3.10/typing.py", line 329, in <genexpr>
    ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
  File "/usr/local/Cellar/python@3.10/3.10.7/Frameworks/Python.framework/Versions/3.10/lib/python3.10/typing.py", line 327, in _eval_type
    return t._evaluate(globalns, localns, recursive_guard)
  File "/usr/local/Cellar/python@3.10/3.10.7/Frameworks/Python.framework/Versions/3.10/lib/python3.10/typing.py", line 694, in _evaluate
    eval(self.__forward_code__, globalns, localns),
  File "<string>", line 1, in <module>
NameError: name 'Attribute' is not defined

Relevant code:

from typing import TYPE_CHECKING, Annotated
from strawberry_django_plus import gql
...

if TYPE_CHECKING:
    from api.attribute.types.attribute import Attribute

...
@gql.django.type(models.ProductType)
class ProductType(gql.relay.Node):
    @gql.relay.connection
    def attributes(self) -> list[Annotated["Attribute", gql.lazy("api.attribute.types.attribute")]]:
        pass

The error goes away if I use a regular forward reference without Annotation and lazy, like so:

    @gql.relay.connection
    def attributes(self) -> list["Attribute"]:
        pass

(Although I'm guessing this would prevent the schema from being generated correctly.)

System Information

Python 3.10.7