Skip to content
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

Change timedelta scalar to return ISO format #42

Open
gdalmau opened this issue Sep 22, 2021 · 1 comment
Open

Change timedelta scalar to return ISO format #42

gdalmau opened this issue Sep 22, 2021 · 1 comment

Comments

@gdalmau
Copy link

gdalmau commented Sep 22, 2021

Currently the timedelta scalar serializer returns a float equivalent to the total amount of seconds.

https://github.com/reset-button/ariadne_django/blob/5a7c9d15a2b0e0baa8b7e7aad032c738a3dcd1fe/ariadne_django/scalars/timedelta.py#L10-L12

It could be an idea to return the ISO format for durations (see https://en.wikipedia.org/wiki/ISO_8601#Durations). Since python doesn't implement a way to do timedelta(days=1).isoformat(), we could use django duration_iso_string:

from datetime import timedelta
from django.utils.duration import duration_iso_string

timedelta_scalar = ScalarType("Timedelta")

 @timedelta_scalar.serializer 
 def serialize_timedelta(value: timedelta) -> float: 
     return duration_iso_string(value)

Also instead of Timedelta it could be called Duration

@maxmorlocke
Copy link
Contributor

Happy to accept a PR for a new scalar. Let's not be the type of folks that break backwards compatibility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants