Changeset View
Changeset View
Standalone View
Standalone View
swh/graphql/resolvers/scalars.py
# Copyright (C) 2022 The Software Heritage developers | # Copyright (C) 2022 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import datetime | import datetime | ||||
from typing import Optional | from typing import Optional, Union | ||||
from ariadne import ScalarType | from ariadne import ScalarType | ||||
from swh.graphql.errors import InvalidInputError | from swh.graphql.errors import InvalidInputError | ||||
from swh.graphql.utils import utils | from swh.graphql.utils import utils | ||||
from swh.model.exceptions import ValidationError | from swh.model.exceptions import ValidationError | ||||
from swh.model.swhids import CoreSWHID | from swh.model.swhids import CoreSWHID | ||||
datetime_scalar = ScalarType("DateTime") | datetime_scalar = ScalarType("DateTime") | ||||
swhid_scalar = ScalarType("SWHID") | swhid_scalar = ScalarType("SWHID") | ||||
id_scalar = ScalarType("ID") | id_scalar = ScalarType("ID") | ||||
@id_scalar.serializer | @id_scalar.serializer | ||||
def serialize_id(value) -> str: | def serialize_id(value: Union[bytes, str]) -> str: | ||||
if type(value) is str: | if isinstance(value, bytes): | ||||
value = value.encode() | |||||
return value.hex() | return value.hex() | ||||
return value | |||||
@datetime_scalar.serializer | @datetime_scalar.serializer | ||||
def serialize_datetime(value: Optional[datetime.datetime]) -> Optional[str]: | def serialize_datetime(value: Optional[datetime.datetime]) -> Optional[str]: | ||||
return utils.get_formatted_date(value) if type(value) == datetime.datetime else None | return utils.get_formatted_date(value) if type(value) == datetime.datetime else None | ||||
@swhid_scalar.value_parser | @swhid_scalar.value_parser | ||||
Show All 11 Lines |