Skip to content

Commit 2044d29

Browse files
authored
Merge pull request #387 from hung-phan/master
Implement JSON type
2 parents 2f87698 + 17ea139 commit 2044d29

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

graphene/types/jsontype.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from __future__ import unicode_literals
2+
3+
from graphql.language.ast import (BooleanValue, FloatValue, IntValue,
4+
StringValue, ListValue, ObjectValue)
5+
6+
from graphene.types.scalars import MIN_INT, MAX_INT
7+
from .scalars import Scalar
8+
9+
10+
class JSON(Scalar):
11+
"""
12+
The `JSON` scalar type represents JSON values as specified by
13+
[ECMA-404](http://www.ecma-international.org/
14+
publications/files/ECMA-ST/ECMA-404.pdf).
15+
"""
16+
17+
@staticmethod
18+
def identity(value):
19+
return value
20+
21+
serialize = identity
22+
parse_value = identity
23+
24+
@staticmethod
25+
def parse_literal(ast):
26+
if isinstance(ast, (StringValue, BooleanValue)):
27+
return ast.value
28+
elif isinstance(ast, IntValue):
29+
num = int(ast.value)
30+
if MIN_INT <= num <= MAX_INT:
31+
return num
32+
elif isinstance(ast, FloatValue):
33+
return float(ast.value)
34+
elif isinstance(ast, ListValue):
35+
return [JSON.parse_literal(value) for value in ast.values]
36+
elif isinstance(ast, ObjectValue):
37+
return {field.name.value: JSON.parse_literal(field.value) for field in ast.fields}
38+
else:
39+
return None

graphene/types/tests/test_jsontype.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
from ..jsontype import JSON
2+
from ..objecttype import ObjectType
3+
from ..schema import Schema
4+
5+
6+
class Query(ObjectType):
7+
json = JSON(input=JSON())
8+
9+
def resolve_json(self, args, context, info):
10+
input = args.get('input')
11+
return input
12+
13+
14+
schema = Schema(query=Query)
15+
16+
17+
def test_json_query_variable():
18+
for json_value in [
19+
1,
20+
1.1,
21+
True,
22+
'str',
23+
[1, 2, 3],
24+
[1.1, 2.2, 3.3],
25+
[True, False],
26+
['str1', 'str2'],
27+
{
28+
'key_a': 'a',
29+
'key_b': 'b'
30+
},
31+
{
32+
'int': 1,
33+
'float': 1.1,
34+
'boolean': True,
35+
'string': 'str',
36+
'int_list': [1, 2, 3],
37+
'float_list': [1.1, 2.2, 3.3],
38+
'boolean_list': [True, False],
39+
'string_list': ['str1', 'str2'],
40+
'nested_dict': {
41+
'key_a': 'a',
42+
'key_b': 'b'
43+
}
44+
},
45+
None
46+
]:
47+
result = schema.execute(
48+
'''query Test($json: JSON){ json(input: $json) }''',
49+
variable_values={'json': json_value}
50+
)
51+
assert not result.errors
52+
assert result.data == {
53+
'json': json_value
54+
}
55+
56+
57+
def test_json_parse_literal_query():
58+
result = schema.execute(
59+
'''
60+
query {
61+
json(input: {
62+
int: 1,
63+
float: 1.1
64+
boolean: true,
65+
string: "str",
66+
int_list: [1, 2, 3],
67+
float_list: [1.1, 2.2, 3.3],
68+
boolean_list: [true, false]
69+
string_list: ["str1", "str2"],
70+
nested_dict: {
71+
key_a: "a",
72+
key_b: "b"
73+
},
74+
empty_key: undefined
75+
})
76+
}
77+
'''
78+
)
79+
assert not result.errors
80+
assert result.data == {
81+
'json': {
82+
'int': 1,
83+
'float': 1.1,
84+
'boolean': True,
85+
'string': 'str',
86+
'int_list': [1, 2, 3],
87+
'float_list': [1.1, 2.2, 3.3],
88+
'boolean_list': [True, False],
89+
'string_list': ['str1', 'str2'],
90+
'nested_dict': {
91+
'key_a': 'a',
92+
'key_b': 'b'
93+
},
94+
'empty_key': None
95+
}
96+
}

0 commit comments

Comments
 (0)