@tpluscode/sparql-builder

Simple JS library to build SPARQL queries

Usage no npm install needed!

<script type="module">
  import tpluscodeSparqlBuilder from 'https://cdn.skypack.dev/@tpluscode/sparql-builder';
</script>

README

@tpluscode/sparql-builder Test codecov npm version

Simple library to create SPARQL queries with tagged ES Template Strings

How is it different from simply string concatenation/plain templates?

  1. Focuses on graph patterns. No need to remember exact syntax
  2. Still looks like SPARQL, having a familiar structure and little glue code
  3. Has the IDE provide syntactic hints of valid SPARQL keywords
  4. Ensures correct formatting of terms (URI nodes, literals variables) via @tpluscode/rdf-string
  5. Automatically shortens URIs with @zazuko/rdf-vocabularies

Installation

npm i -S @tpluscode/sparql-builder

To be able to execute queries against a remote endpoint install a peer dependency:

npm i -S sparql-http-client

Usage

Build a SPARQL query string

import { variable } from '@rdfjs/data-model'
import namespace from '@rdfjs/namespace'
import { prefixes } from '@zazuko/rdf-vocabularies'
import { SELECT } from '@tpluscode/sparql-builder'

const ex = namespace('http://example.com/')
const foaf = namespace(prefixes.foaf)

/*
    PREFIX foaf: <http://xmlns.com/foaf/0.1/>

    SELECT ?person ?name
    FROM <http://example.com/People>
    WHERE 
    {
      ?person a foaf:Person ;
              foaf:name ?name .
    }
*/
const person = variable('person')
const query = 
  SELECT`${person} ?name`
  .FROM(ex.People)
  .WHERE`
    ${person} a ${foaf.Person} ; 
              ${foaf.name} ?name .
  `.build()

Executing a query

Using sparql-http-client

import namespace from '@rdfjs/namespace'
import { prefixes } from '@zazuko/rdf-vocabularies'
import SparqlHttp from 'sparql-http-client'
import { ASK } from '@tpluscode/sparql-builder'
import fetch from 'isomorphic-fetch'

const dbo = namespace(prefixes.dbo)
const dbr = namespace('http://dbpedia.org/resource/')

const client = new SparqlHttp({
  endpointUrl: 'http://dbpedia.org/sparql',
  fetch,
})

const scoobyDoo = dbr('Scooby-Doo')

/*
    PREFIX dbo: <http://dbpedia.org/ontology/>

    ASK {
        <http://dbpedia.org/resource/Scooby-Doo> a dbo:Person .
    }
 */
ASK`${scoobyDoo} a ${dbo.Person}`
  .execute(client)
  .then(isScoobyAPerson => {
    // Fun fact: DBpedia seems to claim that Scooby-Doo is indeed a Person...
    return isScoobyAPerson
  })

Running examples

The example in docs/examples can be executed locally. To do so, first replace the package import to point to the repository root.

-const { /* */ } = require('@tpluscode/sparql-builder')
+const { /* */ } = require('../..')

Then simply npm run example.