Changeset View
Changeset View
Standalone View
Standalone View
swh/graphql/resolvers/origin.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 | ||||
from swh.model.model import Origin | from swh.model.model import Origin | ||||
from swh.storage.interface import PagedResult | from swh.storage.interface import PagedResult | ||||
from .base_connection import BaseConnection | from .base_connection import BaseConnection | ||||
from .base_node import BaseSWHNode | from .base_node import BaseSWHNode | ||||
from .search import SearchResultNode | from .target import TargetNode | ||||
class BaseOriginNode(BaseSWHNode): | class BaseOriginNode(BaseSWHNode): | ||||
def is_type_of(self): | def is_type_of(self): | ||||
# is_type_of is required only when resolving a UNION type | # is_type_of is required only when resolving a UNION type | ||||
# This is for ariadne to return the right type | # This is for ariadne to return the right type | ||||
return "Origin" | return "Origin" | ||||
class OriginNode(BaseOriginNode): | class OriginNode(BaseOriginNode): | ||||
""" | """ | ||||
Node resolver for an origin requested directly with its URL | Node resolver for an origin requested directly with its URL | ||||
""" | """ | ||||
def _get_node_data(self): | def _get_node_data(self): | ||||
return self.archive.get_origin(self.kwargs.get("url")) | return self.archive.get_origin(self.kwargs.get("url")) | ||||
class TargetOriginNode(BaseOriginNode): | class TargetOriginNode(BaseOriginNode): | ||||
""" | """ | ||||
Node resolver for an origin requested as a target | Node resolver for an origin requested as a target | ||||
""" | """ | ||||
obj: SearchResultNode | obj: TargetNode | ||||
def _get_node_data(self): | def _get_node_data(self): | ||||
# The target origin URL is guaranteed to exist in the archive | # The target origin URL (target_id here) is guaranteed to exist in the archive | ||||
# Hence returning the origin object without any explicit check in the archive | # Hence returning the origin object without any explicit check in the archive | ||||
# This assumes that the search index and archive are in sync | # This assumes that the search index and archive are in sync | ||||
return Origin(self.obj.target_url) | return Origin(self.obj.target_id) | ||||
class OriginConnection(BaseConnection): | class OriginConnection(BaseConnection): | ||||
""" | """ | ||||
Connection resolver for the origins | Connection resolver for the origins | ||||
""" | """ | ||||
_node_class = BaseOriginNode | _node_class = BaseOriginNode | ||||
Show All 15 Lines |