gcyphrq-ext-graphml 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Pascal Le Levier
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,97 @@
1
+ # gcyphrq-ext-graphml
2
+
3
+ GraphML graph format extension for [gcyphrq](https://github.com/plelevier/gcyphrq).
4
+
5
+ Converts [GraphML](https://graphml.graphdrawing.org/) files into gcyphrq's in-memory graph format.
6
+
7
+ ## Install
8
+
9
+ ### Global CLI install
10
+
11
+ Install `gcyphrq` and this extension globally so the `gcyphrq` command is available everywhere:
12
+
13
+ ```bash
14
+ npm install -g gcyphrq gcyphrq-ext-graphml
15
+ ```
16
+
17
+ ### Project dependency install
18
+
19
+ Install both as project dependencies:
20
+
21
+ ```bash
22
+ npm install gcyphrq gcyphrq-ext-graphml
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ### CLI
28
+
29
+ ```bash
30
+ gcyphrq -g my-graph.graphml --ext graphml -e 'MATCH (n) RETURN n'
31
+ ```
32
+
33
+ ### Library
34
+
35
+ ```ts
36
+ import { convertWithExtension, executeQuery } from 'gcyphrq';
37
+ import { readFileSync } from 'fs';
38
+
39
+ const content = readFileSync('my-graph.graphml', 'utf-8');
40
+ const graphData = await convertWithExtension('graphml', {
41
+ content,
42
+ filePath: 'my-graph.graphml',
43
+ });
44
+
45
+ const results = await executeQuery(graphData, 'MATCH (n) RETURN n');
46
+ ```
47
+
48
+ ## Supported formats
49
+
50
+ - `.graphml` files (GraphML 1.0)
51
+ - `.xml` files with GraphML content
52
+
53
+ Parses directed and undirected graphs. Supports node/edge attributes, typed key definitions, and edge identifiers.
54
+
55
+ ## Examples
56
+
57
+ See the `examples/` directory for sample GraphML files:
58
+
59
+ ### Simple directed graph
60
+
61
+ ```bash
62
+ # List all persons
63
+ gcyphrq -g examples/simple-directed.graphml --ext graphml -e 'MATCH (n:Person) RETURN n.name'
64
+
65
+ # Find who Alice knows
66
+ gcyphrq -g examples/simple-directed.graphml --ext graphml -e 'MATCH (a:Person {name: "Alice"})-[r]->(b:Person) RETURN b.name, type(r)'
67
+
68
+ # Find all relationships
69
+ gcyphrq -g examples/simple-directed.graphml --ext graphml -e 'MATCH (a:Person)-[r]->(b:Person) RETURN a.name, type(r), b.name'
70
+ ```
71
+
72
+ ### Graph with typed attributes
73
+
74
+ ```bash
75
+ # Find all engineers
76
+ gcyphrq -g examples/with-attributes.graphml --ext graphml -e 'MATCH (n:Employee) WHERE n.role = "engineer" RETURN n.name, n.age'
77
+
78
+ # Find edges with weight > 0.8
79
+ gcyphrq -g examples/with-attributes.graphml --ext graphml -e 'MATCH (a:Employee)-[r]->(b:Employee) WHERE r.weight > 0.8 RETURN a.name, type(r), b.name'
80
+
81
+ # Find who manages whom
82
+ gcyphrq -g examples/with-attributes.graphml --ext graphml -e 'MATCH (a:Employee)-[r:MANAGES]->(b:Employee) RETURN a.name, b.name'
83
+ ```
84
+
85
+ ### Undirected graph
86
+
87
+ ```bash
88
+ # List all city connections
89
+ gcyphrq -g examples/undirected.graphml --ext graphml -e 'MATCH (a:City)--(b:City) RETURN a.name, b.name'
90
+
91
+ # Find cities connected to Paris
92
+ gcyphrq -g examples/undirected.graphml --ext graphml -e 'MATCH (c:City {name: "Paris"})--(neighbor:City) RETURN neighbor.name'
93
+ ```
94
+
95
+ ## License
96
+
97
+ MIT
@@ -0,0 +1,3 @@
1
+ import type { GraphInputExtension } from 'gcyphrq';
2
+ declare const graphmlExtension: GraphInputExtension;
3
+ export default graphmlExtension;
package/dist/index.js ADDED
@@ -0,0 +1,86 @@
1
+ import { parse } from 'graphology-graphml';
2
+ import Graph from 'graphology';
3
+ /**
4
+ * Convert a Graphology graph (created by graphology-graphml) into the
5
+ * GraphInput shape that gcyphrq expects.
6
+ *
7
+ * GraphML stores node labels and edge types as `<data>` elements.
8
+ * The `label` attribute on nodes maps to the gcyphrq node label (kind)
9
+ * (configurable via `ctx.labelProperty`).
10
+ * The `type` attribute on edges maps to the gcyphrq relationship type
11
+ * (configurable via `ctx.edgeTypeProperty`).
12
+ *
13
+ * For mixed graphs, `graphology-graphml` can separate directed and
14
+ * undirected edges. We iterate undirected edges first to mark them,
15
+ * then add all edges.
16
+ */
17
+ function graphologyToGraphInput(graph, edgeTypeProperty, labelProperty) {
18
+ const nodes = [];
19
+ const edges = [];
20
+ for (const id of graph.nodes()) {
21
+ const attrs = { ...graph.getNodeAttributes(id) };
22
+ // GraphML stores the node label in a "label" data element by default.
23
+ // If the user provided a custom labelProperty and the node doesn't
24
+ // already have that property, remap the attribute name.
25
+ if (labelProperty !== 'label' && attrs.label !== undefined && attrs[labelProperty] === undefined) {
26
+ attrs[labelProperty] = attrs.label;
27
+ delete attrs.label;
28
+ }
29
+ nodes.push({ key: id, attributes: attrs });
30
+ }
31
+ const isMixed = graph.type === 'mixed';
32
+ // In mixed graphs, graphology-graphml stores undirected edges separately.
33
+ // We iterate undirected edges first to mark them, then add all edges.
34
+ const undirectedEdgeIds = new Set();
35
+ if (isMixed && typeof graph.forEachUndirectedEdge === 'function') {
36
+ graph.forEachUndirectedEdge((edgeId) => {
37
+ undirectedEdgeIds.add(edgeId);
38
+ });
39
+ }
40
+ graph.forEachEdge((edgeId, attrs, source, target) => {
41
+ const edgeAttrs = { ...attrs };
42
+ // GraphML stores the relationship type in a "type" data element by default.
43
+ // If the user provided a custom edgeTypeProperty and the edge doesn't
44
+ // already have that property, we don't need to remap — the graphml parser
45
+ // already uses the key name directly.
46
+ if (edgeTypeProperty !== 'type' && edgeAttrs.type !== undefined && edgeAttrs[edgeTypeProperty] === undefined) {
47
+ edgeAttrs[edgeTypeProperty] = edgeAttrs.type;
48
+ delete edgeAttrs.type;
49
+ }
50
+ const isUndirected = undirectedEdgeIds.has(edgeId);
51
+ edges.push({
52
+ key: edgeId,
53
+ source,
54
+ target,
55
+ ...(isUndirected ? { undirected: true } : {}),
56
+ attributes: edgeAttrs,
57
+ });
58
+ });
59
+ return { nodes, edges };
60
+ }
61
+ const graphmlExtension = {
62
+ async convert(ctx) {
63
+ const content = typeof ctx.content === 'string' ? ctx.content : ctx.content.toString();
64
+ const edgeTypeProperty = ctx.edgeTypeProperty ?? 'type';
65
+ const labelProperty = ctx.labelProperty ?? 'label';
66
+ // graphology-graphml needs a Graph constructor. We pass the base Graph
67
+ // constructor which the parser uses to instantiate the right graph type
68
+ // based on the edgedefault attribute.
69
+ try {
70
+ const graph = parse(Graph, content);
71
+ const result = graphologyToGraphInput(graph, edgeTypeProperty, labelProperty);
72
+ // Surface the graph type from the GraphML edgedefault attribute
73
+ // so gcyphrq creates the correct Graphology graph.
74
+ const graphType = graph.type;
75
+ if (graphType !== 'directed') {
76
+ result.options = { type: graphType };
77
+ }
78
+ return result;
79
+ }
80
+ catch (err) {
81
+ const message = err instanceof Error ? err.message : String(err);
82
+ throw new Error(`Failed to parse GraphML file "${ctx.filePath}": ${message}`);
83
+ }
84
+ },
85
+ };
86
+ export default graphmlExtension;
@@ -0,0 +1,26 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <graphml xmlns="http://graphml.graphdrawing.org/xmlns">
3
+ <graph edgedefault="directed">
4
+ <node id="0">
5
+ <data key="label">Person</data>
6
+ <data key="name">Alice</data>
7
+ </node>
8
+ <node id="1">
9
+ <data key="label">Person</data>
10
+ <data key="name">Bob</data>
11
+ </node>
12
+ <node id="2">
13
+ <data key="label">Person</data>
14
+ <data key="name">Charlie</data>
15
+ </node>
16
+ <edge id="0" source="0" target="1">
17
+ <data key="type">KNOWS</data>
18
+ </edge>
19
+ <edge id="1" source="1" target="2">
20
+ <data key="type">KNOWS</data>
21
+ </edge>
22
+ <edge id="2" source="0" target="2">
23
+ <data key="type">FRIEND</data>
24
+ </edge>
25
+ </graph>
26
+ </graphml>
@@ -0,0 +1,20 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <graphml xmlns="http://graphml.graphdrawing.org/xmlns">
3
+ <graph edgedefault="undirected">
4
+ <node id="a">
5
+ <data key="label">City</data>
6
+ <data key="name">Paris</data>
7
+ </node>
8
+ <node id="b">
9
+ <data key="label">City</data>
10
+ <data key="name">Lyon</data>
11
+ </node>
12
+ <node id="c">
13
+ <data key="label">City</data>
14
+ <data key="name">Marseille</data>
15
+ </node>
16
+ <edge id="e1" source="a" target="b"/>
17
+ <edge id="e2" source="b" target="c"/>
18
+ <edge id="e3" source="a" target="c"/>
19
+ </graph>
20
+ </graphml>
@@ -0,0 +1,58 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <graphml xmlns="http://graphml.graphdrawing.org/xmlns">
3
+ <key id="d0" for="node" attr.name="label" attr.type="string">
4
+ <default>DefaultLabel</default>
5
+ </key>
6
+ <key id="d1" for="node" attr.name="name" attr.type="string"/>
7
+ <key id="d2" for="node" attr.name="age" attr.type="int"/>
8
+ <key id="d3" for="node" attr.name="role" attr.type="string"/>
9
+ <key id="d4" for="edge" attr.name="weight" attr.type="double"/>
10
+ <key id="d5" for="edge" attr.name="since" attr.type="string"/>
11
+ <key id="d6" for="edge" attr.name="type" attr.type="string"/>
12
+ <graph edgedefault="directed">
13
+ <node id="0">
14
+ <data key="d0">Employee</data>
15
+ <data key="d1">Alice</data>
16
+ <data key="d2">30</data>
17
+ <data key="d3">engineer</data>
18
+ </node>
19
+ <node id="1">
20
+ <data key="d0">Employee</data>
21
+ <data key="d1">Bob</data>
22
+ <data key="d2">25</data>
23
+ <data key="d3">designer</data>
24
+ </node>
25
+ <node id="2">
26
+ <data key="d0">Employee</data>
27
+ <data key="d1">Charlie</data>
28
+ <data key="d2">35</data>
29
+ <data key="d3">manager</data>
30
+ </node>
31
+ <node id="3">
32
+ <data key="d0">Employee</data>
33
+ <data key="d1">Diana</data>
34
+ <data key="d2">28</data>
35
+ <data key="d3">engineer</data>
36
+ </node>
37
+ <edge id="e0" source="0" target="1">
38
+ <data key="d6">WORKS_WITH</data>
39
+ <data key="d4">0.8</data>
40
+ <data key="d5">2023</data>
41
+ </edge>
42
+ <edge id="e1" source="0" target="3">
43
+ <data key="d6">WORKS_WITH</data>
44
+ <data key="d4">0.9</data>
45
+ <data key="d5">2022</data>
46
+ </edge>
47
+ <edge id="e2" source="2" target="0">
48
+ <data key="d6">MANAGES</data>
49
+ <data key="d4">1.0</data>
50
+ <data key="d5">2021</data>
51
+ </edge>
52
+ <edge id="e3" source="2" target="1">
53
+ <data key="d6">MANAGES</data>
54
+ <data key="d4">1.0</data>
55
+ <data key="d5">2023</data>
56
+ </edge>
57
+ </graph>
58
+ </graphml>
package/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "gcyphrq-ext-graphml",
3
+ "version": "0.1.0",
4
+ "description": "GraphML graph format extension for gcyphrq",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js"
11
+ },
12
+ "files": [
13
+ "dist",
14
+ "examples",
15
+ "README.md",
16
+ "LICENSE"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsc",
20
+ "test": "vitest run",
21
+ "prepublishOnly": "npm run build"
22
+ },
23
+ "dependencies": {
24
+ "graphology": "^0.26.0",
25
+ "graphology-graphml": "^0.5.2"
26
+ },
27
+ "peerDependencies": {
28
+ "gcyphrq": ">=0.61.5"
29
+ },
30
+ "devDependencies": {
31
+ "typescript": "^5.8.3",
32
+ "vitest": "^4.1.9"
33
+ },
34
+ "keywords": [
35
+ "gcyphrq",
36
+ "gcyphrq-extension",
37
+ "graphml",
38
+ "graph"
39
+ ],
40
+ "license": "MIT",
41
+ "repository": {
42
+ "type": "git",
43
+ "url": "https://github.com/plelevier/gcyphrq-ext-graphml"
44
+ },
45
+ "allowScripts": {
46
+ "fsevents@2.3.3": true
47
+ },
48
+ "gcyphrqExtensions": {
49
+ "graphml": {
50
+ "type": "graph-input",
51
+ "description": "Convert GraphML files to gcyphrq graph format",
52
+ "entryPoint": "./dist/index.js",
53
+ "fileExtensions": [
54
+ ".graphml",
55
+ ".xml"
56
+ ]
57
+ }
58
+ }
59
+ }