# edge

Source: https://zotonic.com/docs/1297/edge

URI: https://zotonic.com/id/1297

Language: en



Access information about page connections.

Edges represent the connections between resources. They are implemented as tuples `{EdgeId, SubjectId, PredicateId, ObjectId, OrderNr}`. The edge id is a unique id representing the edge, it can be used with edit actions. The OrderNr defines the order of the edges with respect to the subject.

Most edge information is accessed using the <a href="/id/doc_model_model_rsc" class="doc-reference doc-reference-model"><code>model#rsc</code></a> model, but some information can only accessed with the m\_edge model.

This model implements two template accessible options. They are mainly used to obtain the edge's id for edit pages.

The following m\_edge model properties are available in templates:

| Property | Description                                                                      | Example value                                                                    |
| -------- | -------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- |
| o        | Returns a function that accepts a page id and a predicate. The end result is a list of tuples \{PageId, EdgeId\} which are objects of the page. Example usage: `m.edge.o[id].author` | `[{204,13},{510,14}, {508,15}]`                                                  |
| s        | Identical to the "o" property, except that this function returns the subject edges. |                                                                                  |
| o\_props | Similar to `m.edge.o[id].author` above, but returns a property list for the edges instead of the 2-tuple. | `> [ > {id, 86062}, > {subject_id, 10635}, > {predicate_id, 304}, > {object_id, 57577}, > {seq, 1}, > {creator_id, 1}, > {created, { > {2015,11,17}, > {11,23,32} > }} > ] > ]` |
| s\_props | Similar to `m.edge.s[id].author` above, but returns a property list for the edges instead of the 2-tuple. |                                                                                  |
| edges    | Returns a function that accepts a page id. The end result is a list of edges per predicate where the predicate is an atom and the edges are property lists. Example usage: `m.edge[10635]` | `See example below.`                                                             |
| id       | Look up an edge id by a subject/predicate/object triple. Example usage: `erlang m.edge.id[subject_id].relation[object_id]` or: `erlang m.edge.id[subject_id][predicate_name][object_id]` Returns `undefined` if the edge does not exist; otherwise returns an integer. | `213`                                                                            |

Example return value for `{% print m.edge[10635] %}`:

```erlang
[{about,[[{id,86060},
          {subject_id,10635},
          {predicate_id,300},
          {name,<<"about">>},
          {object_id,17433},
          {seq,1},
          {created,{{2015,11,17},{11,22,11}}},
          {creator_id,1}]]},
 {author,[[{id,6},
           {subject_id,10635},
           {predicate_id,301},
           {name,<<"author">>},
           {object_id,10634},
           {seq,1000000},
           {created,{{2015,2,3},{16,23,20}}},
           {creator_id,1}]]}]
```

Other Topics
------------

`model/edge/post/o/+subject/+predicate/+object` or `model/edge/post/s/+object/+predicate/+subject` inserts a new edge between resources.

The posted message can optionally include the name or id of the object, predicate and subject.

```javascript
cotonic.broker.publish("bridge/origin/edge/post/o/4312",
                       {
                         predicate: "author",
                         subject: 7575
                       });
```

It is also possible to insert edges via cotonics onclick topics.

```django
<div data-onclick-topic="bridge/origin/model/edge/post/o/{{ id }}/?/{{ m.acl.user }}">
   <button data-edge-predicate="is_going">Is Going</button>
   <button data-edge-predicate="is_interested">Might Go</button>
</div>
```

When a user clicks on a button, the model retrieves the predicate name (or id) from the `data-edge-predicate` attribute. This is also possible by for the object and subject attributes of the edge. When there is a `?` in the topic path, the value can be retrieved from a data attribute. The attribute value for object is: `data-edge-object`. For subject it is: `data-edge-subject`. If all path parts from the first `?` to the end are `?`, then those trailing `?` parts may be omitted from the topic path.

`model/edge/delete/o/+subject/+predicate/+object`, `model/edge/delete/s/+object/+predicate/+subject` or `model/edge/delete/edge/+edge_id` deletes the specified edge.

```javascript
cotonic.broker.publish("bridge/origin/edge/delete/edge/6776");
```

Or via a onclick topic.

```django
<button data-onclick-topic="bridge/origin/edge/delete/{{ edge_id }}">Delete</button
```

Available Model API Paths
-------------------------

| Method   | Path pattern                         | Description                                                                      |
| -------- | ------------------------------------ | -------------------------------------------------------------------------------- |
| `get`    | `/o/+id/+pred/...`                   | Return outgoing edge tuples `{ObjectId, EdgeId}` from subject `+id` for predicate `+pred` (subject must be visible). |
| `get`    | `/o_props/+id/+pred/...`             | Return outgoing edge rows (id/subject/predicate/object/seq/created/creator) from subject `+id` for predicate `+pred`. |
| `get`    | `/s/+id/+pred/...`                   | Return incoming edge tuples `{SubjectId, EdgeId}` pointing to object `+id` for predicate `+pred` (object must be visible). |
| `get`    | `/s_props/+id/+pred/...`             | Return incoming edge rows (id/subject/predicate/object/seq/created/creator) for object `+id` and predicate `+pred`. |
| `get`    | `/edges/+id/...`                     | Return all outgoing edges of resource `+id` grouped by predicate name.           |
| `get`    | `/id/+subjectid/+pred/+objectid/...` | Return edge id for (`+subjectid`, `+pred`, `+objectid`) or `undefined` if absent (subject must be visible). |
| `get`    | `/graph/...`                         | Return graph map (`nodes`, `edges`, `is_truncated`) for payload `ids`, with optional payload options `limit` and `unescape`. |
| `get`    | `/+id`                               | Return same grouped outgoing-edge data as `/edges/+id` for resource `+id`. No further lookups. |
| `post`   | `/o/...`                             | Insert one edge using object-oriented argument order `(subject, predicate, object)`; values come from path segments, payload keys, `message.data-edge-*`, or query args. |
| `post`   | `/s/...`                             | Insert one edge using subject-oriented path order `(object, predicate, subject)`; stored edge is still `(subject, predicate, object)`. |
| `delete` | `/o/...`                             | Delete edge(s) using object-oriented order `(subject, predicate, object)`.       |
| `delete` | `/s/...`                             | Delete edge(s) using subject-oriented path order `(object, predicate, subject)`; this maps to stored triple `(subject, predicate, object)`. |
| `delete` | `/edge/+edge`                        | Delete a single edge by edge id `+edge` (invalid id returns `enoent`). No further lookups. |

The `o`, `s`, `o_props` and `s_props` paths accept optional payload/query parameters `offset` and `limit`. With neither parameter, their existing full-list behavior is unchanged. With either parameter, offset defaults to 0 and limit to 20; limit must be 1–100 and offset plus limit cannot exceed 10000. Values can be integers or decimal strings. The predicate is required, and further path lookups remain supported. For example:

```text
/api/model/edge/get/o/123/author?offset=20&limit=10
/api/model/edge/get/s_props/123/subject?limit=20
```

Pagination retains the existing tuple/property-list result shape, `seq, id` ordering and caller ACL semantics. SQL selects only the requested subset without loading or caching the full list. It does not return totals or continuation metadata. Concurrent edits can shift offsets. Sequence ordering may still require sorting many edges; `get_page/6` is the Erlang helper for indexed candidate reads with endpoint ordering, endpoint/predicate visibility filtering and lookahead.

`/+name` marks a variable path segment. A trailing `/...` means extra path segments are accepted for further lookups.

Post Method Examples
--------------------

Path orientation notes:

- Object-oriented path (`/o/...`) uses order: `subject / predicate / object`.
- Subject-oriented path (`/s/...`) uses order: `object / predicate / subject`.
- Both forms create or delete the same edge triple in storage: `(subject, predicate, object)`.
- Trailing wildcard parts can be omitted: if all path parts from the first `?` to the end are `?`, you may stop the path at that first `?`.

Insert via object-oriented path:

```javascript
cotonic.broker.call(
    "bridge/origin/model/edge/post/o/7575/author/4312",
    {}
);
```

Insert via wildcard path values (`?`) resolved from payload:

```javascript
cotonic.broker.call(
    "bridge/origin/model/edge/post/o/?/?/?",
    {
        subject: 7575,
        predicate: "author",
        object: 4312
    }
);
```

Insert with trailing wildcards omitted (only subject in path, predicate/object from payload):

```javascript
cotonic.broker.call(
    "bridge/origin/model/edge/post/o/7575",
    {
        predicate: "author",
        object: 4312
    }
);
```

Insert with all values from payload (shortest path):

```javascript
cotonic.broker.call(
    "bridge/origin/model/edge/post/o",
    {
        subject: 7575,
        predicate: "author",
        object: 4312
    }
);
```

Insert via subject-oriented path:

```javascript
cotonic.broker.call(
    "bridge/origin/model/edge/post/s/4312/author/7575",
    {}
);
```

License: 

Attribution: 

Related resources: https://zotonic.com/api/agent/related/1297?language=en


## API access

- [API help](<https://zotonic.com/api-help>)
- [OpenAPI specification](<https://zotonic.com/openapi.json>)
- [This resource as JSON](<https://zotonic.com/api/agent/resource/1297?language=en>)
