# `PgRest.SchemaGraph`
[🔗](https://github.com/agoodway/pgrest/blob/v0.2.0/lib/pg_rest/schema_graph.ex#L1)

The relationship graph over registered resources, with PostgREST's embedding
resolution rules.

Relationships come from these places, none of which touch the database:

  * Ecto associations: `belongs_to` gives `:m2o` (and the inverse `:o2m`),
    `has_many` / `has_one` the same pair. The foreign key constraint name defaults
    to `"<table>_<column>_fkey"`.
  * Junctions, as in PostgREST: a registered resource with `:m2o` relationships to
    two tables whose key columns all lie in its primary key yields `:m2m` in both
    directions (self joins included). `many_to_many` associations are not a source;
    their name is accepted as an embed alias for the derived relationship.
  * Overrides in the resource's `relationships:` option keyed by association name:
    `[constraint: "client"]` renames the constraint, `[cardinality: :one_to_one]`
    makes the pair `:o2o`.
  * Explicit declarations in `relationships:`, needed for composite keys:

        relationships: [
          students_info: [
            kind: :o2o,
            target: MyApp.StudentsInfo,
            columns: [{"code", "code"}, {"id", "id"}],
            constraint: "students_info_code_id_fkey"
          ],
          files: [
            kind: :m2m,
            target: MyApp.Files,
            junction: [
              table: "touched_files",
              schema: "public",
              source_columns: [{"user_id", "user_id"}],
              target_columns: [{"filename", "filename"}],
              constraints: {"fk_users_tasks", "fk_upload"}
            ]
          ]
        ]

    An explicit `:m2m` yields both directions and its junction table must be a
    registered resource (`ArgumentError` otherwise, as for an unregistered `target:`).
    `:m2o` declares the foreign key on this resource; `:o2m` and `:o2o` declare it
    on the target. `columns` pairs are `{source_column, target_column}`.

Every foreign key yields both directions, so a relationship is discoverable from
either resource even when only one side declares it.

# `t`

```elixir
@type t() :: %PgRest.SchemaGraph{
  rels: %{required(module()) =&gt; [PgRest.SchemaGraph.Relationship.t()]},
  tables: %{required(module()) =&gt; {schema :: String.t(), table :: String.t()}}
}
```

# `build`

```elixir
@spec build([module()]) :: t()
```

Builds the graph for the given resource modules. Ecto associations to other modules
are ignored; explicit declarations naming an unregistered target or junction raise
`ArgumentError`.

# `no_relationship_from`

```elixir
@spec no_relationship_from(t(), String.t(), String.t(), String.t(), String.t() | nil) ::
  PgRest.Error.t()
```

`PGRST200` for an embed `name` from `parent`, a source that is no registered resource and so
has no relationships (a function's composite result): as PostgREST, the hint names the
closest table of `schema` that has relationships.

# `resolve`

```elixir
@spec resolve(t(), module(), String.t(), String.t() | nil) ::
  {:ok, PgRest.SchemaGraph.Relationship.t()} | {:error, PgRest.Error.t()}
```

Resolves the embed `name` (with optional `hint`) from `source`.

Mirrors PostgREST's `findRel`: the name is a target table, a constraint or a
single foreign-key column of the source; the hint a constraint, a single key
column on either side, or a junction table. A name that is an association name
on `source` is accepted when nothing else matches.

Errors are `PGRST200` (nothing matches, 400) and `PGRST201` (several match, 300).

# `restrict`

```elixir
@spec restrict(t(), String.t()) :: t()
```

The relationships a request against `schema` can embed: those whose source and target
tables both live in `schema`. PostgREST keys its relationships by the foreign table's
schema and looks them up with the request schema, so a foreign key into another exposed
schema is not embeddable (`PGRST200`), even though it is in the full graph.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
