strawberry-graphql / strawberry-django

Strawberry GraphQL Django extension
https://strawberry.rocks/docs/django
MIT License
404 stars 117 forks source link

DurationField support #233

Open JerryMartinsonLeviti opened 1 year ago

JerryMartinsonLeviti commented 1 year ago

Feature Request Type

Description

I use DurationFields a fair amount in Django and I noticed that they don't seem to be supported. Digging through the code a bit, I think it could be added by changing .../strawberry_django/fields/types.py in the following way:

Adding fields.DurationField: datetime.timedelta to the field_type_map dict between line 70 & 71.

I'm a total newbie to any contributory activity, so I'm not sure if there's a better way to submit this.

Upvote & Fund

Fund with Polar

bellini666 commented 1 year ago

Yes, it is basically that. But note that this will require a new scalar too, and I think that scalar should go do https://github.com/strawberry-graphql/strawberry/blob/main/strawberry/schema/types/base_scalars.py

Basically a scalar named Timedelta that serializes to .total_seconds() and parsers the seconds as a timedelta object

Then you just need to map the DurationField to datetime.timedelta in this lib and it is done