Is it possible to define fields on types separately from the type itself? #3577
Unanswered
rohan-mehta
asked this question in
Q&A
Replies: 1 comment 3 replies
-
If I understand you problem correctly, the following would be the easiest solution: import strawberry
import typing
if typing.TYPE_CHECKING:
from .posts import Post
@strawberry.type
class User:
name: str
@strawberry.field
async def recent_post() -> Annotated["Post", strawberry.lazy(".posts")]:
from .posts import Post
post_data: dict = fetch_post()
return Post(**post_data) However, you can also define field resolvers separately from the fields as shown in our resolvers documentation. |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a circular import in my code. It looks something like this:
The Lazy types documentation doesn't really solve for this, because we don't just need the types for type checking - we actually need it at runtime to call the constructors for
User()
andPost()
.My ideal solution would be to define the
Post
andUser
types as simple data model objects in one file. Then "extend" them by adding resolved fields from somewhere else. Something like this:Is something like that possible? Thanks!
Beta Was this translation helpful? Give feedback.
All reactions