Graphene's resolver returns my django model Decimal field as a string (e.g. "8.33" instead of 8.33). I want to receive it as a float. Is there some way to do this?
My code is as follows:
models.py
class Toy(models.Model):
name = models.CharField(max_length=50)
price = models.DecimalField()
queries.py
class ToyType(DjangoObjectType):
class Meta:
model = Toy
fields = ('name', 'price')
class ToyQuery(graphene.ObjectType):
first_toy = graphene.Field(ToyType)
def resolve_first_toy(self, info):
return Toy.objects.first()
And my query and result are:
query
{
firstToy{
name
price
}
}
result
{
"data": {
"name": "BuzzLighYear",
"price": "19.95"
}
}
Instead, I would like to receive:
{
"data": {
"name": "BuzzLighYear",
"price": 19.95
}
}
Do I have to write a custom resolver for the price field, or is there a more elegant way to do this with a setting?
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.