A collection of RDF libraries for JavaScript

View the Project on GitHub

« API / Examples

Examples demonstrating some common use cases for the graphy JavaScript API.
For examples showcasing the command-line interface, see CLI/Examples here.


Count number of statements in Turtle document

To get started, let’s write a simple Node.js script that counts the number of statements in a Turtle document.

// count-statements.js
const ttl_read = require('@graphy/');

let c_statements = 0;
    .on('data', () => {
        c_statements += 1;
    .on('eof', () => {
        console.log(`${c_statements} statements`);


$ echo "
    <a> <b> <c> .
    <a> <b> <c> .
" | node count.js


2 statements

Notice how the result from this experiment shows that we are counting the number of statements in the input. We are more likely interested in counting the number of distinct triples or quads, which we show in the next example.

Count number of distinct triples in Turtle document

Anytime we are dealing with triples in RDF, we are really talking about quads without the graph component. For example, when you load a Turtle document into a triplestore, you are either loading them into the default graph, or you are choosing some graph to load them into. In essence, a ‘triplestore’ is really just a set of quads (the graph being the fourth component to the triple).

Let’s count the number of distinct triples (or in other words, the number of distinct quads in the default graph) in a Turtle document. In order to get a distinct count, we must load the data into a structure that will remove duplicates, such as the FastDataset package.

// count-quads.js
// since we are using multiple packages from graphy, we can load them from the super module
const graphy = require('graphy');
const read =;
const dataset =;

// we are going to perform an asynchronous task
(async() => {
    // pipe data from stdin thru the Turtle reader
    let k_dataset = await process.stdin.pipe(read())
        // pipe the RDF data thru the DatasetTree package
        // use `.until(event, true)` to await an event and return the stream
        .until('finish', true);

    // at this point, we have awaited for the dataset to emit 'finish' and have loaded all quads
    console.log(`${k_dataset.size} triples`);

Testing it out on real data!

$ curl | node count.js


214 statements

You can also use the CLI to quickly count the number of distinct quads/triples with one line of bash code; see the CLI example here

Validate Turtle document(s)

In this simple example, we demonstrate how to validate Turtle sent to a mock Node.js web server using Express.

const app = require('express')();
const ttl_read = require('@graphy/');'/validate', (ds_req, ds_res) => {
        validate: true,
        .on('error', (e_read) => {
            ds_res.status(400).end(`Invalid Turtle document: ${e_read.message}`);
        .on('eof', () => {
            ds_res.status(200).end('Valid Turtle!');


Testing with invalid input:

$ curl -X POST --data "<This> <is> <bad input> ." http://localhost:3210/validate


Invalid Turtle document: invalid IRI: "bad input"

Testing with valid input:

$ echo "
    @prefix : <> .
    :This :is :good_input .
" | curl -X POST --data @- http://localhost:3210/validate


Valid Turtle!