File tree Expand file tree Collapse file tree 1 file changed +13
-5
lines changed Expand file tree Collapse file tree 1 file changed +13
-5
lines changed Original file line number Diff line number Diff line change @@ -319,14 +319,22 @@ class GraphQLScalarType(GraphQLNamedType):
319
319
and are defined with a name and a series of functions used to parse input from ast
320
320
or variables and to ensure validity.
321
321
322
- If a type's serialize function does not return a value (i.e. it returns ``None``),
323
- then no error will be included in the response.
322
+ If a type's serialize function returns ``None``, then an error will be raised and a
323
+ ``None`` value will be returned in the response. It is always better to validate .
324
324
325
325
Example::
326
326
327
- def serialize_odd(value):
328
- if value % 2 == 1:
329
- return value
327
+ def serialize_odd(value: Any) -> int:
328
+ try:
329
+ value = int(value)
330
+ except ValueError:
331
+ raise GraphQLError(
332
+ f"Scalar 'Odd' cannot represent '{value}'"
333
+ " since it is not an integer.")
334
+ if not value % 2:
335
+ raise GraphQLError(
336
+ f"Scalar 'Odd' cannot represent '{value}' since it is even.")
337
+ return value
330
338
331
339
odd_type = GraphQLScalarType('Odd', serialize=serialize_odd)
332
340
You can’t perform that action at this time.
0 commit comments