Skip to content

Commit 70c2cdb

Browse files
authored
Merge pull request #723 from GraphScope/fix-path
feat: Fix cases where the query result is a path.
2 parents 60ed66e + 98f369c commit 70c2cdb

File tree

1 file changed

+49
-17
lines changed

1 file changed

+49
-17
lines changed

packages/studio-driver/src/cypher-driver.ts

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class CypherDriver {
106106
*/
107107
async query(cypher: string): Promise<Graph | Table> {
108108
try {
109+
console.log('%c[Cypher Query Driver] Statement', 'color:blue', cypher);
109110
const session = await this.getSession();
110111
if (!session) {
111112
return {
@@ -114,10 +115,32 @@ class CypherDriver {
114115
};
115116
}
116117
const result = await session.run(cypher);
117-
console.log('%c[Cypher Query Driver] QueryCypher 查询语句', 'color:blue', cypher);
118-
console.log('%c[Cypher Query Driver] QueryCypher 查询结果', 'color:green', result);
119-
session.close();
120-
return processResult(result);
118+
console.log('%c[Cypher Query Driver] Result', 'color:green', result);
119+
120+
const { nodes_need_properties, ...data } = processResult(result);
121+
if (nodes_need_properties.length === 0) {
122+
session.close();
123+
return data;
124+
} else {
125+
/** path 中的node 不包含属性,需要在这里额外请求一次 **/
126+
const script = `
127+
Match (n) where elementId(n) in [${nodes_need_properties}] return n;
128+
`;
129+
const result_nodes = await session.run(script);
130+
const { nodes } = processResult(result_nodes);
131+
const nodeMap = nodes.reduce((acc, curr) => {
132+
acc[curr.id] = curr;
133+
return acc;
134+
}, {});
135+
/** 将属性追加到数据中 */
136+
data.nodes.forEach(item => {
137+
if (nodeMap[item.id]) {
138+
item.properties = { ...item.properties, ...nodeMap[item.id].properties };
139+
}
140+
});
141+
session.close();
142+
return data;
143+
}
121144
} catch (error: any) {
122145
return {
123146
nodes: [],
@@ -150,6 +173,7 @@ export function processResult(result) {
150173
const nodes: GraphNode[] = [];
151174
const edges: GraphEdge[] = [];
152175
const table: any[] = [];
176+
const nodes_need_properties: Set<any> = new Set();
153177
result.records.forEach(record => {
154178
let tempRow = {};
155179
//@ts-ignore
@@ -183,39 +207,42 @@ export function processResult(result) {
183207
});
184208
}
185209
if (isPath) {
186-
const { segments } = item as Path;
210+
const { start, segments, end } = item as Path;
211+
187212
segments.forEach(c => {
188213
const { start, end, relationship } = c;
189-
const { identity: startIdentity, labels: startLabels, properties: startProperties } = start;
190-
const { identity: endIdentity, labels: endLabels, properties: endProperties } = end;
214+
const { elementId: startElementId, labels: startLabels, properties: startProperties } = start;
215+
const { elementId: endElementId, labels: endLabels, properties: endProperties } = end;
191216
let hasStarNode, hasEndNode;
192217

193218
nodes.forEach(item => {
194-
if (item.id === startIdentity.low.toString()) hasStarNode = true;
195-
if (item.id === endIdentity.low.toString()) hasEndNode = true;
219+
if (item.id === startElementId) hasStarNode = true;
220+
if (item.id === endElementId) hasEndNode = true;
196221
});
197222

198223
if (!hasStarNode) {
199224
nodes.push({
200-
id: startIdentity.low.toString(),
225+
id: startElementId,
201226
label: startLabels[0],
202227
properties: processProperties(start.properties),
203228
});
229+
nodes_need_properties.add(startElementId);
204230
}
205231
if (!hasEndNode) {
206232
nodes.push({
207-
id: endIdentity.low.toString(),
233+
id: endElementId,
208234
label: endLabels[0],
209235
properties: processProperties(end.properties),
210236
});
237+
nodes_need_properties.add(endElementId);
211238
}
212-
const { identity, type, start: source, end: target } = relationship;
213-
const hasRelationship = edges.find(d => d.id === identity.low.toString());
239+
const { elementId: edgeElementId, type, startNodeElementId, endNodeElementId } = relationship;
240+
const hasRelationship = edges.find(d => d.id === edgeElementId);
214241
if (!hasRelationship) {
215242
edges.push({
216-
id: 'e_' + identity.low.toString(),
217-
source: source.low.toString(),
218-
target: target.low.toString(),
243+
id: edgeElementId,
244+
source: startNodeElementId,
245+
target: endNodeElementId,
219246
label: type,
220247
properties: processProperties(relationship.properties),
221248
});
@@ -235,7 +262,12 @@ export function processResult(result) {
235262
}
236263
});
237264

238-
return { ...transformData(nodes, edges), table, raw: result };
265+
return {
266+
...transformData(nodes, edges),
267+
table,
268+
raw: result,
269+
nodes_need_properties: [...nodes_need_properties.values()],
270+
};
239271
}
240272

241273
export function transformData(_nodes, _edges) {

0 commit comments

Comments
 (0)