SOCOM Tutorial Slides - GraphQL-LD

Ruben Taelman, Julian Rojas

ISWC 2019, Auckland, 26 October 2019

SOCOM Tutorial Slides - GraphQL-LD

Ghent University – imec – IDLab, Belgium

Tutorial Website: http://bit.ly/iswcquery

GraphQL

GraphQL is a highly popular
data query language

Input:

{
  people {
    name
    height
  }
}

Output:

{
  "data": {
    "people": [
      { "name": "Victor Gilbert", 1.8 },
      { "name": "Scarlett Nixon", 1.65 },
      { "name": "Axel Blackburn", 1.82 }
    ]
  }
}
GraphQL

GraphQL pros and cons

Pros:
Cons:

GraphQL-LD enhances GraphQL with RDF

GraphQL-LD

Simple GraphQL-LD example

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.
}

Try live

Functionality: Overview

Fields and nesting

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.
}

Arguments

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

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.
  }
}

Pagination

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
  }
}

Singularization

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 scoping

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
  }
}

More advanced features

All features are listed on https://github.com/rubensworks/graphql-to-sparql.js

Getting started with GraphQL-LD

Conclusions

Some examples


https://query.linkeddatafragments.org/