-
-
Notifications
You must be signed in to change notification settings - Fork 112
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
Better typing model property #240
Comments
I'm rewriting my api in graphene_django to strawberry.
|
If it helps, I created a custom type def json_field():
"""
Creates a field and resolver for fields on a json blob.
"""
def resolver(root: dict, info: Info):
if isinstance(info.return_type, StrawberryOptional):
return root.get(info.python_name, None)
return root[info.python_name]
return strawberry.field(resolver=resolver)
@strawberry.type
class JsonType:
"""
Models the type of a json blob, which needs specific typing for graphql.
"""
def __init_subclass__(cls) -> None:
for field_name in cls.__annotations__.keys():
setattr(cls, field_name, json_field()) You would just need to have your @strawberry.type
class SiteType:
@strawberry.type()
class PlayerSiteDataType(JsonType):
src: str
size: int
type: str
name: str
player_data: List[PlayerSiteDataType] |
@vitosamson it looks good. Thank you! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I have a model property in the django framework that returns a list of dict
Is there any way to type it more easily than using a resolver?
Currently my code looks like this:
My django model:
My types:
Thanks.
Upvote & Fund
The text was updated successfully, but these errors were encountered: