Skip to content

Your first graph

This example creates Alice → Bob → Cara, then finds Alice’s friend of a friend. Run it from a source checkout after building the CLI.

From the 0.2.0 source checkout:

Terminal window
cargo run -p graphfusion --locked -- ./demo-db

At the prompt, load the bundled example once with \read examples/social.gql. Then enter MATCH (p:Person) RETURN p.name AS name, p.age AS age;. End each GQL input with a semicolon; use \help for commands and \quit to exit. Reopen with the same command and enter SESSION SET GRAPH social; to select the existing graph. Do not reload the create script into a database that already contains it.

The steps below demonstrate the separate one-shot run command. Use a different database directory if you already created demo-db in the REPL, and close the REPL before opening its database from another process.

Save the following as first-graph.gql:

CREATE GRAPH social ANY GRAPH;
SESSION SET GRAPH social;
INSERT (alice:Person {name: 'Alice', age: 30})-[:Knows]->
(bob:Person {name: 'Bob', age: 40})-[:Knows]->
(cara:Person {name: 'Cara', age: 25});
MATCH (a:Person {name: 'Alice'})-[:Knows]->(b)-[:Knows]->(c)
RETURN c.name AS friend;
Terminal window
cargo run -p graphfusion --locked -- run --file first-graph.gql

The final table contains friend = Cara. The program runs in memory and its database disappears when the process exits. ANY GRAPH creates an open graph: you can introduce labels and properties without declaring a graph type first.

Use a new database directory:

Terminal window
cargo run -p graphfusion --locked -- run \
--database ./demo-db --create --file first-graph.gql
cargo run -p graphfusion --locked -- run --database ./demo-db \
--query "USE GRAPH social MATCH (p:Person) RETURN p.name AS name ORDER BY name"

The second process reopens the graph and returns Alice, Bob and Cara. Session settings do not persist between processes, so the query selects social again with USE GRAPH.

Do not rerun the create program against the same directory unchanged: CREATE GRAPH social will find an existing graph. Reopen the directory to query it, or use a different directory to repeat the tutorial.

MATCH (p:Person {name: 'Alice'})
SET p.age = p.age + 1
RETURN p.name AS name, p.age AS age;

This returns Alice and 31. INSERT, SET and DELETE explain how statements publish changes.