Ghent University – imec – IDLab, Belgium
Input:
{ people { name height } }
Output:
{ "data": { "people": [ { "name": "Victor Gilbert", 1.8 }, { "name": "Scarlett Nixon", 1.65 }, { "name": "Axel Blackburn", 1.82 } ] } }
Query fields are converted to URIs
Hierarchies to chained triple patterns
Any SPARQL engine can be used. E.g. Comunica
Same shape as the input query
GraphQL query:
{ name(_:"The DBpedia Ontology") description }
JSON-LD context:
{ "name": { "@id": "dct:title", "@language": "en" }, "description": "dct:description" }
Resulting SPARQL query:
SELECT ?description WHERE { _:b1 dct:title "The DBpedia Ontology"@en. _:b1 dct:description ?description. }
Create chained triple patterns
Set objects in triple patterns
Define optional typed blocks
Apply offset and limit
Nodes can be nested to any depth, and just produce more triple patterns.
GraphQL:
{ hero { name friends { name } } }
SPARQL:
SELECT ?hero_name ?hero_friends_name WHERE { _:b1 ex:hero ?hero. ?hero ex:name ?hero_name. ?hero ex:friends ?hero_friends. ?hero_friends ex:name ?hero_friends_name. }
Node arguments are converted to triple objects in SPARQL.
GraphQL:
{ human(id: "1000") { name height } }
SPARQL:
SELECT ?hero_name ?hero_friends_name WHERE { _:b1 ex:hero ?hero. ?human ex:id "1000". ?human ex:name ?human_name. ?human ex:height ?human_height. }
Inline fragments can be used to scope an (optional) block to a certain type.
GraphQL:
{ hero { name ... on Droid { function } ... on Human { height } } }
SPARQL:
SELECT ?hero_name ?hero_function ?hero_height WHERE { _:b1 ex:hero ?hero. ?hero ex:name ?hero_name. OPTIONAL { ?hero rdf:type ex:Droid. ?hero ex:function ?hero_function. } OPTIONAL { ?hero rdf:type ex:Human. ?hero ex:height ?hero_height. } }
first
and offset
arguments set the limit and offset.
GraphQL:
{ hero { name friends(offset:10){ name } } }
SPARQL:
SELECT ?hero_name ?hero_friends_name WHERE { _:b1 ex:hero ?hero. ?hero ex:name ?hero_name. { SELECT * WHERE { ?hero ex:friends ?hero_friends. ?hero_friends ex:name ?hero_friends_name. } OFFSET 10 } }
Only select a single value, instead of multiple.
Input:
{ name @single }
Output singularized:
[ { "name": "Alice" }, { "name": "Bob" } ]
Output non-singularized:
[ { "name": [ "Alice", "Al" ] }, { "name": [ "Bob", "Bobby" ] } ]
Singularization can be scoped to a single field, to all fields, or can be reversed
GraphQL:
query @single(scope: all) { # Makes all fields singular by default hero @plural { # Makes only this field plural name } }
All features are listed on https://github.com/rubensworks/graphql-to-sparql.js
rdf:type
of something.OPTIONAL
.by applying a JSON-LD context
Queries can be structured via arguments, JSON-LD features, directives
Can be used in any JavaScript application (Node.js, browser)