-
Notifications
You must be signed in to change notification settings - Fork 821
Issue 703 -- support for Decimal type #726
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4925819
adds decimal type and associated tests
d40ef4b
adds Decimal to types __all__
de050fa
fix linting error
0fdc2ca
should fix some import issues with python 2.7
fc3dbf0
Merge branch 'master' into issue-703
c076412
automatically generated linting fixes
8ca7b85
more flake8 fixes
00cc978
Merge branch 'master' into issue-703
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from __future__ import absolute_import | ||
|
||
from decimal import Decimal as _Decimal | ||
|
||
from graphql.language import ast | ||
|
||
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), 'Received not compatible Decimal "{}"'.format( | ||
repr(dec) | ||
) | ||
return str(dec) | ||
|
||
@classmethod | ||
def parse_literal(cls, node): | ||
if isinstance(node, ast.StringValue): | ||
return cls.parse_value(node.value) | ||
|
||
@staticmethod | ||
def parse_value(value): | ||
try: | ||
return _Decimal(value) | ||
except ValueError: | ||
return None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import decimal | ||
|
||
from ..decimal import Decimal | ||
from ..objecttype import ObjectType | ||
from ..schema import Schema | ||
|
||
|
||
class Query(ObjectType): | ||
decimal = Decimal(input=Decimal()) | ||
|
||
def resolve_decimal(self, info, input): | ||
return input | ||
|
||
|
||
schema = Schema(query=Query) | ||
|
||
|
||
def test_decimal_string_query(): | ||
decimal_value = decimal.Decimal("1969.1974") | ||
result = schema.execute("""{ decimal(input: "%s") }""" % decimal_value) | ||
assert not result.errors | ||
assert result.data == {"decimal": str(decimal_value)} | ||
assert decimal.Decimal(result.data["decimal"]) == decimal_value | ||
|
||
|
||
def test_decimal_string_query_variable(): | ||
decimal_value = decimal.Decimal("1969.1974") | ||
|
||
result = schema.execute( | ||
"""query Test($decimal: Decimal){ decimal(input: $decimal) }""", | ||
variable_values={"decimal": decimal_value}, | ||
) | ||
assert not result.errors | ||
assert result.data == {"decimal": str(decimal_value)} | ||
assert decimal.Decimal(result.data["decimal"]) == decimal_value | ||
|
||
|
||
def test_bad_decimal_query(): | ||
not_a_decimal = "Nobody expects the Spanish Inquisition!" | ||
|
||
result = schema.execute("""{ decimal(input: "%s") }""" % not_a_decimal) | ||
assert len(result.errors) == 1 | ||
assert result.data is None |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it check for
decimal.DecimalException
here, as for example Django does? I've copied your changes to my repo (as it hasn't been released yet) and I'm trying to use it in my mutations, but when providing invalid values like an empty string I'm getting:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@maarcingebala can you submit a pull request with this change so it can be reviewed?