Solid
On this page
What is Solid
Solid is a Web-based decentralization ecosystem where people are in control over their own data.
Solid achieves this by giving everyone control over their own personal data pod. Applications are completely separate, and have to ask permission to access your data.
Since Solid and Comunica have a compatible technology stack, Comunica can be used to query over one or more Solid data pods.
This document describes the different approaches that exist for querying Solid pods using Comunica.
In summary, there are three main approaches for querying Solid pods with Comunica:
- Query specific public documents: Comunica SPARQL (
@comunica/query-sparql) - Query specific private documents: Comunica SPARQL (
@comunica/query-sparql) and pass authenticated fetch function, or Comunica SPARQL Solid (@comunica/query-sparql-solid) - Query pods using link traversal (experimental): Comunica SPARQL Link Traversal Solid (
@comunica/query-sparql-link-traversal-solid)
1. Query specific public documents in Solid pods
If you have prior knowledge of what documents in a Solid pod contain the data you want to query over, and these documents do not require authentication, then you can directly query over these documents using the default Comunica SPARQL engine as a traditional federated query.
Querying from JavaScript:
import { QueryEngine } from '@comunica/query-sparql'; const myEngine = new QueryEngine(); const bindingsStream = await myEngine.queryBindings(` SELECT * { ?s ?p ?o }`, { sources: ['https://pod1.org/document1.ttl', 'https://pod2.org/document2.ttl'], });
Querying from the command line:
$ comunica-sparql https://pod1.org/document1.ttl https://pod2.org/document2.ttl \ "SELECT * { ?s ?p ?o }"
2. Query specific private documents in Solid pods
Similar to the previous approach, you can also query specific documents in Solid pods that require authentication.
For this, you can use Comunica in combination with libraries such as @inrupt/solid-client-authn-node
and @inrupt/solid-client-authn-browser that allow you to authenticate with your Solid WebID.
These libraries provide a custom fetch function, using which you can execute authenticated HTTP requests.
You can forward this fetch function to Comunica SPARQL to make it perform authenticated queries to pods as shown below.
Querying from JavaScript with Comunica SPARQL:
import { QueryEngine } from '@comunica/query-sparql'; import { Session } from '@inrupt/solid-client-authn-node'; const session = new Session(); const myEngine = new QueryEngine(); await session.login({ ... }); // Log in as explained in https://docs.inrupt.com/developer-tools/javascript/client-libraries/tutorial/authenticate-nodejs-web-server/ const bindingsStream = await myEngine.queryBindings(` SELECT * WHERE { ?s ?p ?o } LIMIT 100`, { // Set your profile as query source sources: [session.info.webId], // Pass the authenticated fetch function fetch: session.fetch, });
Alternatively, you can use the dedicated Comunica SPARQL Solid, which allows you to pass your authenticated Solid session object. Hereafter, we list some examples on how to use it from JavaScript and the command line. Please refer to the README of Comunica SPARQL Solid for more details.
Querying from JavaScript Comunica SPARQL Solid:
import { QueryEngine } from '@comunica/query-sparql-solid'; import { Session } from '@inrupt/solid-client-authn-node'; const session = new Session(); const myEngine = new QueryEngine(); await session.login({ ... }); // Log in as explained in https://docs.inrupt.com/developer-tools/javascript/client-libraries/tutorial/authenticate-nodejs-web-server/ const bindingsStream = await myEngine.queryBindings(` SELECT * WHERE { ?s ?p ?o } LIMIT 100`, { // Set your profile as query source sources: [session.info.webId], // Pass your authenticated session '@comunica/actor-http-inrupt-solid-client-authn:session': session, });
Querying an existing document:
$ comunica-sparql-solid --idp https://solidcommunity.net/ \ http://example.org/existing-document.ttl \ "SELECT * { ?s ?p ?o }"
Creating a new document:
$ comunica-sparql-solid --idp https://solidcommunity.net/ \ http://example.org/new-document.ttl \ "INSERT DATA { <ex:s> <ex:p> <ex:o> }"
Updating an existing document:
$ comunica-sparql-solid --idp https://solidcommunity.net/ \ http://example.org/existing-document.ttl \ "INSERT DATA { <ex:s> <ex:p> <ex:o> }"
Please be aware that that there are several open known issues relating to other software.
LDflex and GraphQL-LD are examples of tools that ship with Comunica SPARQL Solid.
3. Query pods using link traversal
The approaches for querying Solid mentioned above require you to know upfront in which pod and in which documents your data resides before you can query over it. Comunica SPARQL Link Traversal Solid provides a way to query over Solid pods without having to know beforehand in which documents the necessary data resides in. It does this by following links between documents during query execution.
This is still an experimental query approach, which does not yet work well for complex queries. Learn more about active research on link traversal in Solid.
The example below executes a query across multiple simulated Solid pods to find all messages by a certain creator:
import { QueryEngine } from '@comunica/query-sparql-link-traversal-solid'; const myEngine = new QueryEngine(); const bindingsStream = await myEngine.queryBindings(` PREFIX snvoc: <https://solidbench.linkeddatafragments.org/www.ldbc.eu/ldbc_socialnet/1.0/vocabulary/> SELECT DISTINCT ?forumId ?forumTitle WHERE { ?message snvoc:hasCreator <https://solidbench.linkeddatafragments.org/pods/00000006597069767117/profile/card#me>. ?forum snvoc:containerOf ?message; snvoc:id ?forumId; snvoc:title ?forumTitle. }`, { // Sources field is optional. Will be derived from query if not provided. //sources: [session.info.webId], // Sets your profile as query source // Session is optional for authenticated requests //'@comunica/actor-http-inrupt-solid-client-authn:session': session, // The lenient flag will make the engine not crash on invalid documents lenient: true, });
Try out this query above in our live demo.