WorldsSdk facade does not care which engine answers SPARQL. The
sparqlEngine option accepts any implementation of SparqlEngineInterface.
Which path?
Built-in engine (zero config)
If you have an RDF/JSStore, the built-in engine covers SPARQL 1.1 & 1.2 with
timeout and abort handling — no adapter needed:
MemoryStore is the engine’s built-in RDF/JS store with full SPARQL Update
support (addQuad/removeQuad). If you use a different store (e.g. N3’s Store),
it must also support addQuad/removeQuad for UPDATE queries, or you can pass
a createTransaction factory for atomic writes.
Skip the rest of this guide unless you need a different engine.
Custom engine (this guide)
If you need Comunica’s federation, actor system, or query-planning features, implement the interface yourself. It is a single method.The contract
SparqlEngineInterface (defined in
@wazoo/sparql-engine) has one method:
SparqlRequestcarriesquery,baseIri?,timeoutMs?, andsignal?(anAbortSignalthe caller uses to cancel an in-flight request).SparqlResponseis one of four shapes:{ kind: "select", data },{ kind: "ask", data },{ kind: "construct", data }, or{ kind: "void" }.
SparqlEngineInterface, SparqlRequest, SparqlResponse,
SparqlValue, etc.) are defined in @wazoo/sparql-engine and re-exported
through @worlds/sdk/sparql-engine for SDK consumers. If you are implementing
an engine, import from @wazoo/sparql-engine directly.
Install
Comunica and its RDF/JS types are npm packages. The types and term converter come from the Wazoo SPARQL engine:Implement the interface
The adapter runs Comunica over the same RDF/JS store the quad facade uses, so imports and SPARQL queries see one consistent dataset. Comunica returns raw RDF/JS terms in async streams. TheSparqlEngineInterface
expects SPARQL JSON results format. The rdfTermToSparqlValue function from
@wazoo/sparql-engine handles the conversion (including RDF 1.2 directional
language-tagged literals).
comunica-sparql-engine.ts
execute/#run—executeis the interface entry point;#runholds the Comunica call plus result mapping so the timeout/abort wiring below can wrap it.rdfTermToSparqlValue(imported, not reimplemented) converts each RDF/JS binding term into theSparqlValuewire format the interface expects.#selectreads Comunica’s variable metadata to get the column names, then iterates the async bindings stream and buildsSparqlBinding[].#boolean,#quads, andvoidare one-liners that delegate to Comunica’sexecute().
Adding timeout and abort support
TheSparqlRequest carries timeoutMs and an AbortSignal. To honour them,
compose one AbortController that fires on whichever comes first, then race the
engine work against a promise that rejects when that controller aborts. The race
is what hands control back to your caller; without it, a fired timer or caller
abort would change nothing and queries would run to completion:
WazooSparqlEngine implements exactly this
compose-and-race shape out of the box.
Wire it into a client
Swap the engine in the same way you would configure any otherSparqlEngineInterface:
index.ts
timeoutMs and signal exactly as the
interface documents.
Notes
- Import from
@wazoo/sparql-engine, not@worlds/sdk. The SDK re-exports the types for convenience (@worlds/sdk/sparql-engine), but@wazoo/sparql-engineis where they are defined. Engine implementors should depend on@wazoo/sparql-enginedirectly — it gives you the interface, the types, andrdfTermToSparqlValuein one import. - Transactional updates. Comunica writes directly to the store. If your
backend needs atomic writes through its own transaction (like the built-in
engine’s
createTransaction), wrap the read store in aTransactionalRdfjsStorefrom@worlds/sdk/quad-store, hand that to the engine, and commit onvoidresponses. - When to stay on Wazoo. The built-in
WazooSparqlEngineis zero-dependency and covers SPARQL 1.1 & 1.2 for in-process RDF/JS stores. Reach for a custom engine when you need capabilities it does not provide — for example Comunica’s federation or actor customization. - No SDK changes needed. The adapter lives entirely in your project; the Worlds SDK only defines the interface.