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 Solid data pods. The default Comunica SPARQL engine can directly be used to query over public Solid data pods as long as you are querying over public data. If you want to query over data pods that require authentication, you can use one of the approaches mentioned below.

    Query pods with a custom fetch function

    Libraries such as @inrupt/solid-client-authn-node and @inrupt/solid-client-authn-browser 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.

    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 the authenticated fetch function
      fetch: session.fetch,
    });
    

    Query pods with an existing Solid session

    Comunica SPARQL Solid 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:

    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.

    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-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.