Skip to content

Commit 1fb8111

Browse files
committed
value_from_ast: fixed coercing of null vars on non-nullable type
Replicates graphql/graphql-js@53afba1
1 parent d2a1766 commit 1fb8111

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/graphql/utilities/value_from_ast.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,6 @@ def value_from_ast(
5252
# Importantly, this is different from returning the value null.
5353
return Undefined
5454

55-
if is_non_null_type(type_):
56-
if isinstance(value_node, NullValueNode):
57-
return Undefined
58-
type_ = cast(GraphQLNonNull, type_)
59-
return value_from_ast(value_node, type_.of_type, variables)
60-
61-
if isinstance(value_node, NullValueNode):
62-
return None # This is explicitly returning the value None.
63-
6455
if isinstance(value_node, VariableNode):
6556
variable_name = value_node.name.value
6657
if not variables:
@@ -75,6 +66,15 @@ def value_from_ast(
7566
# is of the correct type.
7667
return variable_value
7768

69+
if is_non_null_type(type_):
70+
if isinstance(value_node, NullValueNode):
71+
return Undefined
72+
type_ = cast(GraphQLNonNull, type_)
73+
return value_from_ast(value_node, type_.of_type, variables)
74+
75+
if isinstance(value_node, NullValueNode):
76+
return None # This is explicitly returning the value None.
77+
7878
if is_list_type(type_):
7979
type_ = cast(GraphQLList, type_)
8080
item_type = type_.of_type

tests/utilities/test_value_from_ast.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ def converts_enum_values_according_to_input_coercion_rules():
6666
assert _value_from('"BLUE"', test_enum) is Undefined
6767
assert _value_from("null", test_enum) is None
6868
assert _value_from("NULL", test_enum) is None
69+
assert _value_from("NULL", GraphQLNonNull(test_enum)) is None
6970
assert isnan(_value_from("NAN", test_enum))
7071
assert _value_from("NO_CUSTOM_VALUE", test_enum) is Undefined
7172

@@ -153,6 +154,7 @@ def accepts_variable_values_assuming_already_coerced():
153154
assert _value_from("$var", GraphQLBoolean, {}) is Undefined
154155
assert _value_from("$var", GraphQLBoolean, {"var": True}) is True
155156
assert _value_from("$var", GraphQLBoolean, {"var": None}) is None
157+
assert _value_from("$var", non_null_bool, {"var": None}) is Undefined
156158

157159
def asserts_variables_are_provided_as_items_in_lists():
158160
assert _value_from("[ $foo ]", list_of_bool, {}) == [None]

0 commit comments

Comments
 (0)