From 16557321c8741b0f7becdedaf0dfe7ea63b0f356 Mon Sep 17 00:00:00 2001 From: Ravi Raja Merugu Date: Sun, 27 Nov 2022 14:36:18 +0530 Subject: [PATCH] order by added to getVertices/getEdges methods for speeding the default order_by=id behaviour --- invana_engine/graph/query_resolvers.py | 32 ++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/invana_engine/graph/query_resolvers.py b/invana_engine/graph/query_resolvers.py index b3bdc66..0f916da 100644 --- a/invana_engine/graph/query_resolvers.py +++ b/invana_engine/graph/query_resolvers.py @@ -35,8 +35,16 @@ class GremlinGenericQuerySchema: execute_query = graphene.Field(QueryResponseData, timeout=graphene.Int(default_value=DEFAULT_QUERY_TIMEOUT), gremlin=graphene.String()) - get_vertices = graphene.Field(graphene.List(NodeType), filters=graphene.JSONString(), - limit=graphene.Int(default_value=DEFAULT_PAGINATION_SIZE), skip=graphene.Int()) + get_vertices = graphene.Field(graphene.List(NodeType), + filters=graphene.JSONString(), + order_by=graphene.String(), + limit=graphene.Int(default_value=DEFAULT_PAGINATION_SIZE), + skip=graphene.Int()) + get_edges = graphene.Field(graphene.List(EdgeType), + filters=graphene.JSONString(), + limit=graphene.Int(default_value=DEFAULT_PAGINATION_SIZE), + order_by=graphene.String(), + skip=graphene.Int()) get_or_create_vertex = graphene.Field(getOrCreateNodeType, label=graphene.String(), properties=graphene.JSONString()) @@ -47,11 +55,27 @@ def resolve_execute_query(self, info: graphene.ResolveInfo, gremlin: str, timeou return {"data": [d.to_json() if hasattr(d, "to_json") else d for d in response.data] if response.data else []} def resolve_get_vertices(self, info: graphene.ResolveInfo, filters: dict = None, + order_by: str = None, limit: int = DEFAULT_PAGINATION_SIZE, skip: int = 0): filters = {} if filters is None else filters - data = info.context['request'].app.state.graph.vertex.search( + _ = info.context['request'].app.state.graph.vertex.search( limit=limit, skip=skip, **filters - ).order_by('id').range(skip, limit).to_list() + ) + if order_by: + _.order_by(order_by) + data = _.range(skip, limit).to_list() + return [datum.to_json() for datum in data] + + def resolve_get_edges(self, info: graphene.ResolveInfo, filters: dict = None, + order_by: str = None, + limit: int = DEFAULT_PAGINATION_SIZE, skip: int = 0): + filters = {} if filters is None else filters + _ = info.context['request'].app.state.graph.edge.search( + limit=limit, skip=skip, **filters + ) + if order_by: + _.order_by(order_by) + data = _.range(skip, limit).to_list() return [datum.to_json() for datum in data] def resolve_get_or_create_vertex(self, info: graphene.ResolveInfo, label: str = None,