graphene-django icon indicating copy to clipboard operation
graphene-django copied to clipboard

Converters cannot convert datetime.timedelta to float.

Open dionyself opened this issue 7 years ago • 9 comments

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

https://github.com/graphql-python/graphene-django/blob/5051d3bb617ce6977eebc023c958bd10de91fe91/graphene_django/converter.py#L111-L113

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

you can check https://github.com/graphql-python/graphql-core/issues/150 for more info.

dionyself avatar Dec 18 '17 16:12 dionyself

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?

spockNinja avatar Dec 23 '17 23:12 spockNinja

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

dionyself avatar Dec 25 '17 13:12 dionyself

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[bot] avatar Jun 11 '19 12:06 stale[bot]

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

zeth avatar Oct 17 '19 19:10 zeth

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

jkimbo avatar Oct 18 '19 09:10 jkimbo

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[bot] avatar Jan 24 '20 02:01 stale[bot]

Following

jjoocceeee avatar Aug 13 '20 21:08 jjoocceeee

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?

JPGarCar avatar Nov 15 '21 05:11 JPGarCar

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

aryadovoy avatar May 25 '22 06:05 aryadovoy