Skip to content

Commit 99b62e8

Browse files
committed
Correct outdated documentation
Replicates graphql/graphql-js@da57238
1 parent 75bb5a0 commit 99b62e8

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/graphql/type/definition.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)