# `Figler.Scene.Graph`
[🔗](https://github.com/open-pencil/figler/blob/v0.1.0-beta.3/lib/figler/scene/graph.ex#L1)

Effective scene graph handle.

`Figler.Scene.Index` is the fast raw projection of Kiwi node records.
`Figler.Scene.Graph` is the surface for resolved Figma semantics: every
instance is expanded into its own occurrences with swaps, component property
assignments, symbol overrides, uniform scale, placed constraints, and saved
derived data applied.

Instances are resolved natively by an occurrence-scoped interpreter ported
from OpenPencil's reader, and the graph stays in a native resource: nodes and
records are read on demand with `fetch/2`, `record/3`, and `records/3`, or
enumerated, and `to_map/2` materializes the parts a caller names. Layers
inside an expansion have clone GUIDs (`"clone:<instance>/<source>/..."`) so
each use of a component is addressable on its own. Overrides that cannot be
resolved are reported by `unresolved/2` rather than failing the build.

Malformed graph options and GUIDs raise `Figler.Error` with stable codes and
structured context.

# `t`

```elixir
@opaque t()
```

Opaque effective scene graph resource.

# `build`

```elixir
@spec build(
  binary() | Figler.Scene.source() | Figler.Scene.Index.t() | reference() | t(),
  keyword()
) :: t()
```

Builds an effective scene graph handle.

Accepts the same binary inputs as `Figler.Scene.index/1`, a
`t:Figler.Scene.source/0`, or another graph. A `%Figler.Scene.Index{}`
or native index resource carries no override data, so it builds a projected
graph with instances left unexpanded.

Options:

  * `:focus` - one or more source GUIDs. Only their subtrees, ancestors, and
    the components they use are decoded and interpreted.
  * `:root` - alias for `:focus`, useful for resolving one subtree.
  * `:resolve_variables` - `:known` resolves each layer's variable bindings
    in the modes it uses; `false` or `:preserve` keeps the values the file
    saved. Defaults to `:known`.
  * `:fields` - fields to project when building from an index.

# `diff`

```elixir
@spec diff(
  t(),
  keyword()
) :: [map()]
```

Diffs effective graph nodes against their projected source nodes.

Returns only nodes whose projected source differs from the effective node.
Pass `root:` to diff a subtree.

# `diff_node`

```elixir
@spec diff_node(Figler.Scene.Index.t() | reference(), t(), String.t()) :: map() | nil
```

Diffs one raw index node against its graph counterpart.

# `explain`

```elixir
@spec explain(t(), String.t()) :: map() | nil
```

Explains how a node appears in the effective graph.

The explanation includes the resolved node, its projected source node,
provenance, the property claims applied to it, and instance metadata for
instance nodes.

    graph = Figler.Scene.graph(fig_binary, root: "3740:112537")
    Figler.Scene.Graph.explain(graph, "3740:112537")

# `fetch`

```elixir
@spec fetch(t(), String.t()) :: {:ok, Figler.Scene.Node.t()} | :error
```

Fetches the node at a GUID string.

    {:ok, node} = Figler.Scene.Graph.fetch(graph, "3740:112537")

# `fetch!`

```elixir
@spec fetch!(t(), String.t()) :: Figler.Scene.Node.t()
```

Fetches the node at a GUID string, raising `KeyError` when the graph has none.

# `instance`

```elixir
@spec instance(t(), String.t()) :: map() | nil
```

Returns instance-specific effective graph metadata for a node.

Returns `nil` for non-instance nodes.

# `profile`

```elixir
@spec profile(
  binary() | Figler.Scene.source(),
  keyword()
) :: map()
```

Profiles graph construction.

Returns the graph, the total time, and the time of each phase: `:decode`,
`:index`, and `:interpret` (which includes materializing the graph).
Options are the same as `build/2`.

# `record`

```elixir
@spec record(t(), String.t(), keyword()) :: map() | struct() | nil
```

Returns the effective record of a graph node: the sparse `NodeChange` map
with every override resolved, or `nil`.

Options:

  * `:fields` - the record fields to return, as atoms. Defaults to all.
  * `:as` - `:map` (default) for a sparse map, or `:struct` for the generated
    `Figler.Schema` structs.

# `records`

```elixir
@spec records(t(), [String.t()], keyword()) :: %{
  optional(String.t()) =&gt; map() | struct()
}
```

Returns the effective records of graph nodes by GUID string, leaving out GUIDs
the graph lacks. Takes the options of `record/3`.

# `source_node`

```elixir
@spec source_node(t(), String.t()) :: Figler.Scene.Node.t() | nil
```

Returns the projected source node a graph node was built from.

# `stats`

```elixir
@spec stats(t()) :: map()
```

Returns graph build/runtime stats.

# `to_map`

```elixir
@spec to_map(
  t(),
  keyword()
) :: map()
```

Materializes the parts of the graph named by `only:` as plain Elixir data,
keyed by internal graph GUIDs:

  * `:nodes` - scene nodes by GUID
  * `:records` - effective records by GUID
  * `:children` - each parent's child GUIDs, in order
  * `:roots` - the source GUIDs the graph starts from
  * `:by_type` and `:by_name` - GUIDs by node type and by name
  * `:provenance` - where each node that is not a plain source node came from
  * `:unresolved` - the overrides the graph could not resolve

Records and nodes of a whole document are large; name only what is needed.

# `tree`

```elixir
@spec tree(
  t(),
  keyword()
) :: [Figler.Scene.Node.t()] | Figler.Scene.Node.t() | nil
```

Returns a resolved tree from the effective graph.

Options:

  * `:root` - graph GUID string/tuple to use as root. Defaults to all roots.
  * `:depth` - maximum child depth. Defaults to `3`.

    graph = Figler.Scene.graph(fig_binary, root: "3740:112537")
    Figler.Scene.Graph.tree(graph, root: "3740:112537", depth: 3)

# `unresolved`

```elixir
@spec unresolved(
  t(),
  keyword()
) :: [term()]
```

Returns diagnostics for overrides the graph could not resolve.

  * `{:unresolved_property, owner, main_component, path, reason}`
  * `{:unresolved_assignment, owner, main_component, path, reason}`
  * `{:missing_component, owner, component}`
  * `{:missing_slot_content, slot, content}`
  * `{:unsupported_variable_expression, variable, mode}`

GUIDs are `{session_id, local_id}` tuples and `reason` is `:missing_target`
or `:ambiguous_target`.

---

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