Notes

Part 8 of 10

Datalog

Datalog comes from 1980s research, well before SPARQL and Cypher. Few engineers know it and few mainstream databases use it, but it is very expressive and good at complex queries. It is relational, not graph-based, but recursive graph queries are a strength, so it belongs here. It is a subset of Prolog.

  • Data is facts, and each fact is a row: place(4, "Pune", "city") says the place table has that row.
  • Edges become two-column tables, like part_of, studied_in, and lives_in, for example lives_in(Asha's ID, Pune's ID).

Queries are rules that define new virtual tables, like SQL views: not stored, but queryable like tables.

  • Before :- is the virtual table and its columns.
  • After :- are patterns matched against tables, binding variables to values.
  • When every pattern matches, it is as if the left-hand row, filled with those values, joined the database.

Four rules answer the example:

  1. Every place is inside itself (by name).
  2. If A is part of B, and B is inside P, then A is inside P. This rule uses itself, so it is recursive.
  3. A mover studied somewhere inside one area and lives somewhere inside another; this joins person, studied_in, inside, lives_in, and inside.
  4. Keep movers who studied inside Asia and live inside Africa.

Running the rules: rule 1 gives inside(Asia's ID, "Asia"); India is part of Asia, so rule 2 adds India; Maharashtra is part of India, so rule 2 adds it too. Repeating rules 1 and 2 finds every place inside every bigger place.

Then rule 3 finds each person's study and home places, and rule 4 filters, matching the Cypher and SPARQL answer.

Datalog builds a big query from small rules that use each other, like functions calling functions. A rule can call itself, which makes graph walks possible. It moves in small steps instead of one huge SELECT.

GraphQL

GraphQL is far more restricted than the others, on purpose, and is meant for OLTP. A client, like a phone app or JavaScript front end, asks for a JSON document shaped exactly as its screen needs. Client teams can change queries quickly without new server APIs.

Costs:

  • Tooling must turn GraphQL queries into calls to internal services, often REST or gRPC (chapter 5).
  • Authorization, rate limiting, and performance need extra work.

Queries come from untrusted clients, so anything that could be costly is banned, to avoid accidental or deliberate denial of service. There are no recursive queries, unlike Cypher, SPARQL, SQL, and Datalog, and no free-form search unless the service offers it.

Example: a news app asks for followed topics, each with its 20 newest stories, each story's headline, time, and author name and photo, plus the headline and author of any earlier story it answers.

The JSON reply has the same shape as the query, with exactly the requested fields. The server need not know what the screen shows; asking for the earlier author's photo too is a client-only change.

Replies repeat data on purpose: an author's name and photo appear on every one of their stories, and an earlier story's details repeat under each answer to it. Sending only IDs would mean extra requests, for instance when the earlier story is not among the 20 returned. GraphQL trades bigger replies for simpler screen code.

Behind it, the database can stay normalized: a story stores its author ID and the ID of the story it answers, and the server resolves them at query time. Clients can only follow links the GraphQL schema declares. Despite its look and its name, GraphQL can front a relational, document, or graph store alike.