Skip to content

Commit f4ea01d

Browse files
committed
add NullValue field validation
1 parent 8dc9fa8 commit f4ea01d

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

graphql/utils/is_valid_literal_value.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
def is_valid_literal_value(type, value_ast):
1010
if isinstance(type, GraphQLNonNull):
1111
of_type = type.of_type
12-
if not value_ast:
13-
return [u'Expected "{}", found null.'.format(type)]
12+
if not value_ast or isinstance(value_ast, ast.NullValue):
13+
return [u'Expected type "{}", found null.'.format(type)]
1414

1515
return is_valid_literal_value(of_type, value_ast)
1616

@@ -60,6 +60,9 @@ def get_field_ast_value(field_name):
6060

6161
assert isinstance(type, (GraphQLScalarType, GraphQLEnumType)), 'Must be input type'
6262

63+
if isinstance(value_ast, ast.NullValue):
64+
return _empty_list
65+
6366
parse_result = type.parse_literal(value_ast)
6467
if parse_result is None:
6568
return [u'Expected type "{}", found {}.'.format(type.name, print_ast(value_ast))]

graphql/validation/tests/test_arguments_of_correct_type.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ def test_good_enum_value(self):
8989
}
9090
''')
9191

92+
def test_null_nullable_int_value(self):
93+
expect_passes_rule(ArgumentsOfCorrectType, '''
94+
{
95+
complicatedArgs {
96+
intArgField(intArg: null)
97+
}
98+
}
99+
''')
100+
92101

93102
# noinspection PyMethodMayBeStatic
94103
class TestInvalidStringValues(object):
@@ -196,6 +205,17 @@ def test_float_into_int(self):
196205
bad_value('intArg', 'Int', '3.333', 4, 37)
197206
])
198207

208+
def test_null_into_non_null_int(self):
209+
expect_fails_rule(ArgumentsOfCorrectType, '''
210+
{
211+
complicatedArgs {
212+
nonNullIntArgField(nonNullIntArg: null)
213+
}
214+
}
215+
''', [
216+
bad_value('nonNullIntArg', 'Int!', 'null', 4, 51)
217+
])
218+
199219

200220
# noinspection PyMethodMayBeStatic
201221
class TestInvalidFloatValues(object):
@@ -642,7 +662,7 @@ def test_partial_object_missing_required(self):
642662
}
643663
''', [
644664
bad_value('complexArg', 'ComplexInput', '{intField: 4}', 4, 45, [
645-
'In field "requiredField": Expected "Boolean!", found null.'
665+
'In field "requiredField": Expected type "Boolean!", found null.'
646666
])
647667
])
648668

graphql/validation/tests/test_default_values_of_correct_type.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def test_variables_missing_required_field():
8787
}
8888
''', [
8989
bad_value('a', 'ComplexInput', '{intField: 3}', 2, 51, [
90-
'In field "requiredField": Expected "Boolean!", found null.'
90+
'In field "requiredField": Expected type "Boolean!", found null.'
9191
])
9292
])
9393

graphql/validation/tests/utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,16 @@ def expect_invalid(schema, rules, query, expected_errors, sort_list=True):
229229
for loc in error['locations']
230230
]
231231

232+
errors = list(map(format_error, errors))
232233
if sort_list:
233-
assert sort_lists(list(map(format_error, errors))) == sort_lists(expected_errors)
234+
expected_errors = sort_lists(expected_errors)
235+
errors = sort_lists(errors)
236+
msg = 'expected errors: %s, got errors: %s' % (expected_errors, errors)
237+
assert errors == expected_errors, msg
234238

235239
else:
236-
assert list(map(format_error, errors)) == expected_errors
240+
msg = 'expected errors: %s, got errors: %s' % (expected_errors, errors)
241+
assert errors == expected_errors, msg
237242

238243

239244
def expect_passes_rule(rule, query):

0 commit comments

Comments
 (0)