Skip to content

feat: export graph as PNG image or JSON data#411

Open
shaunpatterson wants to merge 2 commits into
dgraph-io:mainfrom
shaunpatterson:sp/graph-export
Open

feat: export graph as PNG image or JSON data#411
shaunpatterson wants to merge 2 commits into
dgraph-io:mainfrom
shaunpatterson:sp/graph-export

Conversation

@shaunpatterson

Copy link
Copy Markdown
Contributor

What

Adds graph export to the query results view — a standard feature in comparable tools (Neo4j Browser, Memgraph Lab) that Ratel lacks. Two new buttons in the graph toolbar next to Fit to screen:

  • Download PNG — composites every <canvas> inside .graph-container (in DOM order) onto a single white-backed canvas and downloads it via toBlob. Renderer-agnostic: works with the current d3 canvas renderer and any future layered/WebGL renderer.
  • Download JSON — serializes the current nodes/edges to plain JSON ({nodes: [{uid, label, group, attrs, facets}], edges: [{source, target, predicate, facets}]}).

Implementation notes

  • Edge source/target may be uid strings or node objects (d3-force resolves them to objects in place after first render). graphToJSON handles both shapes and always emits uids, so the output is free of circular references — covered by tests.
  • Filenames are timestamped (ratel-graph-2026-06-12-0832.png).
  • lib/exportGraph.js has 8 unit tests (serialization, cycle-safety, canvas compositing, filename format).

Testing

  • npx jest src/lib/exportGraph — 8/8 pass (note: the rest of the suite is repaired separately in fix: repair broken Jest test suite (16/18 suites failing) #410).
  • npm run build passes.
  • Verified end-to-end in headless Chrome against a live Dgraph v25 cluster: both buttons produce valid downloads — the PNG renders the graph, the JSON has correct uid endpoints.

🤖 Generated with Claude Code

Adds two buttons to the graph toolbar (next to zoom-to-fit):

- PNG: composites every canvas in the graph container onto a single
  white-backed canvas and downloads it via toBlob, so it works with
  the current d3 canvas renderer and any future layered renderer.
- JSON: serializes the GraphParser node/edge Maps to plain JSON.
  Edge endpoints may be uid strings or node objects resolved in place
  by d3-force - both shapes export as uids, keeping the output free
  of circular references.

lib/exportGraph.js is covered by 8 unit tests. Verified in a real
browser against a live Dgraph cluster: both buttons download valid
files (the PNG renders the graph, the JSON carries correct uid
endpoints).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant