Finding a human by id with GraphQL
GraphQL Query:
{
human(id: "1000") {
name
height
}
}
JSON results:
{
"data": {
"human": {
"name": "Luke Skywalker",
"height": 1.72
}
}
}
Finding a human by URI with SPARQL
GraphQL Query:
@prefix schema: <https://schema.org/>.
SELECT ?name ?height WHERE {
<http//example.org/human/1000> a schema:Person;
schema:name ?name;
schema:height ?height.
}
JSON results:
{
"head": {
"vars": [ "name" , "height" ]
},
"results": {
"bindings": [
{
"name": { "type": "literal" , "value": "Luke Skywalker" },
"height": { "type": "literal" , "value": "1.72", "datatype": "http://www.w3.org/2001/XMLSchema#double" },
}
]
}
}
GraphQL vs SPARQL
|
GraphQL |
SPARQL |
Low developer effort |
✓ |
|
Wide availability of professional development tools |
✓ |
|
High expressivity |
|
✓ |
Reusability of queries across datasets |
|
✓ |
Using GraphQL to query RDF:
different approaches exist
-
GraphQL-LD: Conversion to SPARQL based on a JSON-LD context,
independent of any engine and works for any RDF source.
-
Stardog: SPARQL engine that also accepts GraphQL (converted to SPARQL). Conversion is done using a GraphQL schema, or
@prefix
directives.
-
HyperGraphQL: GraphQL interface wrapper around RDF sources using a configuration file and an annotated GraphQL schema.
-
TopBraid: Graph store with GraphQL interface. GraphQL schemas can be generated using shape definitions (SHACL).
These approaches are incompatible
GraphQL-LD:
{
human {
name
height
}
}
Stardog:
{
Human {
name
height
}
}
SELECT ?name ?height WHERE {
_:b1 ex:human _:human.
_:human ex:name ?name;
ex:height ?height.
}
SELECT ?name ?height WHERE {
_:b1 a ex:Human;
ex:name ?name;
ex:height ?height.
}
More differences
- Aggregates (sum, count, ...)
- Filtering (directives, custom fields, ...)
- Optional fields
- Named graphs
- Ordering
- Pagination
- ...
→ Annoying for developers if they want to switch between different approaches
A standard way of querying
RDF with GraphQL is needed
Proposal for a plan of action:
-
Analyze the different aspects of querying RDF with GraphQL.
(Property paths, joins, ordering, aggregation, ...)
-
Categorize the existing approaches over these aspects.
-
Compare the advantages and disadvantages of each aspect approach.
-
Propose a standard approach and semantics for querying RDF with GraphQL.