Changeset View
Changeset View
Standalone View
Standalone View
swh/graphql/resolvers/search.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.storage.interface import PagedResult | from swh.storage.interface import PagedResult | ||||
from .base_connection import BaseConnection, BaseList | from .base_connection import BaseConnection, BaseList | ||||
from .base_node import BaseNode | from .base_node import BaseNode | ||||
class SearchResultNode(BaseNode): | class SearchResultNode(BaseNode): | ||||
""" """ | """ """ | ||||
@property | @property | ||||
def targetType(self): # To support the schema naming convention | def target_type(self): | ||||
return self._node.type | return self._node.type | ||||
@property | |||||
def target_id(self): | |||||
return self._node.target_id | |||||
class ResolveSwhidList(BaseList): | class ResolveSwhidList(BaseList): | ||||
_node_class = SearchResultNode | _node_class = SearchResultNode | ||||
def _get_results(self) -> list: | def _get_results(self) -> list: | ||||
swhid = self.kwargs.get("swhid") | swhid = self.kwargs.get("swhid") | ||||
results = [] | results = [] | ||||
if self.archive.is_object_available(swhid.object_id, swhid.object_type): | if self.archive.is_object_available(swhid.object_id, swhid.object_type): | ||||
results = [ | results = [ | ||||
{ | { | ||||
"target_hash": swhid.object_id, | "target_id": swhid.object_id, | ||||
"type": swhid.object_type.name.lower(), | "type": swhid.object_type.name.lower(), | ||||
} | } | ||||
] | ] | ||||
return results | return results | ||||
class SearchConnection(BaseConnection): | class SearchConnection(BaseConnection): | ||||
_node_class = SearchResultNode | _node_class = SearchResultNode | ||||
def _get_paged_result(self) -> PagedResult: | def _get_paged_result(self) -> PagedResult: | ||||
origins = self.search.get_origins( | origins = self.search.get_origins( | ||||
query=self.kwargs.get("query"), | query=self.kwargs.get("query"), | ||||
after=self._get_after_arg(), | after=self._get_after_arg(), | ||||
first=self._get_first_arg(), | first=self._get_first_arg(), | ||||
) | ) | ||||
# FIXME hard coding type to origin for now, as it is the only searchable object | # FIXME hard coding type to origin for now, as it is the only searchable object | ||||
results = [ | results = [ | ||||
{"target_url": ori["url"], "type": "origin"} for ori in origins.results | {"target_id": ori["url"], "type": "origin"} for ori in origins.results | ||||
] | ] | ||||
return PagedResult(results=results, next_page_token=origins.next_page_token) | return PagedResult(results=results, next_page_token=origins.next_page_token) |