couldnt able to create edge in gremlin

39 views Asked by At

I tried to run the query below:

edge = g.V(id).addE("rated").to(g.V(n_id)).next()

but I am getting the error

InvalidRequest: Error from server: code=2200 [Invalid query] message="Could not read the traversal from the request sent. Error was: Could not locate method: DefaultGraphTraversal.addE([rated, [GraphStep(vertex,[{source_id=34897306, source_system=MSI_CLIENT, ~label=person}])]])"

Both g.V(id) and g.V(n_id) is returning vertices and I don't see any issues with that. Can some one please let me know why I am facing above error ?

1

There are 1 answers

0
Ethan Posner On

gremlinpython has a quirk which prevents you from nesting traversals within other traversals in this way. When you nest V() within a traversal, you must use the __ class like so: __.V(). You can import the __ class as from gremlin_python.process.graph_traversal import __.

The below code should work:

from gremlin_python.process.anonymous_traversal import traversal
from gremlin_python.process.graph_traversal import __, GraphTraversalSource
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection

GRAPH_DB_HOST = "your db host"
GRAPH_DB_USER = "your db user"
GRAPH_DB_PASSWORD = "your db password"
GREMLIN_SEVER_PORT = "8182"
GRAPH_DB_URL = f"ws://{GRAPH_DB_HOST}:{GREMLIN_SEVER_PORT}/gremlin"

g: GraphTraversalSource = traversal().withRemote(
    DriverRemoteConnection(GRAPH_DB_URL, 'g',
                           username=GRAPH_DB_USER,
                           password=GRAPH_DB_PASSWORD)
)

edge = g.V(id).addE("rated").to(__.V(n_id)).next()