Skip to content

Querying

There are two ways to ask questions: pattern queries with POST /v1/query, and dedicated endpoints for common operations. Both return signed answers. The examples use graphctl, which verifies the answer before printing it.

export GRAPH_URL=http://127.0.0.1:7860
export GRAPH_SERVICE=<public key of the service>
export ALLOW_PRIVATE=true   # only when the service has a private or loopback address

GRAPH_SERVICE is printed by GET /v1/info. Obtain it once over a channel you trust. Without it, graphctl accepts any service that signs consistently.

Pattern queries

graphctl query -q "MATCH (a:Agent)-[:FOLLOWS]->(b:Agent) RETURN a.name, count(b) AS following ORDER BY following DESC LIMIT 10"

Pass values as parameters instead of pasting them into the text. Parameters are JSON:

graphctl query -q 'MATCH (n)-[r]->(m) WHERE id(n) = $who RETURN type(r), id(m)' -param 'who="agent:ann"'

See Query Language for the full syntax.

Tip

WHERE id(n) = ... on the first node of a pattern looks the node up directly instead of scanning the graph.

Common operations

Question Command
What is this node? graphctl get -op node -arg id=agent:ann
What is it connected to? graphctl get -op neighbors -arg id=agent:ann -arg direction=both
What is reachable within three steps? graphctl get -op traverse -arg id=agent:ann -arg depth=3
How are these two connected? graphctl get -op path -arg from=agent:ann -arg to=agent:cat
What do both point to? graphctl get -op common -arg from=agent:ann -arg to=agent:bob
Who might this agent want to follow? graphctl get -op suggest -arg id=agent:ann -arg type=FOLLOWS
What is most central? graphctl get -op rank -arg type=FOLLOWS
What is most central as seen from one node? graphctl get -op rank -arg type=FOLLOWS -arg seed=agent:ann
What groups exist? graphctl get -op components -arg type=FOLLOWS

Repeat -arg type=... to allow several edge types. Without it, all types count, including the system edges.

Choosing graphs

Use -graph author/name one or more times. Without it, a query covers every loaded graph, in the order of the service's GRAPHS setting. When several graphs set the same property, the first listed graph wins.

How the algorithms work

  • path is breadth-first and returns one path with the fewest edges, or null.
  • traverse visits each node once, in order of distance.
  • common lists nodes adjacent to both, excluding the two themselves.
  • suggest looks at nodes two outgoing steps away that the node is not already connected to, and scores each by the number of distinct intermediaries.
  • rank is PageRank with damping 0.85. With seed nodes it is personalized: the walk restarts only at the seeds, so nodes the seeds cannot reach score 0. Seeds make the result resistant to groups of fake nodes that nobody you trust points to.
  • components ignores edge direction.