2626 GraphQLInt ,
2727 GraphQLList ,
2828 GraphQLNonNull ,
29+ GraphQLScalarType ,
2930 GraphQLString ,
3031)
3132from 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