GraphQL Tutorial
GraphQL and RDF

Ruben Taelman

Olaf Hartig

ISWC 2019, Auckland, 26/27 October 2019

GraphQL and RDF

GraphQL Tutorial

1Ghent University – imec – IDLab, Belgium

2Department of Computer and Information Science (IDA) — Linköping University, Sweden

GraphQL queries and schemas
have no global semantics or identifiers

Querying over multiple GraphQL interfaces is hard with the current solutions:

RDF offers solutions for this problem

Support for federation is built into its foundations.

GraphQL can simplify access to RDF

GraphQL is highly popular and widely used.

→ Querying RDF with GraphQL would be highly beneficial.

Bridges between GraphQL and RDF exist

HyperGraphQL exposes RDF sources
as a GraphQL interface

HyperGraphQL Logo

Configure a HyperGraphQL instance

config.json: interface parameters and available services (SPARQL, RDF files,...)

{
    "name": "dbpedia-sparql-hgql-demo",
    "schema": "schema.graphql",
    "server": {
        "port": 8080,
        "graphql": "/graphql",
        "graphiql": "/graphiql"
    },
    "services": [{
        "id": "dbpedia-sparql",
        "type": "SPARQLEndpointService",
        "url": "http://dbpedia.org/sparql/",
        "graph": "http://dbpedia.org"
    }]
}

Create an annotated GraphQL schema

__Context maps fields to IRIs
@service defines source for a type/field

type __Context {
  Person: _@href(iri: "http://dbpedia.org/ontology/Person")
  City:   _@href(iri: "http://dbpedia.org/ontology/City")
  name:   _@href(iri: "http://xmlns.com/foaf/0.1/name")
  label:  _@href(iri: "http://www.w3.org/2000/01/rdf-schema#label")
}

type Person @service(id:"dbpedia-sparql") {
  name: String @service(id:"dbpedia-sparql")
  label: [String] @service(id:"dbpedia-sparql")
  birthPlace: City @service(id:"dbpedia-sparql")
  birthDate: String @service(id:"dbpedia-sparql")
}

HyperGraphQL returns JSON-LD

Both valid GraphQL response and RDF

{
  "data": {
    "Person_GET": [
      {
        "_id": "http://dbpedia.org/resource/Sani_ol_molk",
        "_type": "http://dbpedia.org/ontology/Person",
        "name": "Mirza Abolhassan Khan Ghaffari"
      }
    ],
    "@context": {
      "_id": "@id",
      "_type": "@type",
      "name": "http://xmlns.com/foaf/0.1/name"
    }
  }
}

TopBraid enables GraphQL interaction

TopBraid Logo

GraphQL querying of RDF with TopBraid

Enhanced GraphQL schema in TopBraid

schema {
  query: RootRDFQuery
}

type RootRDFQuery {
  humans (...): [Human]
}

type Human {
  uri: ID!
  label: String!
  id (...): ID!
  name (...): String!
  friends (...): [Human]
}

...: auto-generated filters, aggregators, ...

GraphQL query in TopBraid

Enhanced GraphQL functionality: filtering, ordering, transformation

{
  humans (where: {name:{pattern:"^L"}}, orderBy: name) {
    id
    name
    height (transform: "$height / 0.3048")
    friends {
      id
      name
    }
  }
}

Stardog databases allow querying
using GraphQL queries

Stardog Logo

Conversion with default namespaces

GraphQL:

{
  Human {
    name
    height
  }
}

SPARQL:

SELECT ?name ?height WHERE {
  _:b1 a :Human;
       :name ?name;
       :height ?height.
}

Custom prefixes or aliases can be defined

Prefixes:

query withPrefixes @prefix(foaf: "http://xmlns.com/foaf/0.1/") {
  Human {
    foaf:name
  }
}

Aliases:

query withAliases @config(alias: {myType: "http://ex.com/my-type",
                                  myProp: "http://ex.com/my-prop"}) {
  myType {
    myProp
  }
}

GraphQL-LD converts GraphQL to SPARQL

GraphQL-LD Logo

JSON-LD contexts are exploited

Conversion expands fields to IRIs

GraphQL:

{
  label
  artist { label }
}

JSON-LD context:

{
  "label": "http://www.w3.org/2000/01/rdf-schema#label",
  "artist": "http://dbpedia.org/ontology/musicalArtist"
}

SPARQL:

SELECT ?label ?writer ?artist_label
WHERE {
  _:b1 rdfs:label ?label;
       dbo:writer ?writer.
  _:b1 dbo:musicalArtist [
    rdfs:label ?artist_label
  ].
}

Querying RDF with GraphQL: Overview

Name Open-source Engine-independent Lifting method
HyperGraphQL GraphQL schema
TopBraid Shapes / GraphQL schema
Stardog Namespaces / GraphQL schema
GraphQL-LD JSON-LD context

In Summary

Sources