|
| 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