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

Converters cannot convert datetime.timedelta to float. #348

Open
dionyself opened this issue Dec 18, 2017 · 9 comments
Open

Converters cannot convert datetime.timedelta to float. #348

dionyself opened this issue Dec 18, 2017 · 9 comments

Comments

@dionyself
Copy link

hello devs.
During a conversation about an issue of graphql-core @jkimbo found a possible error

@convert_django_field.register(models.DurationField)
def convert_field_to_float(field, registry=None):
return Float(description=field.help_text, required=not field.null)

raising something like { "message": "float() argument must be a string or a number, not 'datetime.timedelta'" }

you can check graphql-python/graphql-core#150 for more info.

@dionyself dionyself changed the title Converters cannot conevert datetime.timedelta to float. Converters cannot convert datetime.timedelta to float. Dec 19, 2017
@spockNinja
Copy link
Contributor

Yeah, I can see how that'd be a problem... Even if it could convert, a float doesn't quite capture all of the data in a timedelta.

One possibility would be to add an ObjectType wrapper that pulls out the individual parts of the timedelta and send that, potentially with one property being the "human readable" representation.

Any other ideas?

@dionyself
Copy link
Author

dionyself commented Dec 25, 2017

I managed to get an human readlable for string type by using duration = graphene.String()

https://github.com/dionyself/leaky/blob/71ec608204a3dd66f2fcd25ff36096ba25eb62bc/warehouses/schema/query/asset.py#L8-L13

We probably won't need and human readable for the Float converter, so using timedelta.total_seconds() is fine... but, i think this works in py3 only

@stale
Copy link

stale bot commented Jun 11, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jun 11, 2019
@stale stale bot closed this as completed Jun 18, 2019
@zeth
Copy link

zeth commented Oct 17, 2019

This is a bad bot. This is still an issue.

@jkimbo
Copy link
Member

jkimbo commented Oct 18, 2019

Apologies @zeth , reopening the issue. If you have any time any help with the issue would be most appreciated.

@stale
Copy link

stale bot commented Jan 24, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jan 24, 2020
@jkimbo jkimbo removed the wontfix label Jan 29, 2020
@jjoocceeee
Copy link

Following

@JPGarCar
Copy link

Is help still wanted for this issue? I could give it a stab if yall can tell me what you want the solution to be?

@aryadovoy
Copy link

aryadovoy commented May 25, 2022

I used custom scalar for DurationField, it has helped:

import datetime as dt
from graphene import Scalar
from graphql.language import ast


class DurationScalar(Scalar):

    @staticmethod
    def serialize(value):
        if isinstance(value, dt.timedelta):
            return str(value).zfill(8)[:-3]

    @staticmethod
    def parse_literal(node):
        if isinstance(node, ast.StringValue):
            delta = node.value.split(':')
            duration = dt.timedelta(hours=delta[0], minutes=delta[1])
            return duration

    @staticmethod
    def parse_value(value):
        delta = value.split(':')
        duration = dt.timedelta(hours=int(delta[0]), minutes=int(delta[1]))
        return duration

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

No branches or pull requests

8 participants