Filtering
The api.filter namespace lets you declaratively hide nodes and edges that
don't match criteria. Filters are composable, named, and removable —
unlike hidden, which is a flat ID set you manage yourself, filters compute
their match set from the current data and re-evaluate when the graph changes.
api.filter.nodes.* filters nodes, api.filter.edges.* filters edges, and
the cross-cutting any / all composers combine filter handles into
boolean trees.
Filter by property range
The most common case — keep nodes whose numeric property falls in a window:
// Show only people aged 25-45
const h = api.filter.nodes.range({
property: "age",
min: 25,
max: 45,
});
// Later: drop the filter
api.filter.clear(h);
range returns a FilterHandle you keep around to remove later. Set
api.filter.clear() with no argument to remove every active filter.
For date-shaped properties:
const recent = api.filter.nodes.dateRange({
property: "createdAt",
after: new Date("2024-01-01"),
before: new Date("2024-12-31"),
});
Filter by category / enum
Keep only nodes whose category-style property matches one of N values:
// Karate club: hide everyone outside Mr Hi's faction
const mrHiOnly = api.filter.nodes.match({
property: "faction",
oneOf: ["Mr Hi"],
});
match checks string equality. For multi-valued or array-typed properties,
use predicate instead.
Filter by custom predicate
When the criterion isn't a simple range or enum membership, pass a function:
const lonely = api.filter.nodes.predicate(
(node) => api.query.node(node.id).neighbors().count() < 2,
);
The predicate runs once per node on every relevant change (data mutation, delta apply). Keep it cheap.
Filter by degree
For "show only hubs" or "hide leaves":
const hubs = api.filter.nodes.degree({ min: 5 }); // ≥ 5 connections
const leaves = api.filter.nodes.degree({ exactly: 1 }); // degree = 1
Composing filters
any (logical OR) and all (logical AND) combine multiple handles:
const adults = api.filter.nodes.range({ property: "age", min: 18 });
const engineers = api.filter.nodes.match({ property: "role", oneOf: ["engineer"] });
// Adults AND engineers visible
const both = api.filter.all([adults, engineers]);
// Adults OR engineers
const either = api.filter.any([adults, engineers]);
The composed handle is itself a FilterHandle — pass it back into any/
all to build deeper trees, and clear it to drop the whole subtree.
Watching the match count
When you build a filter UI (a sidebar showing "showing 42 of 200 nodes"),
subscribe to onChange:
useEffect(() => {
const unsub = api.filter.onChange((result) => {
setMatchCount(result.matchCount);
});
return unsub;
}, [api]);
For a one-shot read use matchCount():
const { nodes, edges } = api.filter.matchCount();
console.log(`${nodes.visible} / ${nodes.total} nodes visible`);
Filters vs hidden
api.filter.* |
hidden prop |
|
|---|---|---|
| Source of truth | engine (computed from data) | host (explicit ID set) |
| Reacts to data mutations | ✓ (re-evaluates) | ✗ (stale until you update the set) |
| Composable | ✓ (any/all trees) |
✗ (one flat set) |
| Removable | ✓ (clear(handle)) |
✓ (mutate the array) |
| Inspectable count | ✓ (matchCount()) |
host computes |
Use filters for declarative, data-driven hiding. Use hidden when
the host already knows exactly which IDs to hide (a checkbox UI, an
external query result).
The two compose — a node is visible only when no filter matches it for
removal AND its id is not in hidden.
See also
api.filternamespace — full method referencehidden— controlled visibility propapi.query— graph traversal used inside predicates