Gremlin JavaScript Driver for Apache TinkerPop

3.8.1 · active · verified Sun Apr 19

The `gremlin` package provides the official JavaScript Gremlin Language Variant (GLV) for Apache TinkerPop, enabling developers to interact with any TinkerPop-enabled graph database. Currently stable at version 3.8.1, this driver is actively maintained with typically 3-4 releases per year, often aligning with major/minor TinkerPop versions. It differentiates itself by being the official Apache project, supporting bytecode-based traversals (the recommended approach over string-based scripts for better performance, portability, and security), and runs on Node.js (version 20 and higher) with experimental support for Web APIs. The driver establishes a WebSocket connection to a remote Gremlin Server or a compatible graph provider, translating JavaScript method calls into Gremlin traversals for execution on the server-side.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish a connection to a Gremlin Server, create a graph traversal source, execute a simple vertex count, add a new vertex with properties, and then count specific vertices, showcasing fundamental Gremlin operations.

import { Client } from 'gremlin/lib/driver/client';
import { DriverRemoteConnection } from 'gremlin/lib/driver/driver-remote-connection';
import { Graph } from 'gremlin/lib/structure/graph';
import { AnonymousTraversalSource } from 'gremlin/lib/process/anonymous-traversal-source';

const gremlinServerUrl = process.env.GREMLIN_SERVER_URL ?? 'ws://localhost:8182/gremlin';

async function runGremlinQuery() {
  let connection: DriverRemoteConnection | null = null;
  try {
    // Establish a remote connection to the Gremlin Server
    connection = new DriverRemoteConnection(gremlinServerUrl);
    
    // Create a GraphTraversalSource (g) for fluent Gremlin traversals
    const g = AnonymousTraversalSource.traversal().withRemote(connection);

    // Execute a simple traversal: count all vertices
    const vertexCount = await g.V().count().next();
    console.log(`Number of vertices in the graph: ${vertexCount.value}`);

    // Add a new vertex and retrieve it
    const newVertex = await g.addV('person').property('name', 'Alice').next();
    console.log(`Added vertex with ID: ${newVertex.value.id} and label: ${newVertex.value.label}`);

    // Count all 'person' vertices
    const personCount = await g.V().hasLabel('person').count().next();
    console.log(`Number of 'person' vertices: ${personCount.value}`);

  } catch (err) {
    console.error('Error during Gremlin traversal:', err);
  } finally {
    if (connection) {
      await connection.close();
      console.log('Gremlin connection closed.');
    }
  }
}

runGremlinQuery();

view raw JSON →