strawberry-graphql / strawberry

A GraphQL library for Python that leverages type annotations 🍓
https://strawberry.rocks
MIT License
4k stars 531 forks source link

Lazy Types and Generics #2433

Open luizpcf opened 1 year ago

luizpcf commented 1 year ago

Describe the Bug

I'm trying to use a lazy type to solve a cyclical dep issue with a generic type and I'm getting a TypeError.

I have the following code:

from typing import TYPE_CHECKING, Annotated, List

import strawberry
from mercury.models.pagination import Connection
from mercury.models.task import Task

if TYPE_CHECKING:
    from mercury.models.project import Project

@strawberry.type
class Annotator:
    id: str
    name: str
    tasks: Connection[Task]
    projects: Connection[Annotated["Project", strawberry.lazy("mercury.models.project")]]

And I get this error:

TypeError: Annotator fields cannot be resolved. Could not resolve the type of 'projects'. Check that the class is accessible from the global module scope.

If I use, projects: List[Annotated["Project", strawberry.lazy("mercury.models.project")]] the code will work without problem.

Connection is a generic type for pagination copied from here: https://strawberry.rocks/docs/guides/pagination

System Information

Additional Context

Upvote & Fund

Fund with Polar

patrick91 commented 1 year ago

this should work, @luizpcf do you have a minimal reproduction I can try? 😊