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 | |||||
from .content import SearchContentNode | |||||
from .release import SearchReleaseNode | |||||
from .revision import SearchRevisionNode | |||||
class SearchResultNode(BaseNode): | |||||
@property | |||||
def targetType(self): | |||||
return self._node.__class__.__name__.lower() | |||||
@property | |||||
def match(self): | |||||
return None | |||||
@property | |||||
def target(self): | |||||
# return the right node resolver | |||||
# not using the ariadne resolver here | |||||
# it is not needed as the result field will never | |||||
# accept extra arguments | |||||
# node_data is already avialble here | |||||
resolvers = { | |||||
"revision": SearchRevisionNode, | |||||
"release": SearchReleaseNode, | |||||
"content": SearchContentNode, | |||||
} | |||||
resolver = resolvers[self.targetType] | |||||
return resolver(obj=self, info=self.info, node_data=self._node) | |||||
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 | |||||
} | |||||
} | |||||
""" | |||||
# add more categories as needed | |||||
# advanced query language will be handled implicitly in | |||||
# metadata and origins | |||||
_node_class = SearchResultNode | |||||
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 = PagedResult( | |||||
results=archive.Archive().search_in_swhids(query), next_page_token=None | |||||
) | |||||
elif category == "metadata": | |||||
# FIXME, validate the query language as a key expression in the utils | |||||
results = archive.Archive().search_in_metadata(query) | |||||
elif category == "origin": | |||||
# FIXME, validate the query language as a key expression in the utils | |||||
# 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 results |