diff --git a/README_OPL.md b/README_OPL.md new file mode 100644 index 0000000..71f40cc --- /dev/null +++ b/README_OPL.md @@ -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 . 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" + } + ] + } +} +``` diff --git a/examples/dbpediaBook.graphql b/examples/dbpediaBook.graphql new file mode 100644 index 0000000..60509ba --- /dev/null +++ b/examples/dbpediaBook.graphql @@ -0,0 +1,37 @@ +""" +--- endpoint --- +https://dbpedia.org/sparql + +--- sparql --- +PREFIX : +PREFIX dbo: +PREFIX foaf: +PREFIX rdfs: +PREFIX schema: + +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 +} + diff --git a/examples/dbpediaBooks.graphql b/examples/dbpediaBooks.graphql new file mode 100644 index 0000000..16d9d6d --- /dev/null +++ b/examples/dbpediaBooks.graphql @@ -0,0 +1,39 @@ +""" +--- endpoint --- +https://dbpedia.org/sparql + +--- sparql --- +PREFIX : +PREFIX dbo: +PREFIX foaf: +PREFIX rdfs: +PREFIX schema: + +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 +} + diff --git a/examples/index.graphql b/examples/index.graphql index 3489c67..eb03ede 100644 --- a/examples/index.graphql +++ b/examples/index.graphql @@ -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!]! } diff --git a/resource.ts b/resource.ts index 83e354a..9c2583d 100644 --- a/resource.ts +++ b/resource.ts @@ -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' } };