Data Mapping
Transform any JSON structure into graph data using a mapping config. No manual data transformation needed.
Basic Usage
import { Graph } from '@topokit/core';
const apiResponse = {
users: [
{ userId: 1, fullName: 'Alice Chen', role: 'engineer' },
{ userId: 2, fullName: 'Bob Smith', role: 'designer' },
],
connections: [
{ from: 1, to: 2, type: 'collaborates' },
],
};
const graph = Graph.fromData(apiResponse, {
nodes: {
path: 'users',
id: 'userId',
label: 'fullName',
group: 'role',
},
edges: {
path: 'connections',
source: 'from',
target: 'to',
label: 'type',
},
}); Node Mapping
| Property | Type | Description |
|---|---|---|
path | string | Dot-path to node array (e.g., 'data.users'). |
id | string | (item) => string | Node ID field or extractor. |
label | string | (item) => string | Display label field or extractor. |
group | string | (item) => string | Grouping field (for autoStyle). |
size | string | (item) => number | Node radius field or extractor. |
data | (item) => Record | Custom data payload (defaults to whole item). |
style | (item) => Partial<NodeStyle> | Per-node style overrides. |
Edge Mapping
| Property | Type | Description |
|---|---|---|
path | string | Dot-path to edge array. |
id | string | (item) => string | Edge ID (auto-generated if omitted). |
source | string | (item) => string | Source node ID field. |
target | string | (item) => string | Target node ID field. |
label | string | (item) => string | Edge label field. |
style | (item) => Partial<EdgeStyle> | Per-edge style overrides. |
Dot-Path Strings
Field resolvers accept dot-paths for nested properties: 'user.profile.name' accesses item.user.profile.name.
const graph = Graph.fromData(data, {
nodes: {
path: 'response.data.entities',
id: 'meta.uuid',
label: 'attributes.displayName',
group: 'attributes.category',
},
edges: {
path: 'response.data.relationships',
source: 'meta.sourceId',
target: 'meta.targetId',
},
}); Auto Styling
Enable autoStyle: true to assign distinct fill colors per group automatically.
const graph = Graph.fromData(data, {
nodes: { path: 'nodes', id: 'id', label: 'name', group: 'type' },
edges: { path: 'links', source: 'source', target: 'target' },
autoStyle: true,
autoEdgeStyle: true, // color edges by label value
}); Function Resolvers
Use functions for complex transformations.
const graph = Graph.fromData(rawData, {
nodes: {
path: 'items',
id: (item) => `node-${item.pk}`,
label: (item) => `${item.firstName} ${item.lastName}`,
group: (item) => item.score > 80 ? 'high' : 'low',
size: (item) => Math.max(15, Math.min(50, item.importance * 5)),
style: (item) => ({
shape: item.type === 'org' ? 'rect' : 'circle',
icon: item.emoji,
}),
},
edges: {
path: 'relationships',
source: (item) => `node-${item.subject}`,
target: (item) => `node-${item.object}`,
label: 'predicate',
style: (item) => ({
dashed: item.confidence < 0.5,
width: item.weight * 2,
}),
},
autoStyle: true,
}); Using with create()
import { Graph } from '@topokit/core';
import { create } from '@topokit/renderer-canvas';
const graph = Graph.fromData(apiResponse, mapping);
const app = create(container, {
graph, // pass the pre-built graph
layout: 'force',
theme: 'dark',
}); Next Steps
- Clustering -- group mapped nodes by category
- Legend -- auto-generate from mapped groups
- Filtering -- filter mapped data