Skip to content

WebAssembly API

Run Grafeo in the browser, Deno or Cloudflare Workers. ~660 KB gzipped.

npm install @grafeo-db/wasm

Quick Start

import init, { Database } from '@grafeo-db/wasm';

await init();
const db = new Database();

db.execute("INSERT (:Person {name: 'Alix', age: 30})");
db.execute("INSERT (:Person {name: 'Gus', age: 25})");

const results = db.execute("MATCH (p:Person) RETURN p.name, p.age");
console.log(results); // [{name: "Alix", age: 30}, {name: "Gus", age: 25}]

Database

const db = new Database();   // in-memory (all WASM databases are in-memory)

Query Methods

db.execute(gql);                              // GQL: returns array of row objects
db.executeRaw(gql);                          // GQL: returns {columns, rows, executionTimeMs}
db.executeWithParams(gql, params);           // GQL with parameter binding
db.executeWithLanguage(query, language);     // "gql", "cypher", "graphql", etc.
db.executeWithLanguageAndParams(query, language, params);  // language + params
db.executeCypher(query);                     // Cypher shorthand
db.executeGremlin(query);                    // Gremlin shorthand
db.executeGraphql(query);                    // GraphQL shorthand
db.executeSparql(query);                     // SPARQL shorthand (requires rdf feature)
db.executeSql(query);                        // SQL/PGQ shorthand
db.executeRawWithLanguage(query, language);  // raw result with language selection

Properties

db.nodeCount();   // number of nodes
db.edgeCount();   // number of edges
db.schema();      // database schema as JSON
Database.version(); // Grafeo version string

Create BM25 text indexes and run full-text queries:

db.createTextIndex("Document", "content");
const results = db.textSearch("Document", "content", "graph database", 10);
// [{nodeId, score}, ...]

db.rebuildTextIndex("Document", "content");
db.dropTextIndex("Document", "content");

Combine BM25 text scores with HNSW vector similarity:

// Create indexes via GQL queries
db.execute("CREATE TEXT INDEX ON Document(content)");
db.execute("CREATE VECTOR INDEX ON Document(embedding) OPTIONS {dimensions: 384}");

const results = db.hybridSearch(
    "Document",
    "content", "graph database",     // text field + query
    "embedding", queryVector,         // vector field + query
    10                                // top-k
);

Vector Index Creation

The createVectorIndex method is not available in the WASM bindings. Use a GQL CREATE VECTOR INDEX query via db.execute() instead.

Batch Import

Load structured data in a single call, avoiding per-row query overhead.

LPG Import

const result = db.importLpg({
    nodes: [
        { labels: ["Person"], properties: { name: "Alix", age: 30 } },
        { labels: ["Person"], properties: { name: "Gus", age: 25 } },
    ],
    edges: [
        { source: 0, target: 1, type: "KNOWS", properties: { since: 2020 } }
    ]
});
console.log(result); // { nodes: 2, edges: 1 }

Edge source and target are zero-based indexes into the nodes array from the same call.

RDF Import

Requires the rdf feature flag.

const result = db.importRdf({
    triples: [
        {
            subject: "http://example.org/Alix",
            predicate: "http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
            object: "http://example.org/Person"
        },
        {
            subject: "http://example.org/Alix",
            predicate: "http://example.org/name",
            object: { value: "Alix" }
        },
        {
            subject: "http://example.org/Alix",
            predicate: "http://example.org/age",
            object: { value: "30", datatype: "http://www.w3.org/2001/XMLSchema#integer" }
        }
    ]
});
console.log(result); // { triples: 3 }

Objects can be a plain string (treated as IRI), or a structured literal with value, optional datatype and optional language fields.

Snapshots (Persistence)

Export/import the entire database as a binary snapshot for IndexedDB persistence:

// Export
const snapshot = db.exportSnapshot();
// Store in IndexedDB...

// Import
const db2 = Database.importSnapshot(snapshot);

Supported Query Languages

The WASM build supports query languages based on compile-time features:

Feature Language Default
gql GQL Yes
cypher Cypher No
sparql SPARQL No
gremlin Gremlin No
graphql GraphQL No
sql-pgq SQL/PGQ No

The full feature enables all languages. The default npm package includes only GQL to minimize bundle size.

Bundle Size

Build Size
Default (GQL only) ~660 KB gzipped
Full (all languages) ~800 KB gzipped