2525 GraphQLInt ,
2626 GraphQLList ,
2727 GraphQLNonNull ,
28+ GraphQLScalarType ,
2829 GraphQLString ,
2930)
3031from graphql .utilities import ast_from_value
@@ -123,6 +124,33 @@ def converts_id_values_to_int_or_string_asts():
123124
124125 assert ast_from_value (INVALID , GraphQLString ) is None
125126
127+ def converts_using_serialize_from_a_custom_scalar_type ():
128+ pass_through_scalar = GraphQLScalarType (
129+ "PassThroughScalar" , serialize = lambda value : value ,
130+ )
131+
132+ assert ast_from_value ("value" , pass_through_scalar ) == StringValueNode (
133+ value = "value"
134+ )
135+
136+ return_null_scalar = GraphQLScalarType (
137+ "ReturnNullScalar" , serialize = lambda value : None ,
138+ )
139+
140+ assert ast_from_value ("value" , return_null_scalar ) is None
141+
142+ class SomeClass :
143+ pass
144+
145+ return_custom_class_scalar = GraphQLScalarType (
146+ "ReturnCustomClassScalar" , serialize = lambda value : SomeClass (),
147+ )
148+
149+ with raises (TypeError ) as exc_info :
150+ ast_from_value ("value" , return_custom_class_scalar )
151+ msg = str (exc_info .value )
152+ assert msg == "Cannot convert value to AST: <SomeClass instance>"
153+
126154 def does_not_convert_non_null_values_to_null_value ():
127155 non_null_boolean = GraphQLNonNull (GraphQLBoolean )
128156 assert ast_from_value (None , non_null_boolean ) is None
@@ -162,12 +190,21 @@ def converts_list_singletons():
162190 value = "FOO"
163191 )
164192
165- def converts_input_objects ():
166- input_obj = GraphQLInputObjectType (
167- "MyInputObj" ,
168- {"foo" : GraphQLInputField (GraphQLFloat ), "bar" : GraphQLInputField (my_enum )},
193+ def skips_invalid_list_items ():
194+ ast = ast_from_value (
195+ ["FOO" , None , "BAR" ], GraphQLList (GraphQLNonNull (GraphQLString ))
196+ )
197+
198+ assert ast == ListValueNode (
199+ values = [StringValueNode (value = "FOO" ), StringValueNode (value = "BAR" )]
169200 )
170201
202+ input_obj = GraphQLInputObjectType (
203+ "MyInputObj" ,
204+ {"foo" : GraphQLInputField (GraphQLFloat ), "bar" : GraphQLInputField (my_enum )},
205+ )
206+
207+ def converts_input_objects ():
171208 assert ast_from_value ({"foo" : 3 , "bar" : "HELLO" }, input_obj ) == ObjectValueNode (
172209 fields = [
173210 ObjectFieldNode (
@@ -180,11 +217,6 @@ def converts_input_objects():
180217 )
181218
182219 def converts_input_objects_with_explicit_nulls ():
183- input_obj = GraphQLInputObjectType (
184- "MyInputObj" ,
185- {"foo" : GraphQLInputField (GraphQLFloat ), "bar" : GraphQLInputField (my_enum )},
186- )
187-
188220 assert ast_from_value ({"foo" : None }, input_obj ) == ObjectValueNode (
189221 fields = [ObjectFieldNode (name = NameNode (value = "foo" ), value = NullValueNode ())]
190222 )
0 commit comments