mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-25 19:13:57 +03:00
commit
2044d29edb
39
graphene/types/jsontype.py
Normal file
39
graphene/types/jsontype.py
Normal file
|
@ -0,0 +1,39 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
from graphql.language.ast import (BooleanValue, FloatValue, IntValue,
|
||||
StringValue, ListValue, ObjectValue)
|
||||
|
||||
from graphene.types.scalars import MIN_INT, MAX_INT
|
||||
from .scalars import Scalar
|
||||
|
||||
|
||||
class JSON(Scalar):
|
||||
"""
|
||||
The `JSON` scalar type represents JSON values as specified by
|
||||
[ECMA-404](http://www.ecma-international.org/
|
||||
publications/files/ECMA-ST/ECMA-404.pdf).
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def identity(value):
|
||||
return value
|
||||
|
||||
serialize = identity
|
||||
parse_value = identity
|
||||
|
||||
@staticmethod
|
||||
def parse_literal(ast):
|
||||
if isinstance(ast, (StringValue, BooleanValue)):
|
||||
return ast.value
|
||||
elif isinstance(ast, IntValue):
|
||||
num = int(ast.value)
|
||||
if MIN_INT <= num <= MAX_INT:
|
||||
return num
|
||||
elif isinstance(ast, FloatValue):
|
||||
return float(ast.value)
|
||||
elif isinstance(ast, ListValue):
|
||||
return [JSON.parse_literal(value) for value in ast.values]
|
||||
elif isinstance(ast, ObjectValue):
|
||||
return {field.name.value: JSON.parse_literal(field.value) for field in ast.fields}
|
||||
else:
|
||||
return None
|
96
graphene/types/tests/test_jsontype.py
Normal file
96
graphene/types/tests/test_jsontype.py
Normal file
|
@ -0,0 +1,96 @@
|
|||
from ..jsontype import JSON
|
||||
from ..objecttype import ObjectType
|
||||
from ..schema import Schema
|
||||
|
||||
|
||||
class Query(ObjectType):
|
||||
json = JSON(input=JSON())
|
||||
|
||||
def resolve_json(self, args, context, info):
|
||||
input = args.get('input')
|
||||
return input
|
||||
|
||||
|
||||
schema = Schema(query=Query)
|
||||
|
||||
|
||||
def test_json_query_variable():
|
||||
for json_value in [
|
||||
1,
|
||||
1.1,
|
||||
True,
|
||||
'str',
|
||||
[1, 2, 3],
|
||||
[1.1, 2.2, 3.3],
|
||||
[True, False],
|
||||
['str1', 'str2'],
|
||||
{
|
||||
'key_a': 'a',
|
||||
'key_b': 'b'
|
||||
},
|
||||
{
|
||||
'int': 1,
|
||||
'float': 1.1,
|
||||
'boolean': True,
|
||||
'string': 'str',
|
||||
'int_list': [1, 2, 3],
|
||||
'float_list': [1.1, 2.2, 3.3],
|
||||
'boolean_list': [True, False],
|
||||
'string_list': ['str1', 'str2'],
|
||||
'nested_dict': {
|
||||
'key_a': 'a',
|
||||
'key_b': 'b'
|
||||
}
|
||||
},
|
||||
None
|
||||
]:
|
||||
result = schema.execute(
|
||||
'''query Test($json: JSON){ json(input: $json) }''',
|
||||
variable_values={'json': json_value}
|
||||
)
|
||||
assert not result.errors
|
||||
assert result.data == {
|
||||
'json': json_value
|
||||
}
|
||||
|
||||
|
||||
def test_json_parse_literal_query():
|
||||
result = schema.execute(
|
||||
'''
|
||||
query {
|
||||
json(input: {
|
||||
int: 1,
|
||||
float: 1.1
|
||||
boolean: true,
|
||||
string: "str",
|
||||
int_list: [1, 2, 3],
|
||||
float_list: [1.1, 2.2, 3.3],
|
||||
boolean_list: [true, false]
|
||||
string_list: ["str1", "str2"],
|
||||
nested_dict: {
|
||||
key_a: "a",
|
||||
key_b: "b"
|
||||
},
|
||||
empty_key: undefined
|
||||
})
|
||||
}
|
||||
'''
|
||||
)
|
||||
assert not result.errors
|
||||
assert result.data == {
|
||||
'json': {
|
||||
'int': 1,
|
||||
'float': 1.1,
|
||||
'boolean': True,
|
||||
'string': 'str',
|
||||
'int_list': [1, 2, 3],
|
||||
'float_list': [1.1, 2.2, 3.3],
|
||||
'boolean_list': [True, False],
|
||||
'string_list': ['str1', 'str2'],
|
||||
'nested_dict': {
|
||||
'key_a': 'a',
|
||||
'key_b': 'b'
|
||||
},
|
||||
'empty_key': None
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user