Search code examples
javascriptgremlingremlin-server

Working gremlin javascript example


There is a new version out, but the documentation is somewhat lacking a working example.

Github Ticket: https://github.com/jbmusso/gremlin-javascript/issues/109

I've been trying to get an example to work. Any help appreciated:

gremlin-server: 3.3.2 with config gremlin-server-modern.yaml
npm gremlin lib: 3.3.2

import gremlin from 'gremlin';
import DriverRemoteConnection from 'gremlin/lib/driver/driver-remote-connection';
import { Graph } from 'gremlin/lib/structure/graph';
const graph = new Graph()
const g = graph.traversal().withRemote(new DriverRemoteConnection('ws://localhost:8182/gremlin', { mimeType: 'application/vnd.gremlin-v3.0+json' }));

const fetchById = async (id) => {
  const result = await g.V(id).toList()
  console.log(result);
}

const addUser = async (name) => {
  const newVertex = await g.addV().property('name','marko').property('name','marko a. rodriguez').next()
  console.log(newVertex)
}

addUser()
fetchById(0)

Current Output:

[]
{ value: null, done: true }

Solution

  • UPDATE

    Gremlin JavaScript now supports GraphSON3 and the latest Gremlin Server.

    Working example:

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

    Obtain a traversal source (g):

    const graph = new Graph();
    const connection = new DriverRemoteConnection('ws://localhost:8182/gremlin');
    const g = graph.traversal().withRemote(connection);
    

    Once you have a traversal source (g), reuse it across your application to create traversals, for example:

    // Get the friends' names
    const friends = await g.V().has("name","Matt")
                           .out("knows").values("name").toList();
    

    See more information on the documentation: https://tinkerpop.apache.org/docs/current/reference/#gremlin-javascript

    ORIGINAL ANSWER

    Gremlin JavaScript doesn't support GraphSON3 serialization, which is the default in TinkerPop 3.3+. This causes your response to not be properly parsed.

    I've filed a ticket to support GraphSON3 in the JavaScript GLV: https://issues.apache.org/jira/browse/TINKERPOP-1943

    In the meantime, as a workaround, you can add GraphSON2 serializers to the server by adding the following line to your yaml, below serializers:

    - { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerGremlinV2d0, config: { ioRegistries: [org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerIoRegistryV2d0] }}