26
26
GraphQLInt ,
27
27
GraphQLList ,
28
28
GraphQLNonNull ,
29
+ GraphQLScalarType ,
29
30
GraphQLString ,
30
31
)
31
32
from graphql .utilities import ast_from_value
@@ -124,6 +125,33 @@ def converts_id_values_to_int_or_string_asts():
124
125
125
126
assert ast_from_value (Undefined , GraphQLString ) is None
126
127
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
+
127
155
def does_not_convert_non_null_values_to_null_value ():
128
156
non_null_boolean = GraphQLNonNull (GraphQLBoolean )
129
157
assert ast_from_value (None , non_null_boolean ) is None
@@ -170,12 +198,21 @@ def converts_list_singletons():
170
198
value = "FOO"
171
199
)
172
200
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" )]
177
208
)
178
209
210
+ input_obj = GraphQLInputObjectType (
211
+ "MyInputObj" ,
212
+ {"foo" : GraphQLInputField (GraphQLFloat ), "bar" : GraphQLInputField (my_enum )},
213
+ )
214
+
215
+ def converts_input_objects ():
179
216
assert ast_from_value ({"foo" : 3 , "bar" : "HELLO" }, input_obj ) == ObjectValueNode (
180
217
fields = [
181
218
ObjectFieldNode (
@@ -188,11 +225,9 @@ def converts_input_objects():
188
225
)
189
226
190
227
def converts_input_objects_with_explicit_nulls ():
191
- input_obj = GraphQLInputObjectType (
192
- "MyInputObj" ,
193
- {"foo" : GraphQLInputField (GraphQLFloat ), "bar" : GraphQLInputField (my_enum )},
194
- )
195
-
196
228
assert ast_from_value ({"foo" : None }, input_obj ) == ObjectValueNode (
197
229
fields = [ObjectFieldNode (name = NameNode (value = "foo" ), value = NullValueNode ())]
198
230
)
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