From 5d5e0fa4b566822b36924be971f299ae45e8748c Mon Sep 17 00:00:00 2001 From: Ruslan Bakiev Date: Thu, 8 Jan 2026 00:36:16 +0700 Subject: [PATCH] Add nearestNodes query for coordinate-based search --- geo_app/schema.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/geo_app/schema.py b/geo_app/schema.py index 7a8335b..80b273d 100644 --- a/geo_app/schema.py +++ b/geo_app/schema.py @@ -103,6 +103,14 @@ class Query(graphene.ObjectType): description="Get total count of nodes (with optional transport filter)", ) + nearest_nodes = graphene.List( + NodeType, + lat=graphene.Float(required=True, description="Latitude"), + lon=graphene.Float(required=True, description="Longitude"), + limit=graphene.Int(default_value=5, description="Max results"), + description="Find nearest logistics nodes to given coordinates", + ) + node_connections = graphene.Field( NodeConnectionsType, uuid=graphene.String(required=True), @@ -319,6 +327,41 @@ class Query(graphene.ObjectType): cursor = db.aql.execute(aql, bind_vars={'transport_type': transport_type}) return next(cursor, 0) + def resolve_nearest_nodes(self, info, lat, lon, limit=5): + """Find nearest logistics nodes to given coordinates.""" + db = get_db() + + # Get all logistics nodes and calculate distance + aql = """ + FOR node IN nodes + FILTER node.node_type == 'logistics' OR node.node_type == null + FILTER node.latitude != null AND node.longitude != null + LET dist = DISTANCE(node.latitude, node.longitude, @lat, @lon) / 1000 + SORT dist ASC + LIMIT @limit + RETURN MERGE(node, {distance_km: dist}) + """ + cursor = db.aql.execute( + aql, + bind_vars={'lat': lat, 'lon': lon, 'limit': limit}, + ) + + nodes = [] + for node in cursor: + nodes.append(NodeType( + uuid=node['_key'], + name=node.get('name'), + latitude=node.get('latitude'), + longitude=node.get('longitude'), + country=node.get('country'), + country_code=node.get('country_code'), + synced_at=node.get('synced_at'), + transport_types=node.get('transport_types') or [], + edges=[], + )) + + return nodes + def resolve_node_connections(self, info, uuid, limit_auto=12, limit_rail=12): """Get auto edges from hub and rail edges from nearest rail node.""" db = get_db()