Skip to content

Commit d0d24b8

Browse files
committed
test_ast_from_value: improve coverage
Also fix porting bug revealed in added test. Replicates graphql/graphql-js@34c5580
1 parent b361779 commit d0d24b8

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

src/graphql/utilities/ast_from_value.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
StringValueNode,
1515
ValueNode,
1616
)
17-
from ..pyutils import inspect, is_nullish, is_invalid
17+
from ..pyutils import FrozenList, inspect, is_nullish, is_invalid
1818
from ..type import (
1919
GraphQLID,
2020
GraphQLInputType,
@@ -77,7 +77,8 @@ def ast_from_value(value: Any, type_: GraphQLInputType) -> Optional[ValueNode]:
7777
item_type = type_.of_type
7878
if isinstance(value, Iterable) and not isinstance(value, str):
7979
value_nodes = [ast_from_value(item, item_type) for item in value]
80-
return ListValueNode(values=value_nodes)
80+
value_nodes = [item_node for item_node in value_nodes if item_node]
81+
return ListValueNode(values=FrozenList(value_nodes))
8182
return ast_from_value(value, item_type)
8283

8384
# Populate the fields of the input object by creating ASTs from each value in the
@@ -97,7 +98,7 @@ def ast_from_value(value: Any, type_: GraphQLInputType) -> Optional[ValueNode]:
9798
name=NameNode(value=field_name), value=field_value
9899
)
99100
)
100-
return ObjectValueNode(fields=field_nodes)
101+
return ObjectValueNode(fields=FrozenList(field_nodes))
101102

102103
if is_leaf_type(type_):
103104
# Since value is an internally represented value, it must be serialized to an

tests/utilities/test_ast_from_value.py

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
GraphQLInt,
2727
GraphQLList,
2828
GraphQLNonNull,
29+
GraphQLScalarType,
2930
GraphQLString,
3031
)
3132
from graphql.utilities import ast_from_value
@@ -124,6 +125,33 @@ def converts_id_values_to_int_or_string_asts():
124125

125126
assert ast_from_value(Undefined, GraphQLString) is None
126127

128+
def converts_using_serialize_from_a_custom_scalar_type():
129+
pass_through_scalar = GraphQLScalarType(
130+
"PassThroughScalar", serialize=lambda value: value,
131+
)
132+
133+
assert ast_from_value("value", pass_through_scalar) == StringValueNode(
134+
value="value"
135+
)
136+
137+
return_null_scalar = GraphQLScalarType(
138+
"ReturnNullScalar", serialize=lambda value: None,
139+
)
140+
141+
assert ast_from_value("value", return_null_scalar) is None
142+
143+
class SomeClass:
144+
pass
145+
146+
return_custom_class_scalar = GraphQLScalarType(
147+
"ReturnCustomClassScalar", serialize=lambda value: SomeClass(),
148+
)
149+
150+
with raises(TypeError) as exc_info:
151+
ast_from_value("value", return_custom_class_scalar)
152+
msg = str(exc_info.value)
153+
assert msg == "Cannot convert value to AST: <SomeClass instance>."
154+
127155
def does_not_convert_non_null_values_to_null_value():
128156
non_null_boolean = GraphQLNonNull(GraphQLBoolean)
129157
assert ast_from_value(None, non_null_boolean) is None
@@ -170,12 +198,21 @@ def converts_list_singletons():
170198
value="FOO"
171199
)
172200

173-
def converts_input_objects():
174-
input_obj = GraphQLInputObjectType(
175-
"MyInputObj",
176-
{"foo": GraphQLInputField(GraphQLFloat), "bar": GraphQLInputField(my_enum)},
201+
def skips_invalid_list_items():
202+
ast = ast_from_value(
203+
["FOO", None, "BAR"], GraphQLList(GraphQLNonNull(GraphQLString))
204+
)
205+
206+
assert ast == ListValueNode(
207+
values=[StringValueNode(value="FOO"), StringValueNode(value="BAR")]
177208
)
178209

210+
input_obj = GraphQLInputObjectType(
211+
"MyInputObj",
212+
{"foo": GraphQLInputField(GraphQLFloat), "bar": GraphQLInputField(my_enum)},
213+
)
214+
215+
def converts_input_objects():
179216
assert ast_from_value({"foo": 3, "bar": "HELLO"}, input_obj) == ObjectValueNode(
180217
fields=[
181218
ObjectFieldNode(
@@ -188,11 +225,9 @@ def converts_input_objects():
188225
)
189226

190227
def converts_input_objects_with_explicit_nulls():
191-
input_obj = GraphQLInputObjectType(
192-
"MyInputObj",
193-
{"foo": GraphQLInputField(GraphQLFloat), "bar": GraphQLInputField(my_enum)},
194-
)
195-
196228
assert ast_from_value({"foo": None}, input_obj) == ObjectValueNode(
197229
fields=[ObjectFieldNode(name=NameNode(value="foo"), value=NullValueNode())]
198230
)
231+
232+
def does_not_convert_non_object_values_as_input_objects():
233+
assert ast_from_value(5, input_obj) is None

0 commit comments

Comments
 (0)