File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -319,14 +319,22 @@ class GraphQLScalarType(GraphQLNamedType):
319319 and are defined with a name and a series of functions used to parse input from ast
320320 or variables and to ensure validity.
321321
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 .
324324
325325 Example::
326326
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
330338
331339 odd_type = GraphQLScalarType('Odd', serialize=serialize_odd)
332340
You can’t perform that action at this time.
0 commit comments