From 4ae91661ba3e87c8a690aac2eee3e285852c5ecc Mon Sep 17 00:00:00 2001 From: Carl Blakeley Date: Thu, 27 May 2021 11:38:44 +0100 Subject: [PATCH 1/4] Modified SPARQL query POST requests to work with Virtuoso --- resource.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) 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' } }; From 119bf38066392031cf246f253125603349db38a2 Mon Sep 17 00:00:00 2001 From: Carl Blakeley Date: Thu, 27 May 2021 11:39:48 +0100 Subject: [PATCH 2/4] Added sample GraphQL queries targetting DBpedia / Virtuoso --- examples/dbpedia1.graphql | 37 +++++++++++++++++++++++++++++++++++++ examples/dbpedia2.graphql | 39 +++++++++++++++++++++++++++++++++++++++ examples/index.graphql | 2 ++ 3 files changed, 78 insertions(+) create mode 100644 examples/dbpedia1.graphql create mode 100644 examples/dbpedia2.graphql diff --git a/examples/dbpedia1.graphql b/examples/dbpedia1.graphql new file mode 100644 index 0000000..b3dfc0f --- /dev/null +++ b/examples/dbpedia1.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 Dbpedia1 { + iri: String! + bookTitle: String + authorUri: String + authorName: String +} + diff --git a/examples/dbpedia2.graphql b/examples/dbpedia2.graphql new file mode 100644 index 0000000..ab32ca8 --- /dev/null +++ b/examples/dbpedia2.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 Dbpedia2 { + iri: String! + bookTitle: String + authorUri: String + authorName: String +} + diff --git a/examples/index.graphql b/examples/index.graphql index 3489c67..c8bfdb3 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!]! + dbpedia1(iri: String): Dbpedia1 + dbpedia2(iris: [String!]): [Dbpedia2!]! } From a97a494d52c9fca6cff1cac18236953d83b7d2a8 Mon Sep 17 00:00:00 2001 From: Carl Blakeley Date: Thu, 27 May 2021 12:06:37 +0100 Subject: [PATCH 3/4] Added README_OPL detailing OpenLink additions --- README_OPL.md | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 README_OPL.md diff --git a/README_OPL.md b/README_OPL.md new file mode 100644 index 0000000..cd1e569 --- /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/dbpedia1.graphql` and `./examples/dbpedia2.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 { + dbpedia1(iri: "http://dbpedia.org/resource/Corona_(novel)") { + iri + bookTitle + authorUri + authorName + } +} +``` + +**Result** + +``` +{ + "data": { + "dbpedia1": { + "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 { + dbpedia2(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": { + "dbpedia2": [ + { + "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" + } + ] + } +} +``` From a06af32858510e5acf90580fa79f99dc62e24112 Mon Sep 17 00:00:00 2001 From: Carl Blakeley Date: Thu, 27 May 2021 16:39:40 +0100 Subject: [PATCH 4/4] Renamed DBpedia examples and related files --- README_OPL.md | 10 +++++----- examples/{dbpedia1.graphql => dbpediaBook.graphql} | 2 +- examples/{dbpedia2.graphql => dbpediaBooks.graphql} | 2 +- examples/index.graphql | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) rename examples/{dbpedia1.graphql => dbpediaBook.graphql} (97%) rename examples/{dbpedia2.graphql => dbpediaBooks.graphql} (97%) diff --git a/README_OPL.md b/README_OPL.md index cd1e569..71f40cc 100644 --- a/README_OPL.md +++ b/README_OPL.md @@ -25,7 +25,7 @@ If using nvm `$ RESOURCES_DIR=./examples npm run watch` -This loads the example resource definitions from `./examples`. The resource definitions targetting DBpedia are contained in `./examples/dbpedia1.graphql` and `./examples/dbpedia2.graphql` +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. @@ -35,7 +35,7 @@ Enter either of the following GraphQL queries into the query editor. ``` query { - dbpedia1(iri: "http://dbpedia.org/resource/Corona_(novel)") { + dbpediaBook(iri: "http://dbpedia.org/resource/Corona_(novel)") { iri bookTitle authorUri @@ -49,7 +49,7 @@ query { ``` { "data": { - "dbpedia1": { + "dbpediaBook": { "iri": "http://dbpedia.org/resource/Corona_(novel)", "bookTitle": "Corona (novel)", "authorUri": "http://dbpedia.org/resource/Greg_Bear", @@ -63,7 +63,7 @@ query { ``` query { - dbpedia2(iris: + dbpediaBooks(iris: [ "http://dbpedia.org/resource/AI_Superpowers" "http://dbpedia.org/resource/A_History_of_British_Birds" @@ -83,7 +83,7 @@ query { ``` { "data": { - "dbpedia2": [ + "dbpediaBooks": [ { "iri": "http://dbpedia.org/resource/AI_Superpowers", "bookTitle": "AI Superpowers", diff --git a/examples/dbpedia1.graphql b/examples/dbpediaBook.graphql similarity index 97% rename from examples/dbpedia1.graphql rename to examples/dbpediaBook.graphql index b3dfc0f..60509ba 100644 --- a/examples/dbpedia1.graphql +++ b/examples/dbpediaBook.graphql @@ -28,7 +28,7 @@ WHERE {{#if iri}}VALUES ?iri { {{join " " (as-iriref iri)}} }{{/if}} } """ -type Dbpedia1 { +type DbpediaBook { iri: String! bookTitle: String authorUri: String diff --git a/examples/dbpedia2.graphql b/examples/dbpediaBooks.graphql similarity index 97% rename from examples/dbpedia2.graphql rename to examples/dbpediaBooks.graphql index ab32ca8..16d9d6d 100644 --- a/examples/dbpedia2.graphql +++ b/examples/dbpediaBooks.graphql @@ -30,7 +30,7 @@ WHERE {{/if}} } """ -type Dbpedia2 { +type DbpediaBooks { iri: String! bookTitle: String authorUri: String diff --git a/examples/index.graphql b/examples/index.graphql index c8bfdb3..eb03ede 100644 --- a/examples/index.graphql +++ b/examples/index.graphql @@ -10,6 +10,6 @@ type Query { datasets(iri: [String!]!): [Dataset!]! pubmed(iri: String!): Pubmed pubmeds(iri: [String!], id: [String!]): [Pubmed!]! - dbpedia1(iri: String): Dbpedia1 - dbpedia2(iris: [String!]): [Dbpedia2!]! + dbpediaBook(iri: String): DbpediaBook + dbpediaBooks(iris: [String!]): [DbpediaBooks!]! }