Skip to main content
Module

x/ldkit/docs/how-to/query-with-comunica.md

LDkit - Linked Data query toolkit for TypeScript developers
Go to Latest
File

Query with Comunica

LDkit is fully compatible with Comunica-based query engine.

Comunica lets you access RDF data from multiple sources and various source types, including Solid pods, RDF files, Triple/Quad Pattern Fragments, HDT files.

Note: If you need to access data through a SPARQL endpoint, it is recommended to use the default Query Engine shipped with LDkit. The built-in engine is lightweight and optimized for being used in browser.

To use Comunica with LDkit, simply pass its instance to a Lens, or set it as the default engine. The example below shows a setup how to query in-memory data using N3 store. This particular example uses the @comunica/query-sparql-rdfjs engine.

import { type Context, createLens } from "ldkit";
import { QueryEngine as Comunica } from "@comunica/query-sparql-rdfjs";
import { Store } from "n3";

const store = new Store();

const context: Context = {
  sources: [store],
};

const engine = new Comunica();

const resource = createLens(MySchema, context, engine);

The context format for LDkit is the same as for Comunica. The context that you pass to a resource, or a default context (if you set it up) eventually gets passed to Comunica engine instance.

Using Comunica as a server

If you want to query over an RDF source different from a SPARQL endpoint from a user facing application, but don’t want to compromise performance, you can use Comunica as a proxy SPARQL endpoint, and use the LDkit default query engine to query this endpoint.

The Comunica browser distribution is a 1.2 MB file, which you may not want to include in a client-facing application. In comparison, the LDkit default query engine is a simple class of 150 lines of code.