Changeset View
Changeset View
Standalone View
Standalone View
swh/graphql/resolvers/search.py
- This file was added.
# Copyright (C) 2022 The Software Heritage developers | |||||
# See the AUTHORS file at the top-level directory of this distribution | |||||
# License: GNU General Public License version 3, or any later version | |||||
# See top-level LICENSE file for more information | |||||
from swh.graphql.backends import archive | |||||
from swh.storage.interface import PagedResult | |||||
from .base_connection import BaseConnection | |||||
from .base_node import BaseNode | |||||
class SearchResult(BaseNode): | |||||
# match: int | |||||
@property | |||||
def resultType(self): | |||||
self._node.is_type_of() | |||||
@property | |||||
def match(self): | |||||
return 10 | |||||
class SearchResultConnection(BaseConnection): | |||||
""" | |||||
Category is a mandatory argument, get one category per | |||||
query | |||||
different categories can be requested in one request with | |||||
alias in query | |||||
eg: | |||||
query mysearch { | |||||
ori-search: search(query: "test", category: "origin") { | |||||
type | |||||
} | |||||
meta-search: search(query: "test", category: "metadata") { | |||||
type | |||||
} | |||||
swhid-search: search(query: "test", category: "swhid") { | |||||
type | |||||
} | |||||
} | |||||
""" | |||||
# client UI can have a result page with multiple tabs | |||||
# each category will add a new tab in the UI | |||||
# add more categories as needed | |||||
# advanced query language will be handled implicitly in | |||||
# metadata and origins | |||||
_node_class = SearchResult | |||||
def _get_paged_result(self): | |||||
query = self.kwargs.get("query") | |||||
category = self.kwargs.get("category") | |||||
# FIXME, use a search factory instead, either in this module | |||||
# or in a separate package | |||||
if category == "swhid": | |||||
results = archive.Archive().search_in_swhids(query) | |||||
elif category == "metadata": | |||||
results = archive.Archive().search_in_metadata(query) | |||||
elif category == "origin": | |||||
# this will return a homogeneous list of origin types | |||||
# still using the same SearchResult type to return result | |||||
# FIXME, maybe it is better to add a type OriginSearchResult | |||||
results = archive.Archive().search_in_origins(query) | |||||
return PagedResult(results=results, next_page_token=None) |