Skip to content

Commit

Permalink
Add example showing all relations (#1052)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhghomon authored Dec 12, 2024
1 parent 5875cd5 commit 9962b87
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/content/doc-surrealql/statements/relate.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,49 @@ SELECT
FROM person:tobie;
```

The `?` operator on its own can thus be used to see all of the relations that a record has.

```surql
CREATE person:hermann_hesse, person:abigail, city:calw, book:demian;
RELATE person:hermann_hesse->wrote->book:demian SET written_in = d'1919-01-01';
RELATE person:hermann_hesse->born_in->city:calw;
RELATE person:abigail->likes->person:hermann_hesse;
SELECT
-- all tables in which the record is at `in`
->(?).* AS what_hesse_did,
-- all tables in which the record is at `out`
<-(?).* AS what_others_did_to_hesse
FROM person:hermann_hesse;
```

```surql title="Output"
[
{
what_hesse_did: [
{
id: born_in:k3adylof24a2r5kio8l5,
in: person:hermann_hesse,
out: city:calw
},
{
id: wrote:ncbo9w0d8t3xd7lvl4dx,
in: person:hermann_hesse,
out: book:demian,
written_in: d'1919-01-01T00:00:00Z'
}
],
what_others_did_to_hesse: [
{
id: likes:6gubmldm14gzasoyypay,
in: person:abigail,
out: person:hermann_hesse
}
]
}
]
```

Parentheses can be used at each point of a graph query. The example below includes `person` records (authors) connected to `book` records by the `wrote` table. As both the `person` and `book` tables have fields that can be useful when filtering, they can be isolated with parentheses at this point of the graph query in order to filter using the `WHERE` clause.

```surql
Expand Down

0 comments on commit 9962b87

Please sign in to comment.