2

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.

gameFrenzy07
  • 357
  • 1
  • 4
  • 16
  • Which version of the driver and node are you using and what graph database are you trying to connect to? – bechbd Jun 11 '20 at 15:07

1 Answers1

4

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()
sameetm
  • 58
  • 4
  • 1
    i would just add this for further clarification if anyone has issue: `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' }));` – gameFrenzy07 Jun 15 '20 at 07:15
  • one problem that i ran into is that all my promises remain in pending state with this approach...can i truly attribute this problem to not having a connection to DriverRemoteConnection()? because otherwise i don't find any problem in my code...can you please care to comment...that would be huge help – gameFrenzy07 Jun 17 '20 at 05:30
  • When you say promises, Im assuming you mean the promise returned by the gremlin query. Could you update your post to show an example of your queries? – sameetm Jun 17 '20 at 22:36
  • i have updated the question body. I hope it is what you told me. – gameFrenzy07 Jun 18 '20 at 07:36
  • Thanks, I've updated my answer to answer your new question. Can you please add the original issue back to your question so others with a similar issue can refer to it? I should have asked you to create a new question but oh well – sameetm Jun 19 '20 at 17:41
  • sorry for bothering you again and again but the Pending Promise issue still persists: , domain: Domain { domain: null, _events: { error: [Function: debugDomainError] }, _eventsCount: 1, _maxListeners: undefined, members: [ [Object] ] } } – gameFrenzy07 Jun 22 '20 at 07:14
  • this is what i am given for both functions...is this the intended behaviour? can u confirm that this is what you get too? – gameFrenzy07 Jun 22 '20 at 07:15
  • Do you have anymore hints when traversalSource got added and what the documentation for it is? Had issues while upgrading and found this post as the only one referencing my own issue :) https://stackoverflow.com/questions/62624133/upgrading-to-anonymoustraversalsource-gremlin-3-3-5-node-js – Zanndorin Jun 28 '20 at 15:30
  • @Zanndorin it was a matter of trail and error as many things have been on this gremlin/janusgraph journey :) – sameetm Jul 01 '20 at 00:28