Querying RDF Graph Data
JSON-LD is a good tool for manipulating and presenting data, but how do we get at it's potential as an API layer?
This is a growing projectTowards the end of the article/talk JSON-LD is good, actually I provide some demonstrations of the benefits of graph data structures — specifically in the ability of JSON-LD to create frames of data. This is a GraphQL-ish way to define the shape you want your data in – including what keys to include or exclude – and converting your graph to that shape. For any given graph of data, there are lots of different ways to represent that data as a tree shaped structure (which JSON is!). Frames let us choose the tree we want, and also prune off bits of it we don’t want.
Internally I’m using frames to create the data which renders things like these subject pages. These pages load all of the sites content (represented as JSON-LD), and frame it so that we only get the objects that have the relevant subject.
Where this becomes interesting is in exposing this functionality as an API. We can do this by creating an endpoint that, when queried:
- Loads all of the sites JSON-LD into memory
- Grabs the
queryparameter string - Parses that to JSON
- Enriches it with the sites context
- Treats that as a frame
- Processes the in-memory graph into that frame
- Returns the resulting JSON object.
While cool and useful, frames aren’t quite a real query language. We can get the data we want in the shape we want it, but a real query language gives us a lot more power to understand what’s going on in the data structure.
One of strangest things about the web development ecosystem is that GraphQL is not actually a Query Language for Graphs. This suprised me when I tried to use it as one. GraphQL is really just the same thing as a JSON-LD frame – a spec for defining how to transform not-tree data into tree data. In GraphQL’s case the not-tree data tends to be a large variety of either REST endpoints or rectangular data tables. Which, while not trees, are also not graphs. And like a JSON-LD frame, not actually a real query language.
BUT there is a Query Language for Graphs that predates GraphQL significantly:
✨ SPARQL ✨
Yes, it’s pronounced “sparkle”. Yes, it’s cooler than GraphQL in every way. All RDF triple stores support SPARQL as the way to interact with the data. In this way, SPARQL is directly equivilant to SQL, but you know, for graphs.