Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions README_OPL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Grasp - Binding to Virtuoso and DBpedia

2021-May-27
Carl Blakeley

Branch `opl_develop` includes slight changes to how Grasp POSTs queries to a SPARQL endpoint, to allow it to work against Virtuoso. Two simple GraphQL query examples have been added to demonstrate Grasp querying [DBpedia](https://www.dbpedia.org/).

## Requirements

Node.js 14 or later

## To Install

```
git clone https://github.com/OpenLinkSoftware/grasp.git
cd grasp
git checkout opl_develop
npm install
```

## To Run

If using nvm
`$ nvm use system`

`$ RESOURCES_DIR=./examples npm run watch`

This loads the example resource definitions from `./examples`. The resource definitions targetting DBpedia are contained in `./examples/dbpediaBook.graphql` and `./examples/dbpediaBooks.graphql`

Visit <http://localhost:4000>. You will see GraphQL Playground.

Enter either of the following GraphQL queries into the query editor.

### Example 1: Select a single object

```
query {
dbpediaBook(iri: "http://dbpedia.org/resource/Corona_(novel)") {
iri
bookTitle
authorUri
authorName
}
}
```

**Result**

```
{
"data": {
"dbpediaBook": {
"iri": "http://dbpedia.org/resource/Corona_(novel)",
"bookTitle": "Corona (novel)",
"authorUri": "http://dbpedia.org/resource/Greg_Bear",
"authorName": "Greg Bear"
}
}
}
```

### Example 2: Select multiple objects

```
query {
dbpediaBooks(iris:
[
"http://dbpedia.org/resource/AI_Superpowers"
"http://dbpedia.org/resource/A_History_of_British_Birds"
"http://dbpedia.org/resource/A_Brief_History_of_Time"
]
) {
iri
bookTitle
authorUri
authorName
}
}
```

**Result**

```
{
"data": {
"dbpediaBooks": [
{
"iri": "http://dbpedia.org/resource/AI_Superpowers",
"bookTitle": "AI Superpowers",
"authorUri": "http://dbpedia.org/resource/Kai-Fu_Lee",
"authorName": "Kai-Fu Lee"
},
{
"iri": "http://dbpedia.org/resource/A_Brief_History_of_Time",
"bookTitle": "A Brief History of Time",
"authorUri": "http://dbpedia.org/resource/Stephen_Hawking",
"authorName": "Stephen Hawking"
},
{
"iri": "http://dbpedia.org/resource/A_History_of_British_Birds",
"bookTitle": "A History of British Birds",
"authorUri": "http://dbpedia.org/resource/Thomas_Bewick",
"authorName": "Thomas Bewick"
}
]
}
}
```
37 changes: 37 additions & 0 deletions examples/dbpediaBook.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""
--- endpoint ---
https://dbpedia.org/sparql

--- sparql ---
PREFIX : <https://github.com/dbcls/grasp/ns/>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX schema: <http://schema.org/>

CONSTRUCT {
?iri :iri ?iri .
?iri :bookTitle ?title .
?iri :authorUri ?author .
?iri :authorName ?authorName .
}
WHERE
{
?iri a schema:Book ;
rdfs:label ?title ;
dbo:author ?author .
FILTER (LANG(?title) = 'en')

?author foaf:name ?authorName .
FILTER (LANG(?authorName) = 'en')

{{#if iri}}VALUES ?iri { {{join " " (as-iriref iri)}} }{{/if}}
}
"""
type DbpediaBook {
iri: String!
bookTitle: String
authorUri: String
authorName: String
}

39 changes: 39 additions & 0 deletions examples/dbpediaBooks.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
--- endpoint ---
https://dbpedia.org/sparql

--- sparql ---
PREFIX : <https://github.com/dbcls/grasp/ns/>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX schema: <http://schema.org/>

CONSTRUCT {
?iri :iri ?iri .
?iri :bookTitle ?title .
?iri :authorUri ?author .
?iri :authorName ?authorName .
}
WHERE
{
?iri a schema:Book ;
rdfs:label ?title ;
dbo:author ?author .
FILTER (LANG(?title) = 'en')

?author foaf:name ?authorName .
FILTER (LANG(?authorName) = 'en')

{{#if iris}}
VALUES ?iri { {{join " " (as-iriref iris)}} }
{{/if}}
}
"""
type DbpediaBooks {
iri: String!
bookTitle: String
authorUri: String
authorName: String
}

2 changes: 2 additions & 0 deletions examples/index.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ type Query {
datasets(iri: [String!]!): [Dataset!]!
pubmed(iri: String!): Pubmed
pubmeds(iri: [String!], id: [String!]): [Pubmed!]!
dbpediaBook(iri: String): DbpediaBook
dbpediaBooks(iris: [String!]): [DbpediaBooks!]!
}
9 changes: 4 additions & 5 deletions resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,16 @@ export default class Resource {
throw new Error('query template and endpoint should be specified in order to query');
}
const sparqlQuery = this.queryTemplate(args);
const body = sparqlQuery.replace(/\n/g, " ");

console.log('--- SPARQL QUERY ---', sparqlQuery);

const sparqlParams = new URLSearchParams();
sparqlParams.append('query', sparqlQuery);

const opts = {
method: 'POST',
body: sparqlParams,
body,
headers: {
Accept: 'application/sparql-results+json'
Accept: 'application/sparql-results+json',
'Content-Type': 'application/sparql-query'
}
};

Expand Down