Skip to main content
Version: 4.3

GraphApi

Imperative API returned by useGraph.

Type Parameters​

Type ParameterDefault typeDescription
Element extends ElementJSONInitElementJSONInitelement record shape (e.g. ElementRecord<MyData> for write input, Computed<ElementRecord<MyData>> for reads)
Link extends LinkJSONInitLinkJSONInitlink record shape (e.g. LinkRecord<MyData> / Computed<LinkRecord<MyData>>)

Properties​

exportToJSON​

readonly exportToJSON: (options?) => JSON;

Serialize the graph to a plain JSON object.

By default the output is minimal: attributes that match each cell's defaults are dropped and empty {} placeholders are pruned everywhere except inside attrs at the third nesting level (e.g. attrs.text.textWrap: {} is a meaningful reset marker in JointJS shapes and must survive). Pass { includeDefaults: true } to keep every attribute on every cell, no pruning is applied in that mode.

Parameters​

ParameterType
options?ExportToJSONOptions

Returns​

JSON


graph​

readonly graph: Graph;

The JointJS graph instance.


importFromJSON​

readonly importFromJSON: (json) => void;

Replace the graph contents from a previously exported JSON object (e.g. produced by exportToJSON). Triggers JointJS's reset event so all React subscriptions resync automatically.

Parameters​

ParameterType
jsonJSON

Returns​

void


isElement​

readonly isElement: (input) => input is Element;

Predicate / type guard: true when the input resolves to an element cell. Consults the graph's type registry so any dia.Element subclass (including custom shapes) is recognised, not just the default ElementModel.

Parameters​

ParameterType
inputElement | Link

Returns​

input is Element


readonly isLink: (input) => input is Link;

Predicate / type guard: true when the input resolves to a link cell. Consults the graph's type registry so any dia.Link subclass (including custom shapes) is recognised, not just the default LinkModel.

Parameters​

ParameterType
inputElement | Link

Returns​

input is Link


removeCell​

readonly removeCell: (cellRef?, metadata?) => void;

Remove a cell by id or dia.Cell reference. A nullish reference warns in dev and no-ops; a reference that resolves to no cell is a silent no-op. The optional metadata is forwarded as the graph.removeCells event opt.

Parameters​

ParameterType
cellRef?CellRef | null
metadata?Record<string, unknown>

Returns​

void


removeCells​

readonly removeCells: (cellRefs?, metadata?) => void;

Remove multiple cells by id or dia.Cell reference. A nullish array warns in dev and no-ops; references that resolve to no cell are silently skipped. The optional metadata is forwarded as the graph.removeCells event opt.

Parameters​

ParameterType
cellRefs?readonly CellRef[] | null
metadata?Record<string, unknown>

Returns​

void


resetCells​

readonly resetCells: (input, metadata?) => void;

Atomically replace the cell set. Accepts dia.Cell instances alongside records. The optional metadata is forwarded as the graph.resetCells opt.

Parameters​

ParameterType
inputArrayUpdate<Element | Link, CellInput<Element, Link>>
metadata?Record<string, unknown>

Returns​

void


setCell​

readonly setCell: SetCell<Element, Link>;

Add or update a cell. Two forms:

  • setCell(record), record.id names the target. Existing cell: attributes merge over it. Missing cell: the cell is added.
  • setCell(id, (prev) => next), updater form. The updater is invoked once with the real previous record. A nullish id, or an id with no matching cell, warns in dev and no-ops (use the direct form to add).

setCellData​

readonly setCellData: SetCellData<HandleCellData<Element, Link>>;

Set a single cell's data field. Two forms, both keyed by cell id:

  • setCellData(id, data), replaces the cell's data with data.
  • setCellData(id, (prev) => next), updater form; prev is the current data, the return value replaces it (merge inside the updater for a partial update). A nullish id, or an id with no matching cell, warns in dev and no-ops.

The data type is derived from the useGraph<Element, Link> generics: typed records flow through, otherwise it falls back to Record<string, unknown>. A cell id can't be narrowed to element-vs-link at the type level, so when both are typed the updater sees their union. Narrow inside it, or fix the data shape via useGraph<ElementRecord<MyData>>().


transaction​

readonly transaction: Transaction;

Run a callback as one atomic transaction: every edit inside collapses into a single undo entry and (for sync callbacks) a single re-render. Pass { rollbackOnError: true } to restore the graph on error (off by default — partial edits stay; enabling it snapshots the full cells array up-front, so leave off for large graphs when the callback is trusted). See Transaction.


updateCells​

readonly updateCells: (updater, metadata?) => void;

Apply an updater to the current cells array. Updater may return dia.Cell instances. The optional metadata is forwarded as the sync event opt.

Parameters​

ParameterType
updater(previous) => readonly CellInput<Element, Link>[]
metadata?Record<string, unknown>

Returns​

void