[Answered ]-Can graphene resolver return Float instead of String for django Decimal field?

1πŸ‘

βœ…

It turns out you do not have to write a customer resolver, but instead can just declare the type of the Django field, like so:

class ToyType(DjangoObjectType):

    price = graphene.Float()

    class Meta:
        model = Toy
        fields = ('name', 'price')

In this way, the resolver will return price as a Float. If you do not explicitly declare the type, the graphene default is to return the django Decimal field as a string.

0πŸ‘

To convert the decimal value to a float, use a custom resolver as shown.

class ToyType(DjangoObjectType):
    class Meta:
        model = Toy
        fields = ('name', 'price')

    price = graphene.Float()

    def resolve_price(self, info):
        return float(self.price)

Sample query and response

Leave a comment