Files
knowledge-graph-agent/frontend/src/services/graphApi.ts
T

52 lines
1.2 KiB
TypeScript
Raw Normal View History

2026-04-05 14:41:11 +08:00
import axios from 'axios';
2026-04-06 15:39:28 +08:00
import type { GraphData, Node } from '../types/graph';
2026-04-06 14:24:45 +08:00
import { API_BASE_URL, API_TIMEOUT } from '../config/constants';
2026-04-05 14:41:11 +08:00
2026-04-06 14:24:45 +08:00
/**
* API 客户端实例
* 统一配置了 base URL、超时时间和默认请求头
*/
2026-04-05 14:41:11 +08:00
const apiClient = axios.create({
baseURL: API_BASE_URL,
2026-04-06 14:24:45 +08:00
timeout: API_TIMEOUT,
2026-04-05 14:41:11 +08:00
headers: {
'Content-Type': 'application/json',
},
});
2026-04-06 14:24:45 +08:00
/**
* 知识图谱 API 服务
* 提供与后端交互的所有图谱相关接口
*/
2026-04-05 14:41:11 +08:00
export const graphApi = {
2026-04-06 14:24:45 +08:00
/**
* 获取完整的知识图谱数据
* @returns 节点和边的集合
*/
2026-04-05 14:41:11 +08:00
async getData(): Promise<GraphData> {
try {
const response = await apiClient.get<GraphData>('/graph');
return response.data;
} catch (error) {
2026-04-06 14:24:45 +08:00
console.error('Failed to fetch graph data:', error);
2026-04-05 14:41:11 +08:00
return { nodes: [], edges: [] };
}
},
2026-04-06 14:24:45 +08:00
/**
* 根据 ID 获取节点详情
* @param id 节点 ID
* @returns 节点对象或 null
*/
2026-04-05 14:41:11 +08:00
async getNodeById(id: string): Promise<Node | null> {
try {
const response = await apiClient.get<Node>(`/nodes/${id}`);
return response.data;
} catch (error) {
2026-04-06 14:24:45 +08:00
console.error('Failed to fetch node:', error);
2026-04-05 14:41:11 +08:00
return null;
}
},
};
export default apiClient;