rete · Clients JavaScript — rete-graph

JavaScript API

rete-graph on npm is the JavaScript client for .rete files: the same WebAssembly engine behind the playground, packaged for browsers and Node with an idiomatic wrapper — parsed Term results, clean IRIs, the same API shape as the Python client.

npm install rete-graph
import { open, build } from "rete-graph";

const g = await open("https://data.graphplaza.com/boe/boe.rete"); // Node / worker
for (const row of g.query(`
    SELECT ?s ?label WHERE {
        ?s <http://www.w3.org/2000/01/rdf-schema#label> ?label
    } LIMIT 5`)) {
  console.log(row.s.value, "→", row.label.toJS());
}
console.log(g.stats()); // { fileLength, bytes, requests } — lazy, not a download

Or a single <script> tag — no bundler, no install

p5.js-style: one self-contained file (engine embedded), full and minified, served by any npm CDN:

<script src="https://cdn.jsdelivr.net/npm/rete-graph@0.3.0/dist/rete-graph.min.js"></script>
<script>
  (async () => {
    const bytes = new Uint8Array(await (await fetch("mydata.rete")).arrayBuffer());
    const g = await rete.open(bytes);
    console.log(g.query("SELECT ?s ?p ?o WHERE { ?s ?p ?o } LIMIT 3"));
  })();
</script>

…/dist/rete-graph.js is the readable twin; pin the version in the URL.

Where remote opens work

Remote graphs are read with synchronous XHR range requests (that's what lets the engine stay simple and synchronous):

Environmentopen(url)open(bytes)
Node ≥ 18✅ built-in sync-fetch bridge✅
Browser web worker✅ native sync XHR✅
Browser main thread❌ (browsers forbid sync binary XHR)✅

On a main thread, either fetch the bytes yourself (small files) or run the graph in a worker — the pattern the playground uses. Hosts must send CORS headers and honor Range (Hosting your .rete).

Query results

query() returns {variable: Term} rows for SELECT, a boolean for ASK, and [s, p, o] Term triples for CONSTRUCT/DESCRIBE. A Term has .kind ("iri" | "literal" | "bnode" | "triple"), .value, .datatype, .lang, plus .toJS() (number/boolean/BigInt for the common XSD types) and .n3.

g.query(q, { reason: true });   // OWL 2 QL entailment by query rewriting
g.queryRaw(q);                  // the engine's raw JSON envelope
g.prefixSearch("Berl");         // label autocomplete → [{label, subject}]
g.textSearch("volcano");        // full-text (files built with --text-index)
g.schema();                     // { classes: [[iri, n]], relations: [[s,p,o,n]] }
g.graphNames(); g.info(); g.quads;
g.contentHash();                // remote graphs: blake3-16 cache key
await build(ntText, "nt");      // RDF text → .rete bytes (Uint8Array); also "nq", "ttl", "trig"

RDF 1.2 and quoted triples

The CLI's --quoted-triple-syntax vocabulary and defaults, in both directions. build(text, "ttl" | "trig", { quotedTripleSyntax }) reads Turtle/TriG << s p o >> as a quoted triple under "rdf-star" (the default, as before) or as RDF 1.2 under "rdf12" (<<( s p o )>> triple terms, << … >> reifiers, {| … |} annotations). nquads() / writeNQuads() / toNQuads() take { quotedTripleSyntax } too, defaulting to "rdf12": a quoted triple is written as the RDF 1.2 triple term <<( s p o )>>, which Oxigraph 0.5 and Jena 5 load, and a subject-position one (no RDF 1.2 spelling) makes the stream throw; "rdf-star" writes rete's stored <<s p o>>. card().signals.quoted_triples says whether a file holds any, measured from its header on read, so files built before the signal existed answer it too. See Interop.

Comunica (and the RDF/JS ecosystem)

From 0.2.0 the package ships ReteSource, an RDF/JS Source that plugs .rete files into Comunica, LDflex, GraphQL-LD, and the Solid ecosystem:

import { open, ReteSource } from "rete-graph";
const source = new ReteSource(await open(bytesOrUrl));
// → sources: [source] in any Comunica QueryEngine

Comunica also talks to rete with zero code through the gateway's SPARQL endpoints (full query pushdown — prefer it for heavy joins over big remote files). Both levels, the exact semantics, and the which-level-when table live on the dedicated page: Comunica — rete in the RDF/JS ecosystem.

Feature matrix

CapabilityJSNotes
SPARQL SELECT / ASK / CONSTRUCT / DESCRIBE✅query()
Lazy remote open (HTTP Range)✅Node + browser workers
OWL 2 QL reasoned queries✅query(q, {reason: true})
Schema profile, prefix & text search✅clean IRIs everywhere
Build from RDF text✅build() — uncompressed, like the playground
Script-tag single-file build✅dist/rete-graph(.min).js, global rete
TypeScript types✅bundled index.d.ts
RDF/JS Source (Comunica, LDflex, GraphQL-LD)✅ 0.2.0ReteSource — see Comunica
SERVICE federation✅*via the engine; same worker/Node constraint
Dataset Card / embedded examples✅card() (with signals.quoted_triples), examples()
RDF 1.2 Turtle/TriG in, triple terms out✅quotedTripleSyntax on build() and the N-Quads writers
Custom headers / custom readers⏳planned
Builder (card, pyramid options)⏳use build() or the Python/CLI builders

For contributors — how the package builds its engine from the crates, the sync-XHR bridge, and the release procedure: Client development & releases.