mirror of
https://github.com/graphql-python/graphene.git
synced 2025-10-18 01:34:19 +03:00
* Updated all str.format(…) to f-strings This revamps the PR #984 * Pass black * Fix flake8 * Updated objecttype * Fix black version
35 lines
795 B
Python
35 lines
795 B
Python
from __future__ import absolute_import
|
|
|
|
from decimal import Decimal as _Decimal
|
|
|
|
from graphql.language.ast import StringValueNode
|
|
|
|
from .scalars import Scalar
|
|
|
|
|
|
class Decimal(Scalar):
|
|
"""
|
|
The `Decimal` scalar type represents a python Decimal.
|
|
"""
|
|
|
|
@staticmethod
|
|
def serialize(dec):
|
|
if isinstance(dec, str):
|
|
dec = _Decimal(dec)
|
|
assert isinstance(
|
|
dec, _Decimal
|
|
), f'Received not compatible Decimal "{repr(dec)}"'
|
|
return str(dec)
|
|
|
|
@classmethod
|
|
def parse_literal(cls, node):
|
|
if isinstance(node, StringValueNode):
|
|
return cls.parse_value(node.value)
|
|
|
|
@staticmethod
|
|
def parse_value(value):
|
|
try:
|
|
return _Decimal(value)
|
|
except ValueError:
|
|
return None
|