Complete a Scalar or Enum by serializing to a valid value, returning null if serialization is not possible.

def complete_leaf_value(
    return_type,  # type: Union[GraphQLEnumType, GraphQLScalarType]
    path,  # type: List[Union[int, str]]
    result,  # type: Any
):
    # type: (...) -> Union[int, str, float, bool]
    """
    Complete a Scalar or Enum by serializing to a valid value, returning null if serialization is not possible.
    """
    assert hasattr(return_type, "serialize"), "Missing serialize method on type"
    serialized_result = return_type.serialize(result)

    if serialized_result is None:
        raise GraphQLError(
            ('Expected a value of type "{}" but ' + "received: {}").format(
                return_type, result
            ),
            path=path,
        )
    return serialized_result