gremlin-javascript example only returns Pending Promises

Viewed 741

I have been trying to make gremlin work with nodejs. I found out that i could use one of the gremlin driver gremlin-javascript to achieve this. A quick workable script is as follow:

const gremlin = require('gremlin');
const Graph = gremlin.structure.Graph;
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const graph = new Graph();

const g = graph.traversal().withRemote(new DriverRemoteConnection('ws://localhost:8182/gremlin', { traversalSource: 'g' }));

const addUser1 = async () => {
    const newVertex = await g.addV("id", 1, "label", "person", "name", "marko", "age", 29)
    console.log("Vertex added: ", newVertex)
}

const traverse = async () => {
    const traverse = await g.V().has("name", "marko")
    console.log(traverse)
}

addUser1()
traverse()

Now running this script returns me a Pending Promise of some sort. And I don't need that. What I really need are the nodes not some form of object. Result for addUser1() is given as follows:

Promise {
  <pending>,
  domain:
   Domain {
     domain: null,
     _events: { error: [Function: debugDomainError] },
     _eventsCount: 1,
     _maxListeners: undefined,
     members: [ [Object] ] } }
> Vertex added:  GraphTraversal {
  graph: Graph {},
  traversalStrategies: TraversalStrategies { strategies: [ [Object] ] },
  bytecode: Bytecode { sourceInstructions: [], stepInstructions: [ [Array] ] },
  traversers: null,
  sideEffects: null,
  _traversalStrategiesPromise: null,
  _traversersIteratorIndex: 0 }

And for the traverse():

Promise {
  <pending>,
  domain:
   Domain {
     domain: null,
     _events: { error: [Function: debugDomainError] },
     _eventsCount: 1,
     _maxListeners: undefined,
     members: [ [Object] ] } }
> GraphTraversal {
  graph: Graph {},
  traversalStrategies: TraversalStrategies { strategies: [ [Object] ] },
  bytecode:
   Bytecode {
     sourceInstructions: [],
     stepInstructions: [ [Array], [Array] ] },
  traversers: null,
  sideEffects: null,
  _traversalStrategiesPromise: null,
  _traversersIteratorIndex: 0 }

I don't really understand the problem actually. Isn't there a comprehensive guide that could show a simple working example. Your help in making me understand the issue would be highly appreciated.

1 Answers

initial issue with gremlin-client not connecting

I faced the same issue when i upgraded my gremlin-client from 3.4.6 to 3.4.7. I'm not sure what changed but I found that specifying the traversalSource when creating DriverRemoteConnection seems to resolve the error.

const g = traversal().withRemote(new DriverRemoteConnection('ws://localhost:8182/gremlin', { traversalSource: 'g' }));

updated issue regarding get pending promises

Your addUser1 query is missing a terminal step. You may be used to using the gremlin console with automatically adds a terminal step. However for the javascript client, you need manually terminate the Iterator with a .next() Stephen Mallette explains this very well in this answer: https://stackoverflow.com/a/34019326/3834802

Additionally, the promises returned by the queries will not contain the entire node. For the g.addV, the completed promise will indicate if the node was added successfully or not. For g.v().has(), you will need to specify what fields you require. In the corrected code below, I'm fetching the age for marko.

Finally, the g.addV(key1, value1, key2, value2, ...) format is not one I have encountered before or tested. g.addvV(label).property(key1,value1).property(key2,value2) is the format I am familiar with so I have included this in my answer below.

const gremlin = require('gremlin');
const Graph = gremlin.structure.Graph;
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const graph = new Graph();

const g = graph.traversal().withRemote(new DriverRemoteConnection('ws://localhost:8182/gremlin', { traversalSource: 'g' }));

const addUser1 = async () => {
    try {
        const newVertex = await g.addV('person').property('id',1).property('name','marko').property('age',29).next()
        //curious what this console log prints 
        console.log("Vertex added: ", newVertex)
    } catch (e) {
        console.error(e);
    }

}

const traverse = async () => {
    try {
        const traverse = await g.V().has("name", "marko").values('age').toList()
        console.log(traverse)
    } catch (e) {
        console.error(e)
    }
}

addUser1()
traverse()
Related