25
25
GraphQLInt ,
26
26
GraphQLList ,
27
27
GraphQLNonNull ,
28
+ GraphQLScalarType ,
28
29
GraphQLString ,
29
30
)
30
31
from graphql .utilities import ast_from_value
@@ -123,6 +124,33 @@ def converts_id_values_to_int_or_string_asts():
123
124
124
125
assert ast_from_value (INVALID , GraphQLString ) is None
125
126
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
+
126
154
def does_not_convert_non_null_values_to_null_value ():
127
155
non_null_boolean = GraphQLNonNull (GraphQLBoolean )
128
156
assert ast_from_value (None , non_null_boolean ) is None
@@ -162,12 +190,21 @@ def converts_list_singletons():
162
190
value = "FOO"
163
191
)
164
192
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" )]
169
200
)
170
201
202
+ input_obj = GraphQLInputObjectType (
203
+ "MyInputObj" ,
204
+ {"foo" : GraphQLInputField (GraphQLFloat ), "bar" : GraphQLInputField (my_enum )},
205
+ )
206
+
207
+ def converts_input_objects ():
171
208
assert ast_from_value ({"foo" : 3 , "bar" : "HELLO" }, input_obj ) == ObjectValueNode (
172
209
fields = [
173
210
ObjectFieldNode (
@@ -180,11 +217,6 @@ def converts_input_objects():
180
217
)
181
218
182
219
def converts_input_objects_with_explicit_nulls ():
183
- input_obj = GraphQLInputObjectType (
184
- "MyInputObj" ,
185
- {"foo" : GraphQLInputField (GraphQLFloat ), "bar" : GraphQLInputField (my_enum )},
186
- )
187
-
188
220
assert ast_from_value ({"foo" : None }, input_obj ) == ObjectValueNode (
189
221
fields = [ObjectFieldNode (name = NameNode (value = "foo" ), value = NullValueNode ())]
190
222
)
0 commit comments