Skip to content

GQL

Sample queries

The examples below run on one small social graph: four people who know each other, and the cities two of them live in. Each section shows a GQL query and a SurrealQL query that returns the same or equivalent result shape. The SurrealQL is not a mechanical translation of the GQL compiler; it is an idiomatic read you can compare while learning.

Start a local instance with GQL enabled - see GQL via HTTP.

You can run the GQL examples in either of these ways:

ApproachHow
HTTPcURL to POST /gql (shown in each GQL tab) - GQL is on by default from 3.3.0; on 3.2.x use --allow-experimental gql
REPLeval::gql("…") in surreal db sql or SurrealDB Studio - also needs --allow-eval-query on the process that runs the engine (embedded surreal db sql, or surreal db start for remote)

Each person record is a node with the label person, and each city record a node with the label city. The knows edges join two people, with the year they met in since, and the lives_in edges join a person to a city. Dev's edge to Ana has no since, and Ben and Dev have no city.

Load the graph with ordinary SurrealQL, either through POST /sql in namespace main and database main, or in SurrealDB Studio:

INSERT INTO person [
	{ id: person:ana, name: 'Ana' },
	{ id: person:ben, name: 'Ben' },
	{ id: person:chloe, name: 'Chloe' },
	{ id: person:dev, name: 'Dev' }
] RETURN NONE;
INSERT INTO city [
	{ id: city:london, name: 'London' },
	{ id: city:paris, name: 'Paris' }
] RETURN NONE;
INSERT RELATION INTO knows [
	{ in: person:ana, out: person:ben, since: 2022 },
	{ in: person:ben, out: person:ana, since: 2019 },
	{ in: person:ben, out: person:chloe, since: 2021 },
	{ in: person:chloe, out: person:dev, since: 2017 },
	{ in: person:dev, out: person:ana }
] RETURN NONE;
INSERT RELATION INTO lives_in [
	{ in: person:ana, out: city:london },
	{ in: person:chloe, out: city:paris }
] RETURN NONE;

The SurrealQL tabs below repeat the part of the graph that each query reads, so each of them also runs on its own. With the graph loaded, the REPL form of the first query is:

eval::gql("MATCH (n:person) RETURN n.name AS name ORDER BY name");
//- [{ name: 'Ana' }, { name: 'Ben' }, { name: 'Chloe' }, { name: 'Dev' }]
Query
MATCH (n:person) RETURN n.name AS name ORDER BY name
cURL
curl -sS -X POST -u "root:secret" \
  -H "Surreal-NS: main" -H "Surreal-DB: main" \
  -H "Accept: application/json" -H "Content-Type: text/plain" \
  -d 'MATCH (n:person) RETURN n.name AS name ORDER BY name' \
  http://localhost:8000/gql
Output
[
	{ "name": "Ana" },
	{ "name": "Ben" },
	{ "name": "Chloe" },
	{ "name": "Dev" }
]

The query follows the knows edges between two people, and keeps only those with since after 2020. Dev's edge to Ana has no since, so the comparison leaves it out.

Query
MATCH (a:person)-[k:knows]->(b:person)
WHERE k.since > 2020
RETURN a.name, b.name
ORDER BY a.name
cURL
curl -sS -X POST -u "root:secret" \
  -H "Surreal-NS: main" -H "Surreal-DB: main" \
  -H "Accept: application/json" -H "Content-Type: text/plain" \
  -d 'MATCH (a:person)-[k:knows]->(b:person) WHERE k.since > 2020 RETURN a.name, b.name ORDER BY a.name' \
  http://localhost:8000/gql
Output
[
	{ "a.name": "Ana", "b.name": "Ben" },
	{ "a.name": "Ben", "b.name": "Chloe" }
]

The query returns every person, with the city they live in when a lives_in edge exists.

Query
MATCH (a:person)
OPTIONAL MATCH (a)-[:lives_in]->(c:city)
RETURN a.name AS name, c.name AS city
ORDER BY name
cURL
curl -sS -X POST -u "root:secret" \
  -H "Surreal-NS: main" -H "Surreal-DB: main" \
  -H "Accept: application/json" -H "Content-Type: text/plain" \
  -d 'MATCH (a:person) OPTIONAL MATCH (a)-[:lives_in]->(c:city) RETURN a.name AS name, c.name AS city ORDER BY name' \
  http://localhost:8000/gql
Output
[
	{ "name": "Ana", "city": "London" },
	{ "name": "Ben", "city": null },
	{ "name": "Chloe", "city": "Paris" },
	{ "name": "Dev", "city": null }
]

The query counts the knows edges that leave each person.

Query
MATCH (a:person)-[:knows]->(b:person)
RETURN a.name AS name, count(*) AS c
GROUP BY a.name
ORDER BY name
cURL
curl -sS -X POST -u "root:secret" \
  -H "Surreal-NS: main" -H "Surreal-DB: main" \
  -H "Accept: application/json" -H "Content-Type: text/plain" \
  -d 'MATCH (a:person)-[:knows]->(b:person) RETURN a.name AS name, count(*) AS c GROUP BY a.name ORDER BY name' \
  http://localhost:8000/gql
Output
[
	{ "name": "Ana", "c": 1 },
	{ "name": "Ben", "c": 2 },
	{ "name": "Chloe", "c": 1 },
	{ "name": "Dev", "c": 1 }
]

The language-test corpus under language-tests/tests/opengql/ in the SurrealDB repository covers variable-length quantifiers (->{1,3}, ->*), path search (ALL SHORTEST, SHORTEST k), multi-pattern comma joins, and mutations. Variable-length and path patterns have no single-line SurrealQL equivalent - they are the main reason to use GQL for reads; mutations are the ISO-native way to change graph data in the same program as MATCH.

Was this page helpful?

Last updated